| 1 | package Model::Envoy::Set; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | ▶ | use MooseX::Role::Parameterized; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use Module::Runtime 'use_module'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 5 | our $VERSION = '0.3.0'; | |||||||||
| 8 | |||||||||||
| 9 | =head1 Model::Envoy::Set | ||||||||||
| 10 | |||||||||||
| 11 | A role for creating, finding and listing Model::Envoy based objects. Similar in | ||||||||||
| 12 | philosophy to DBIx::Class::ResultSets. | ||||||||||
| 13 | |||||||||||
| 14 | =head2 Synopsis | ||||||||||
| 15 | |||||||||||
| 16 | package My::Models; | ||||||||||
| 17 | |||||||||||
| 18 | use Moose; | ||||||||||
| 19 | with 'Model::Envoy::Set' => { namespace => 'My::Envoy' }; | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | ....then later... | ||||||||||
| 23 | |||||||||||
| 24 | my $widget = My::Models->m('Widget')->fetch( id => 2 ); | ||||||||||
| 25 | |||||||||||
| 26 | $widget->name('renamed'); | ||||||||||
| 27 | $widget->save; | ||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | =head2 Configuration | ||||||||||
| 31 | |||||||||||
| 32 | When incorporating this role into your class, you will need to specify the perl namespace where | ||||||||||
| 33 | your model classes reside per the synopsis above. | ||||||||||
| 34 | |||||||||||
| 35 | =head2 Methods | ||||||||||
| 36 | |||||||||||
| 37 | =head3 m($type) | ||||||||||
| 38 | |||||||||||
| 39 | Returns an Envoy::Set of the specified $type. So for a class My::Model::Foo | ||||||||||
| 40 | |||||||||||
| 41 | my $set = My::Models->m('Foo'); | ||||||||||
| 42 | |||||||||||
| 43 | =head3 build(\%params) | ||||||||||
| 44 | |||||||||||
| 45 | Create a new instance of the Model::Envoy based class referenced by the set: | ||||||||||
| 46 | |||||||||||
| 47 | my $instance = $set->build({ | ||||||||||
| 48 | attribute => $value, | ||||||||||
| 49 | ... | ||||||||||
| 50 | }); | ||||||||||
| 51 | |||||||||||
| 52 | =head3 fetch(%params) | ||||||||||
| 53 | |||||||||||
| 54 | Retrieve an object from storage | ||||||||||
| 55 | |||||||||||
| 56 | my $model = $set->fetch( id => 1 ); | ||||||||||
| 57 | |||||||||||
| 58 | =head3 list(%params) | ||||||||||
| 59 | |||||||||||
| 60 | Query storage and return a list of objects that matched the query | ||||||||||
| 61 | |||||||||||
| 62 | my $models = $set->list( | ||||||||||
| 63 | color => 'green', | ||||||||||
| 64 | size => 'small', | ||||||||||
| 65 | ... | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | =head3 get_storage($storage_package) | ||||||||||
| 69 | |||||||||||
| 70 | Passes back the storage plugin specified by C<$storage_package> being used by the set's model type. | ||||||||||
| 71 | Follows the same namespace resolution process as the C<Model::Envoy> method of the same name. | ||||||||||
| 72 | |||||||||||
| 73 | =head3 load_types(@names) | ||||||||||
| 74 | |||||||||||
| 75 | For now Model::Envoy does not slurp all the classes in a certain namespace | ||||||||||
| 76 | for use with $set->m(). Call load_types() at the start of your program instead: | ||||||||||
| 77 | |||||||||||
| 78 | My::Models->load_types( qw( Foo Bar Baz ) ); | ||||||||||
| 79 | |||||||||||
| 80 | =cut | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 5 | parameter namespace => ( | |||||||||
| 84 | isa => 'Str', | ||||||||||
| 85 | required => 1, | ||||||||||
| 86 | ); | ||||||||||
| 87 | |||||||||||
| 88 | 5 | ▶ | role { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 89 | |||||||||||
| 90 | 5 | my $namespace = shift->namespace; | |||||||||
| 91 | |||||||||||
| 92 | 16 | ▶ | method '_namespace' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_namespace: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 16 | $namespace; | |||||||||
| 94 | 5 | }; | |||||||||
| 95 | 5 | }; | |||||||||
| 96 | |||||||||||
| 97 | 5 | has model_class => ( | |||||||||
| 98 | is => 'rw', | ||||||||||
| 99 | isa => 'Str', | ||||||||||
| 100 | required => 1, | ||||||||||
| 101 | ); | ||||||||||
| 102 | |||||||||||
| 103 | 4 | ▶ | sub m { | ||||||||
|
Subroutine
m: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 104 | 4 | my ( $class, $name ) = @_; | |||||||||
| 105 | |||||||||||
| 106 | 4 | my $namespace = $class->_namespace; | |||||||||
| 107 | |||||||||||
| 108 | 4 | $name =~ s/^$namespace\:://; | |||||||||
| 109 | |||||||||||
| 110 | 4 | return $class->new( model_class => "$namespace\::$name" ); | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 3 | ▶ | sub build { | ||||||||
|
Subroutine
build: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 114 | 3 | my( $self, $params, $no_rel ) = @_; | |||||||||
| 115 | |||||||||||
| 116 | 3 | return $self->model_class->build($params,$no_rel); | |||||||||
| 117 | } | ||||||||||
| 118 | 13 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 119 | 13 | my $self = shift; | |||||||||
| 120 | |||||||||||
| 121 | 13 | ▶ | return undef unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 12 | return $self->model_class->_dispatch('fetch', @_ ); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 7 | ▶ | sub list { | ||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 126 | 7 | my $self = shift; | |||||||||
| 127 | |||||||||||
| 128 | 7 | return $self->model_class->_dispatch('list', @_ ); | |||||||||
| 129 | |||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 2 | ▶ | sub get_storage { | ||||||||
|
Subroutine
get_storage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 133 | 2 | my $self = shift; | |||||||||
| 134 | |||||||||||
| 135 | 2 | $self->model_class->get_storage(@_); | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 14 | ▶ | sub load_types { | ||||||||
|
Subroutine
load_types: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 139 | 14 | my ( $self, @types ) = @_; | |||||||||
| 140 | |||||||||||
| 141 | 14 | my $namespace = $self->_namespace; | |||||||||
| 142 | |||||||||||
| 143 | 14 | foreach my $type ( @types ) { | |||||||||
| 144 | |||||||||||
| 145 | 17 | ▶ | die "invalid type name '$type'" unless $type =~ /^[a-z]+$/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | |||||||||||
| 147 | 11 | ▶ | use_module("$namespace\::$type") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | or die "Could not load model type '$type'"; | ||||||||||
| 149 | } | ||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 5 | 1; | |||||||||