Toby Inkster avatar Toby Inkster committed 9ff63ec

porting to MooseX::Declare

Comments (0)

Files changed (7)

lib/CRM/CARE/Contact.pm

-package CRM::CARE::Contact;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose;
+class CRM::CARE::Contact
+	with CRM::CARE::Trait::WithDefaultConnection
+	with CRM::CARE::Trait::Loader
+	with CRM::CARE::Trait::Updater
+	with CRM::CARE::Trait::Amendable
+{
+	use CRM::CARE::TypeLibrary ':all';
 
-use namespace::clean -except => 'meta';
+	has 'ContactNumber'  => (is => 'ro', isa => 'Int', required => 1);
+	has 'AddressNumber'  => (is => 'rw', isa => 'Int');
 
-our $VERSION = '0.001';
-
-use CRM::CARE::TypeLibrary ':all';
-
-has 'ContactNumber'  => (is => 'ro', isa => 'Int', required => 1);
-has 'AddressNumber'  => (is => 'rw', isa => 'Int');
-
-has [qw{
-	Title
-	Initials
-	Forenames
-	Surname
-	Honorifics
-	Salutation
-	LabelName
-	PreferredForename
-	Sex
-	Source
-	SourceDesc
-	Status
-	StatusDesc
-	StatusReason
-	Department
-	OwnershipGroup
-	PrincipalUser
-	PrincipalUserReason
-	VatCategory
-	VatNumber
-	}] => (is => 'rw', isa => 'Str');
-
-has [qw{
-	SourceDate
-	StatusDate
-	}] => (is => 'rw', isa => 'CareDate', coerce => 1);
-
-with 'CRM::CARE::Trait::WithDefaultConnection';
-with 'CRM::CARE::Trait::Loader';
-with 'CRM::CARE::Trait::Updater';
-with 'CRM::CARE::Trait::Amendable';
-
-sub _retrieve_method  { return FindContacts => qw(ContactNumber); }
-sub _update_method
-{
-	return UpdateContact => qw(
-		ContactNumber
+	has [qw{
 		Title
 		Initials
 		Forenames
 		PreferredForename
 		Sex
 		Source
+		SourceDesc
 		Status
+		StatusDesc
 		StatusReason
 		Department
 		OwnershipGroup
 		PrincipalUserReason
 		VatCategory
 		VatNumber
+		}] => (is => 'rw', isa => 'Str');
+
+	has [qw{
 		SourceDate
 		StatusDate
-		);
-}
+		}] => (is => 'rw', isa => 'CareDate', coerce => 1);
 
-sub _SelectContactData
-{
-	my ($self, @x) = @_;
-	my ($care, $type, %args) = $self->process_method_arguments(@x);
-	$args{ContactNumber} = $self->ContactNumber;
+	method _retrieve_method ()
+		{ return FindContacts => qw(ContactNumber); }
+		
+	method _update_method ()
+	{
+		return UpdateContact => qw(
+			ContactNumber
+			Title
+			Initials
+			Forenames
+			Surname
+			Honorifics
+			Salutation
+			LabelName
+			PreferredForename
+			Sex
+			Source
+			Status
+			StatusReason
+			Department
+			OwnershipGroup
+			PrincipalUser
+			PrincipalUserReason
+			VatCategory
+			VatNumber
+			SourceDate
+			StatusDate
+			);
+	}
 
-	if (my $bless = delete $args{bless})
+	method _SelectContactData (@x)
 	{
-		return map
-			{ $bless->new(%$_) }
-			$care->SelectContactData({pSelectDataType => $type}, %args);
+		my ($care, $type, %args) = $self->process_method_arguments(@x);
+		$args{ContactNumber} = $self->ContactNumber;
+
+		if (my $bless = delete $args{bless})
+		{
+			return map
+				{ $bless->new(%$_) }
+				$care->SelectContactData({pSelectDataType => $type}, %args);
+		}
+		
+		return $care->SelectContactData({pSelectDataType => $type}, %args);
+	}
+
+	method information ()
+	{
+		my $i = $self->_SelectContactData('xcdtContactInformation');
+		foreach (keys %$i)
+		{
+			$self->$_($i->{$_})
+				if $self->can($_) && !defined $self->$_;
+		}
+		return $i;
+	}
+
+	method addresses ()
+	{
+		return $self->_SelectContactData('xcdtContactAddresses', bless=>'CRM::CARE::Address');
+	}
+
+	method activities ()
+	{
+		return $self->_SelectContactData('xcdtContactCategories');
+	}
+
+	method suppressions ()
+	{
+		return $self->_SelectContactData('xcdtContactSuppressions');
 	}
 	
-	return $care->SelectContactData({pSelectDataType => $type}, %args);
-}
-
-sub information
-{
-	my ($self) = @_;
-	my $i = $self->_SelectContactData('xcdtContactInformation');
-	foreach (keys %$i)
-	{
-		$self->$_($i->{$_})
-			if $self->can($_) && !defined $self->$_;
-	}
-	return $i;
-}
-
-sub addresses
-{
-	my ($self) = @_;
-	return $self->_SelectContactData('xcdtContactAddresses', bless=>'CRM::CARE::Address');
-}
-
-sub activities
-{
-	my ($self) = @_;
-	return $self->_SelectContactData('xcdtContactCategories');
-}
-
-sub suppressions
-{
-	my ($self) = @_;
-	return $self->_SelectContactData('xcdtContactSuppressions');
-}
-
+};
 1;
 
 =head1 NAME

lib/CRM/CARE/Trait/Amendable.pm

-package CRM::CARE::Trait::Amendable;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose::Role;
+role CRM::CARE::Trait::Amendable
+{
+	use CRM::CARE::TypeLibrary ':all';
 
-use namespace::clean -except => 'meta';
-
-use CRM::CARE::TypeLibrary ':all';
-
-our $VERSION = '0.001';
-
-has 'AmendedBy'  => (is => 'rw', isa => 'Str');
-has 'AmendedOn'  => (is => 'rw', isa => 'CareDate', coerce => 1);
-
+	has 'AmendedBy'  => (is => 'rw', isa => 'Str');
+	has 'AmendedOn'  => (is => 'rw', isa => 'CareDate', coerce => 1);
+};
 1;
 
 =head1 NAME

lib/CRM/CARE/Trait/Configurable.pm

-package CRM::CARE::Trait::Configurable;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose::Role;
+role CRM::CARE::Trait::Configurable
+{
+	use Config::Tiny;
 
-use Config::Tiny;
+	requires 'new';
+	requires 'configfile';
 
-use namespace::clean -except => 'meta';
+	method new_from_config ($class: %args)
+	{
+		my @configfile = $class->configfile;
+		
+		if (exists $args{configfile})
+		{
+			$args{configfile} = [$args{configfile}] unless ref $args{configfile} eq 'ARRAY';
+			@configfile = (@{ $args{configfile} }, @configfile);
+			delete $args{configfile};
+		}
+		
+		my $file;
+		FILE: foreach (@configfile)
+		{
+			if (-e)
+			{
+				$file = $_;
+				last FILE;
+			}
+		}
+		confess sprintf("None of the configfiles (%s) exist.", join('|', @configfile))
+			unless defined $file;
 
-our $VERSION = '0.001';
+		my %parameters = %args;
+		my $ct = Config::Tiny->read($file);
+		foreach my $section (keys %$ct)
+		{
+			foreach my $key (keys %{ $ct->{$section} })
+			{
+				my $p = $section eq '_' ? $key : sprintf('%s_%s', $section, $key);
+				$parameters{$p} = $ct->{$section}{$key};
+			}
+		}
+		
+		$class->new(%parameters);
+	}
 
-requires 'new';
-requires 'configfile';
-
-sub new_from_config
-{
-	my ($class, %args) = @_;
-	my @configfile = $class->configfile;
-	
-	if (exists $args{configfile})
-	{
-		$args{configfile} = [$args{configfile}] unless ref $args{configfile} eq 'ARRAY';
-		@configfile = (@{ $args{configfile} }, @configfile);
-		delete $args{configfile};
-	}
-	
-	my $file;
-	FILE: foreach (@configfile)
-	{
-		if (-e)
-		{
-			$file = $_;
-			last FILE;
-		}
-	}
-	confess sprintf("None of the configfiles (%s) exist.", join('|', @configfile))
-		unless defined $file;
-
-	my %parameters = %args;
-	my $ct = Config::Tiny->read($file);
-	foreach my $section (keys %$ct)
-	{
-		foreach my $key (keys %{ $ct->{$section} })
-		{
-			my $p = $section eq '_' ? $key : sprintf('%s_%s', $section, $key);
-			$parameters{$p} = $ct->{$section}{$key};
-		}
-	}
-	
-	$class->new(%parameters);
-}
-
+};
 1;
 
 =head1 NAME

lib/CRM/CARE/Trait/Loader.pm

-package CRM::CARE::Trait::Loader;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose::Role;
+role CRM::CARE::Trait::Loader
+	with CRM::CARE::Trait::WithDefaultConnection
+{
+	requires '_retrieve_method';
 
-use DBI;
-
-use namespace::clean -except => 'meta';
-
-our $VERSION = '0.001';
-
-with 'CRM::CARE::Trait::WithDefaultConnection';
-
-requires '_retrieve_method';
-
-sub retrieve
-{
-	my ($class, @x) = @_;
-	my ($care,  @params) = $class->process_method_arguments(@x);
-	
-	my %s_args;
-	my ($_load_method, @_load_params) = $class->_retrieve_method;
-	foreach (my $i=0; $i < scalar @_load_params; $i++)
+	method retrieve ($class: @x)
 	{
-		$s_args{ $_load_params[$i] } = $params[$i];
+		my ($care,  @params) = $class->process_method_arguments(@x);
+		
+		my %s_args;
+		my ($_load_method, @_load_params) = $class->_retrieve_method;
+		foreach (my $i=0; $i < scalar @_load_params; $i++)
+		{
+			$s_args{ $_load_params[$i] } = $params[$i];
+		}
+		
+		my @results = map
+			{ $class->new(%$_) }
+			$care->call_service($_load_method, %s_args);
+		return unless @results;
+		return wantarray ? @results : $results[0];
 	}
-	
-	my @results = map
-		{ $class->new(%$_) }
-		$care->call_service($_load_method, %s_args);
-	return unless @results;
-	return wantarray ? @results : $results[0];
-}
-
+};
 1;
 
 =head1 NAME

lib/CRM/CARE/Trait/Updater.pm

-package CRM::CARE::Trait::Updater;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose::Role;
+role CRM::CARE::Trait::Updater
+	with CRM::CARE::Trait::WithDefaultConnection
+{
+	requires '_update_method';
 
-use DBI;
+	method update (@x)
+	{
+		my ($care,  %params) = $self->process_method_arguments(@x);
 
-use namespace::clean -except => 'meta';
+		my %s_args;
+		my ($_save_method, @_save_params) = $self->_update_method;
+		PARAM: foreach my $sp (@_save_params)
+		{
+			if (ref $sp eq 'ARRAY')
+			{
+				my $meth = $sp->[0];
+				next PARAM unless defined $self->$meth;
+				$s_args{$sp->[1]} = $self->$meth;
+			}
+			else
+			{
+				next PARAM unless defined $self->$sp;
+				$s_args{$sp} = $self->$sp;
+			}
+		}
+		
+		return $care->call_service($_save_method, %s_args);
+	}
 
-our $VERSION = '0.001';
-
-	with 'CRM::CARE::Trait::WithDefaultConnection';
-
-requires '_update_method';
-
-sub update
-{
-	my ($self, @x) = @_;
-	my ($care,  %params) = $self->process_method_arguments(@x);
-
-	my %s_args;
-	my ($_save_method, @_save_params) = $self->_update_method;
-	PARAM: foreach my $sp (@_save_params)
-	{
-		if (ref $sp eq 'ARRAY')
-		{
-			my $meth = $sp->[0];
-			next PARAM unless defined $self->$meth;
-			$s_args{$sp->[1]} = $self->$meth;
-		}
-		else
-		{
-			next PARAM unless defined $self->$sp;
-			$s_args{$sp} = $self->$sp;
-		}
-	}
-	
-	return $care->call_service($_save_method, %s_args);
-}
-
+};
 1;
 
 =head1 NAME

lib/CRM/CARE/Trait/Validable.pm

-package CRM::CARE::Trait::Validable;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose::Role;
+role CRM::CARE::Trait::Validable
+{
+	use DateTime;
+	use CRM::CARE::TypeLibrary ':all';
 
-use DateTime;
+	has 'ValidFrom'  => (is => 'rw', isa => 'CareDate', coerce => 1);
+	has 'ValidTo'    => (is => 'rw', isa => 'CareDate', coerce => 1);
 
-use namespace::clean -except => 'meta';
+	method is_current (DateTime $when)
+	{
+		$when //= DateTime->now;
+		return
+			if (defined $self->ValidFrom and $when < $self->ValidFrom);		
+		return
+			if (defined $self->ValidTo and $when > $self->ValidTo);
+		return $self;
+	}
 
-our $VERSION = '0.001';
+	method is_future (DateTime $when)
+	{
+		$when //= DateTime->now;
+		return $self
+			if (defined $self->ValidFrom and $when > $self->ValidFrom);		
+		return;
+	}
 
-use CRM::CARE::TypeLibrary ':all';
-
-has 'ValidFrom'  => (is => 'rw', isa => 'CareDate', coerce => 1);
-has 'ValidTo'    => (is => 'rw', isa => 'CareDate', coerce => 1);
-
-sub is_current
-{
-	my ($self, $when) = @_;
-	$when //= DateTime->now;
-	
-	return
-		if (defined $self->ValidFrom and $when < $self->ValidFrom);
-	
-	return
-		if (defined $self->ValidTo and $when > $self->ValidTo);
-	
-	return $self;
-}
-
-sub is_future
-{
-	my ($self, $when) = @_;
-	$when //= DateTime->now;
-	
-	return $self
-		if (defined $self->ValidFrom and $when > $self->ValidFrom);
-	
-	return;
-}
-
-sub is_historic
-{
-	my ($self, $when) = @_;
-	$when //= DateTime->now;
-	
-	return $self
-		if (defined $self->ValidTo and $when < $self->ValidTo);
-	
-	return;
-}
+	method is_historic (DateTime $when)
+	{
+		$when //= DateTime->now;
+		return $self
+			if (defined $self->ValidTo and $when < $self->ValidTo);		
+		return;
+	}
+};
 
 1;
 

lib/CRM/CARE/Trait/WithDefaultConnection.pm

-package CRM::CARE::Trait::WithDefaultConnection;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose::Role;
+role CRM::CARE::Trait::WithDefaultConnection
+{
+	our $default;
 
-use namespace::clean -except => 'meta';
+	method set_default_connection ($conn)
+	{
+		$default = $conn;
+	}
 
-our $VERSION = '0.001';
+	method get_default_connection ()
+	{
+		unless ($default)
+		{
+			$default = CRM::CARE->new_from_config;
+		}		
+		return $default;
+	}
 
-our $default;
-
-sub set_default_connection
-{
-	my ($self, $conn) = @_;
-	$default = $conn;
-}
-
-sub get_default_connection
-{
-	my ($self) = @_;
-	
-	unless ($default)
+	method process_method_arguments (@args)
 	{
-		$default = CRM::CARE->new_from_config;
+		if (blessed($args[0]) and $args[0]->isa('CRM::CARE'))
+		{
+			return @args;
+		}
+		
+		return ($self->get_default_connection, @args);
 	}
-	
-	return $default;
-}
-
-sub process_method_arguments
-{
-	my ($self, @args) = @_;
-
-	if (blessed($args[0]) and $args[0]->isa('CRM::CARE'))
-	{
-		return @args;
-	}
-	
-	return ($self->get_default_connection, @args);
-}
-
-1;
+};
 
 =head1 NAME
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.