Skip to content

Instantly share code, notes, and snippets.

@delonnewman
Created October 7, 2008 20:19
Show Gist options
  • Save delonnewman/15367 to your computer and use it in GitHub Desktop.
Save delonnewman/15367 to your computer and use it in GitHub Desktop.
package Domestic::Member;
use Moose;
use Moose::Util::TypeConstraints;
with 'MooseX::Param';
use DateTime;
use Domestic;
use Domestic::DB;
#
## User Param bit flags
use constant {
MEMBER_EMPLOYER => 1<<1,
MEMBER_PROVIDER => 1<<2,
SUBSCRIPTION => 1<<33,
STATUS_ACTIVE => 1<<3,
STATUS_PENDING => 1<<4,
STATUS_INCOMPLETE => 1<<34,
STATUS_EXPIRED => 1<<35
};
class_type('DateTime');
subtype 'Date'
=> as 'DateTime'
=> where { $_->isa('DateTime') };
coerce 'DateTime'
=> from 'Str'
=> via {
/(\d\d\d\d)-(\d\d)-(\d\d)/;
DateTime->new(
year => int($1) | 1,
month => int($2) | 1,
day => int($3) | 1
);
};
has 'id' => ( is => 'rw', isa => 'Int' );
has 'name' => ( is => 'rw', isa => 'Maybe[Str]' );
has 'email' => ( is => 'rw', isa => 'Maybe[Str]' );
has 'status' => ( is => 'rw', isa => 'Maybe[Int]' );
has 'portrait' => ( is => 'rw', isa => 'Maybe[Str]' );
has 'background' => ( is => 'rw', isa => 'Maybe[Str]' );
has 'experation_date' => (
is => 'rw',
isa => 'DateTime',
coerce => 1,
default => sub { DateTime->now()->add( days => 15 ) }
);
sub find {
my $q = shift;
my $options = shift;
# FIXME: Clean this up
my $query = <<QUERY;
SELECT
jos_pd_members_provider.id,
CONCAT(name, ' ', lastname) AS name,
params,
email,
status,
portrait,
background,
notificationDate AS experation_date
FROM jos_pd_members_provider, jos_users
WHERE
jos_pd_members_provider.uid = jos_users.id
AND jos_pd_members_provider.id = ?;
QUERY
my ( $id, $name, $params, $email, $status,
$portrait, $background, $experation_date ) =
Domestic->new->db->get_row( $query, [$q] );
return Domestic::Member->new(
id => $id,
name => $name,
params => _parse_params($params),
email => $email,
status => $status,
portrait => $portrait,
background => $background,
experation_date => $experation_date
);
}
sub _parse_params {
my $param_string = shift;
my %params;
my @params = map { split /=/, $param_string } split /\n/, $param_string;
foreach my $params (@params) {
$params{$params[0]} = $params[1];
}
return \%params;
}
sub find_first {
}
sub find_all {
}
sub find_by_query {
}
sub find_expired {
}
sub find_active {
}
sub find_inactive {
}
sub find_pending {
}
sub send_mail_to {
}
sub is_past_experation {
return DateTime->compare(DateTime->now(), $_[0]->experation_date) | 0;
}
sub is_employer {
my $self = shift;
return ($self->param('flags') & MEMBER_EMPLOYER) == MEMBER_EMPLOYER
}
sub is_provider {
my $self = shift;
return ($self->param('flags') & MEMBER_PROVIDER) == MEMBER_PROVIDER
}
sub is_active {
my $self = shift;
return ($self->param('flags') & STATUS_ACTIVE) == STATUS_ACTIVE
}
sub is_pending {
my $self = shift;
return ($self->param('flags') & STATUS_PENDING) == STATUS_PENDING
}
sub is_expired {
my $self = shift;
return ($self->param('flags') & STATUS_EXPIRED) == STATUS_EXPIRED
}
sub is_incomplete {
my $self = shift;
return ($self->param('flags') & STATUS_INCOMPLETE) == STATUS_INCOMPLETE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment