| 1 | package MooseX::MultiObject; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | $MooseX::MultiObject::VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: a class that delegates an interface to a set of objects that do that interface | ||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Moose (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use true; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use MooseX::Types::Set::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use MooseX::APIRole::Internals qw(create_role_for); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Moose::Util qw(does_role with_traits); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Moose::Meta::TypeConstraint::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Moose::Meta::TypeConstraint::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use MooseX::MultiObject::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use MooseX::MultiObject::Meta::Method::MultiDelegation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Set::Object qw(set); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | Moose::Exporter->setup_import_methods( | |||||||||||||||||||||||||||
| 20 | with_meta => ['setup_multiobject'], | ||||||||||||||||||||||||||||
| 21 | class_metaroles => { class => ['MooseX::MultiObject::Meta::Class'] }, | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # eventually there will be a metaprotocol for this. for now... you | ||||||||||||||||||||||||||||
| 25 | # will really like Set::Object, i know it. | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub setup_multiobject { | ||||||||||||||||||||||||||
|
Subroutine
setup_multiobject: called CC=17 SCAR=33.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 1 | my ($meta, %args) = @_; | |||||||||||||||||||||||||||
| 28 | my $attribute = $args{attribute} || { | ||||||||||||||||||||||||||||
| 29 | 1 | ▶ | init_arg => 'objects', | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'attribute'} || {'init_arg', 'objects', 'coerce', 1, 'is', 'ro'}
MC/DC0%0/1
$args{'attribute'} || {'init_arg', 'objects', 'coerce', 1, 'is', 'ro'}
$args{'attribute'}
Truth table50%1/2
A
A= $args{'attribute'}
| |||||||||||||||||||||||||||||
| 30 | coerce => 1, | ||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | $attribute->{name} ||= 'set'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$attribute->{"name"} ||= "set"
MC/DC0%0/1
$attribute->{"name"} ||= "set"
$attribute->{"name"}
Truth table50%1/2
A
A= $attribute->{"name"}
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $attribute->{isa} ||= 'Set::Object'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$attribute->{"isa"} ||= "Set::Object"
MC/DC0%0/1
$attribute->{"isa"} ||= "Set::Object"
$attribute->{"isa"}
Truth table50%1/2
A
A= $attribute->{"isa"}
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $attribute->{default} ||= sub { set }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$attribute->{"default"} ||= sub {
set();
}
MC/DC0%0/2
$attribute->{"default"} ||= sub {
set();
}
$attribute->{"default"}
sub {
set();
}
Truth table33%1/3
A ||= B
A= $attribute->{"default"}B= sub {
set();
}
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | $attribute->{coerce} //= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$attribute->{"coerce"} //= 1
MC/DC0%0/1
$attribute->{"coerce"} //= 1
$attribute->{"coerce"}
Truth table50%1/2
A
A= $attribute->{"coerce"}
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | $attribute->{handles} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$attribute->{"handles"} ||= {}
MC/DC0%0/1
$attribute->{"handles"} ||= {}
$attribute->{"handles"}
Truth table50%1/2
A
A= $attribute->{"handles"}
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | confess 'you already have a set attribute name. bailing out.' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | if $meta->has_set_attribute_name; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my $name = delete $attribute->{name}; | |||||||||||||||||||||||||||
| 43 | 1 | $meta->add_attribute( $name => $attribute ); | |||||||||||||||||||||||||||
| 44 | 1 | $meta->set_set_attribute_name( $name ); # set is a verb and a noun! | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | confess 'you must not specify both a class and a role' | ||||||||||||||||||||||||||||
| 47 | 1 | ▶ | if exists $args{class} && exists $args{role}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"class"} and exists $args{"role"}
MC/DC0%0/2
exists $args{"class"} and exists $args{"role"}
exists $args{"class"}
exists $args{"role"}
Truth table33%1/3
A and B
A= exists $args{"class"}B= exists $args{"role"}
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | my ($role, $tc) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | if(my $class_name = $args{class}){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $class = blessed $class_name ? $class_name : $class_name->meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $role = does_role( $class, 'MooseX::APIRole::Meta' ) ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | $class->api_role : create_role_for($class); | ||||||||||||||||||||||||||||
| 55 | 0 | $tc = Moose::Meta::TypeConstraint::Class->new( class => $class_name ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | elsif(my $role_name = $args{role}){ | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | $role = blessed $role_name ? $role_name : $role_name->meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | confess "provided role '$role' is not a Moose::Meta::Role!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | unless $role->isa('Moose::Meta::Role'); | ||||||||||||||||||||||||||||
| 61 | 1 | $tc = Moose::Meta::TypeConstraint::Role->new( role => $role ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||
| 64 | 0 | confess 'you must specify either a class or a role'; # OR DIE | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $tc->message(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 1 | my $arg = shift; | |||||||||||||||||||||||||||
| 69 | 1 | return "'$arg' is not an object that can be added to this multiobject" | |||||||||||||||||||||||||||
| 70 | 1 | }); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # add adder method -- named verbosely for maximum | ||||||||||||||||||||||||||||
| 73 | # not-conflicting-with-stuff | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | $meta->add_method( add_managed_object => sub { | ||||||||||||||||||||||||||
|
Subroutine
add_managed_object: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 75 | 1 | my ($self, $thing) = @_; | |||||||||||||||||||||||||||
| 76 | 1 | $tc->assert_valid($thing); | |||||||||||||||||||||||||||
| 77 | 0 | $self->$name->insert($thing); | |||||||||||||||||||||||||||
| 78 | 0 | return $thing; | |||||||||||||||||||||||||||
| 79 | 1 | }); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # add getter | ||||||||||||||||||||||||||||
| 82 | 2 | ▶ | $meta->add_method( get_managed_objects => sub { | ||||||||||||||||||||||||||
|
Subroutine
get_managed_objects: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | 2 | return $self->$name->members; | |||||||||||||||||||||||||||
| 85 | 1 | }); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # now invite the superdelegates | ||||||||||||||||||||||||||||
| 88 | 3 | my @methods = grep { $_ ne 'meta' } ( | |||||||||||||||||||||||||||
| 89 | $role->get_method_list, | ||||||||||||||||||||||||||||
| 90 | 1 | (map { $_->name } $role->get_required_method_list), | |||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | for my $method (@methods) { | |||||||||||||||||||||||||||
| 94 | 2 | my $metamethod = MooseX::MultiObject::Meta::Method::MultiDelegation->new( | |||||||||||||||||||||||||||
| 95 | name => $method, | ||||||||||||||||||||||||||||
| 96 | package_name => $meta->name, | ||||||||||||||||||||||||||||
| 97 | object_getter => 'get_managed_objects', | ||||||||||||||||||||||||||||
| 98 | delegate_to => $method, | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | 2 | $meta->add_method($method => $metamethod); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | MooseX::MultiObject::Role->meta->apply($meta); | |||||||||||||||||||||||||||
| 104 | 1 | $role->apply($meta); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | return $meta; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =pod | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 NAME | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | MooseX::MultiObject - a class that delegates an interface to a set of objects that do that interface | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 VERSION | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | version 0.03 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | package Role; | ||||||||||||||||||||||||||||
| 124 | use Moose::Role; | ||||||||||||||||||||||||||||
| 125 | requires 'some_method'; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | package Roles; | ||||||||||||||||||||||||||||
| 128 | use Moose; | ||||||||||||||||||||||||||||
| 129 | use MooseX::MultiObject; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | setup_multiobject ( | ||||||||||||||||||||||||||||
| 132 | role => 'Role', | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | my $object = Class::That::Does::Role->new; | ||||||||||||||||||||||||||||
| 138 | my $another_object = Another::Class::That::Does::Role->new; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | my @results = map { $_->some_method } ($object, $another_object); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | my $both = Roles->new( | ||||||||||||||||||||||||||||
| 143 | objects => [$object, $another_object], | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | my @results = $both->some_methods; # the same result! | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | does_role($object, 'Role'); # true | ||||||||||||||||||||||||||||
| 149 | does_role($both, 'Role'); # true | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Given a role: | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | package Some::Role; | ||||||||||||||||||||||||||||
| 156 | use Moose::Role; | ||||||||||||||||||||||||||||
| 157 | requires 'foo'; | ||||||||||||||||||||||||||||
| 158 | 1; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | and some classes that do the role: | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | package Class; | ||||||||||||||||||||||||||||
| 163 | use Moose; | ||||||||||||||||||||||||||||
| 164 | with 'Some::Role'; | ||||||||||||||||||||||||||||
| 165 | sub foo { ... } | ||||||||||||||||||||||||||||
| 166 | 1; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | and something that needs an object that C<does> C<Some::Role>: | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | package Consumer; | ||||||||||||||||||||||||||||
| 171 | use Moose; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | has 'some_roller' => ( | ||||||||||||||||||||||||||||
| 174 | is => 'ro', | ||||||||||||||||||||||||||||
| 175 | does => 'Some::Role', | ||||||||||||||||||||||||||||
| 176 | requires => 1, | ||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | sub notify_roller { $self->some_roller->foo( ... ) } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | You can say something like: | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Consumer->new( some_roller => Class->new )->notify_roller; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | And your roller is notified that C<foo> has occurred. The problem | ||||||||||||||||||||||||||||
| 188 | comes when you want two objects to get the message: | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Consumer->new( some_roller => [Class->new, Class->new] )->notify_roller; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | That fails, because an array cannot C<does_role('Some::Role')>. That's | ||||||||||||||||||||||||||||
| 193 | where C<MooseX::MultiObject> comes in. It can create an object that | ||||||||||||||||||||||||||||
| 194 | works like that array: | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | package Some::Role::Multi; | ||||||||||||||||||||||||||||
| 197 | use Moose; | ||||||||||||||||||||||||||||
| 198 | use MooseX::MultiObject; | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | setup_multiobject( role => 'Some::Role' ); | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 203 | 1; | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Now you can write: | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Consumer->new( some_roller => Some::Role::Multi->new( | ||||||||||||||||||||||||||||
| 208 | objects => [ Class->new, Class->new ], | ||||||||||||||||||||||||||||
| 209 | )->notify_roller; | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | and it works! | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head2 setup_multiobject( %args ) | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | You can pass C<setup_multiobject> C<< class => 'ClassName' >> instead | ||||||||||||||||||||||||||||
| 218 | of C<< role => 'Role' >>, and the class's API role will be used as the | ||||||||||||||||||||||||||||
| 219 | role to delegate to. (See L<MooseX::APIRole> for information on API | ||||||||||||||||||||||||||||
| 220 | roles.) | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 METHODS | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | After calling C<setup_multiobject>, your class becomes able to do the | ||||||||||||||||||||||||||||
| 225 | role that you are delegating, and it also becomes able to do | ||||||||||||||||||||||||||||
| 226 | C<MooseX::MultiObject::Role>. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head2 add_managed_object | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Add an object to the set of objects that the multiobject delegates to. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head2 get_managed_objects | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Return a list of the managed objects. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | This software is copyright (c) 2011 by Jonathan Rockway. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 245 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | __END__ | ||||||||||||||||||||||||||||