| 1 | package Form::Sensible::DelegateConnection; | ||||||||||
| 2 | |||||||||||
| 3 | 16 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 16 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 16 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | ## This overloads our object to be treated as a method call to 'call' if #accessed directly as a code ref. | ||||||||||
| 8 | 16 | ▶ | use overload q/&{}/ => sub { my $self = shift; return sub { $self->call(@_) } }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 16 | Moose::Exporter->setup_import_methods( | |||||||||
| 11 | as_is => [ 'FSConnector', \&Form::Sensible::DelegateConnection::FSConnector ] | ||||||||||
| 12 | ); | ||||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | coerce 'Form::Sensible::DelegateConnection' => from 'HashRef' => via { Form::Sensible::DelegateConnection->new( %{$_} ) }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 15 | 0 | ▶ | coerce 'Form::Sensible::DelegateConnection' => from 'ArrayRef' => via { return FSConnector( @{$_} ); }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 16 | 0 | ▶ | coerce 'Form::Sensible::DelegateConnection' => from 'CodeRef' => via { return FSConnector( $_ ); }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 17 | |||||||||||
| 18 | 16 | has 'delegate_function' => ( | |||||||||
| 19 | is => 'rw', | ||||||||||
| 20 | isa => 'CodeRef', | ||||||||||
| 21 | required => 1, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 2438 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 2438 | my $self = shift; | |||||||||
| 26 | 2438 | my $callingobject = shift; | |||||||||
| 27 | #die "asplode!"; | ||||||||||
| 28 | #print STDERR "Delegate Being Called\n"; | ||||||||||
| 29 | |||||||||||
| 30 | 2438 | return $self->delegate_function->($callingobject, @_); | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # creates a delegate connection | ||||||||||
| 34 | 136 | ▶ | sub FSConnector { | ||||||||
|
Subroutine
FSConnector: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 35 | 136 | my $object = shift; | |||||||||
| 36 | 136 | my $args = [ @_ ]; | |||||||||
| 37 | 0 | ▶ | my $sub = @{$args} ? sub { return $object->( @_, @{$args} ) } : $object; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 136 | ▶ | if ( ref($object) ne 'CODE' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 1 | my $method_name = shift @$args; | |||||||||
| 41 | 1 | ▶ | $sub = sub { return $object->$method_name( @_, @{$args} ) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 136 | return Form::Sensible::DelegateConnection->new( delegate_function => $sub ); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||
| 48 | |||||||||||
| 49 | 16 | 1; | |||||||||
| 50 | |||||||||||
| 51 | =head1 NAME | ||||||||||
| 52 | |||||||||||
| 53 | Form::Sensible::DelegateConnection - Represents a connection between one object and another | ||||||||||
| 54 | |||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||
| 56 | |||||||||||
| 57 | sub the_options { | ||||||||||
| 58 | my $calling_object = shift; | ||||||||||
| 59 | |||||||||||
| 60 | # do stuff to get options and return them | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | ## calls the_options when the delegate is invoked | ||||||||||
| 64 | my $options_delegate = FSConnector( \&the_options ); | ||||||||||
| 65 | |||||||||||
| 66 | ## OR -- | ||||||||||
| 67 | |||||||||||
| 68 | my $options_delegate = FSConnector( sub { | ||||||||||
| 69 | my $calling_object = shift; | ||||||||||
| 70 | |||||||||||
| 71 | ## do stuff to get options and return them | ||||||||||
| 72 | } ); | ||||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | ## OR -- | ||||||||||
| 76 | |||||||||||
| 77 | my $data_source_object = Data::Source->new(...); | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | ## calls $data_source_object->get_option_data($calling_object, ...); when the delegate is invoked | ||||||||||
| 81 | ## Automatically captures your $data_source_object in a closure. | ||||||||||
| 82 | |||||||||||
| 83 | my $options_delegate = FSConnector( $data_source_object, "get_option_data"); | ||||||||||
| 84 | |||||||||||
| 85 | ## OR -- | ||||||||||
| 86 | |||||||||||
| 87 | my $options_delegate = Form::Sensible::DelegateConnection->new( delegate_function => \&the_options ); | ||||||||||
| 88 | |||||||||||
| 89 | =head1 DESCRIPTION | ||||||||||
| 90 | |||||||||||
| 91 | Form::Sensible::DelegateConnection is an object that represents a connection | ||||||||||
| 92 | between one object and another. In Form::Sensible when an object can have its | ||||||||||
| 93 | behavior customized by another object, a Delegate, a C<DelegateConnection> | ||||||||||
| 94 | object is usually used to create the link between the two objects. See | ||||||||||
| 95 | L<Form::Sensible::Delegation> for more information on how DelegateConnection | ||||||||||
| 96 | objects are used within Form::Sensible. | ||||||||||
| 97 | |||||||||||
| 98 | =head1 ATTRIBUTES | ||||||||||
| 99 | |||||||||||
| 100 | =over 8 | ||||||||||
| 101 | |||||||||||
| 102 | =item C<delegate_function($calling_object, $optional_additional_arguments, ... )> | ||||||||||
| 103 | |||||||||||
| 104 | The C<delegate_function> refers to a function that will be run when this | ||||||||||
| 105 | delegate connection is used. In most cases you will not access this attribute | ||||||||||
| 106 | directly, preferring the C<FSConnector()> method, though if you are into deep | ||||||||||
| 107 | voodoo, you may. | ||||||||||
| 108 | |||||||||||
| 109 | =back | ||||||||||
| 110 | |||||||||||
| 111 | =head1 METHODS | ||||||||||
| 112 | |||||||||||
| 113 | =over 8 | ||||||||||
| 114 | |||||||||||
| 115 | =item C<new( %options )> | ||||||||||
| 116 | |||||||||||
| 117 | Creates a new DelegateConnection object with the provided options. All the | ||||||||||
| 118 | attributes above may be passed. | ||||||||||
| 119 | |||||||||||
| 120 | =item C<call( $calling_object, @argument_list )> | ||||||||||
| 121 | |||||||||||
| 122 | Calls the delegate function for the C<$calling_object> using the | ||||||||||
| 123 | C<@argument_list>. This is essentially a wrapper to overload an object so that | ||||||||||
| 124 | if it is accessed directly as a code ref, it will call the delegate function: | ||||||||||
| 125 | |||||||||||
| 126 | my $options_delegate = Form::Sensible::DelegateConnection->new( delegate_function => \&the_options ); | ||||||||||
| 127 | $options_delegate->( ... ); # same as $options_delegate->delegate_function->( ... ) | ||||||||||
| 128 | |||||||||||
| 129 | =back | ||||||||||
| 130 | |||||||||||
| 131 | =head1 FUNCTIONS | ||||||||||
| 132 | |||||||||||
| 133 | =over 8 | ||||||||||
| 134 | |||||||||||
| 135 | =item C<FSConnector(...)> | ||||||||||
| 136 | |||||||||||
| 137 | The C<FSConnector> function is available if you have C<use>d either | ||||||||||
| 138 | C<Form::Sensible> or C<Form::Sensible::DelegateConnection>. It is used to | ||||||||||
| 139 | easily create a C<Form::Sensible::DelegateConnection> object in-place. You | ||||||||||
| 140 | can call it two ways, first, passing a code ref: | ||||||||||
| 141 | |||||||||||
| 142 | # instead of: | ||||||||||
| 143 | my $connection = Form::Sensible::DelegateConnection->new( delegate_function => sub { ... } ); | ||||||||||
| 144 | |||||||||||
| 145 | # this does the same thing: | ||||||||||
| 146 | |||||||||||
| 147 | my $connection = FSConnector( sub { ... } ); | ||||||||||
| 148 | |||||||||||
| 149 | This is a modest savings in typing, but can be very convenient when you are | ||||||||||
| 150 | defining a number of delegates during form creation, for example. | ||||||||||
| 151 | |||||||||||
| 152 | C<FSConnector( ... )> is particularly useful when linking a delegate | ||||||||||
| 153 | connection to a method on an object. This method looks like this: | ||||||||||
| 154 | |||||||||||
| 155 | my $connection = FSConnector( $object, 'method_to_call'); | ||||||||||
| 156 | |||||||||||
| 157 | When used this way, The C<FSConnector> function will create a code ref for you | ||||||||||
| 158 | automatically capturing the passed object in a closure. Again, you can do this | ||||||||||
| 159 | yourself, but this makes your setup code a lot clearer. | ||||||||||
| 160 | |||||||||||
| 161 | As a further benefit, both methods will take additional args which will be | ||||||||||
| 162 | passed to the function or method after the args passed by the calling object: | ||||||||||
| 163 | |||||||||||
| 164 | my $connection = FSConnector( $object, 'method_to_call', 'additional','args'); | ||||||||||
| 165 | |||||||||||
| 166 | This can be useful, for example, when you are using the same object / method | ||||||||||
| 167 | repeatedly, but need slightly different information in each case. | ||||||||||
| 168 | |||||||||||
| 169 | |||||||||||
| 170 | =back | ||||||||||
| 171 | |||||||||||
| 172 | =head1 AUTHOR | ||||||||||
| 173 | |||||||||||
| 174 | Jay Kuri - E<lt>jayk@cpan.orgE<gt> | ||||||||||
| 175 | |||||||||||
| 176 | =head1 SPONSORED BY | ||||||||||
| 177 | |||||||||||
| 178 | Ionzero LLC. L<http://ionzero.com/> | ||||||||||
| 179 | |||||||||||
| 180 | =head1 SEE ALSO | ||||||||||
| 181 | |||||||||||
| 182 | L<Form::Sensible> | ||||||||||
| 183 | L<Form::Sensible::Delegation> | ||||||||||
| 184 | |||||||||||
| 185 | =head1 LICENSE | ||||||||||
| 186 | |||||||||||
| 187 | Copyright 2010 by Jay Kuri E<lt>jayk@cpan.orgE<gt> | ||||||||||
| 188 | |||||||||||
| 189 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 190 | |||||||||||
| 191 | =cut | ||||||||||