| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Class::MakeMethods::Standard::Universal - Generic Methods | ||
| 4 | |||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | package MyObject; | ||
| 9 | use Class::MakeMethods::Standard::Universal ( | ||
| 10 | no_op => 'this', | ||
| 11 | abstract => 'that', | ||
| 12 | delegate => { name=>'play_music', target=>'instrument', method=>'play' }, | ||
| 13 | ); | ||
| 14 | |||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | The Standard::Universal suclass of MakeMethods provides a [INCOMPLETE]. | ||
| 19 | |||
| 20 | =head2 Calling Conventions | ||
| 21 | |||
| 22 | When you C<use> this package, the method names you provide | ||
| 23 | as arguments cause subroutines to be generated and installed in | ||
| 24 | your module. | ||
| 25 | |||
| 26 | See L<Class::MakeMethods::Standard/"Calling Conventions"> for more information. | ||
| 27 | |||
| 28 | =head2 Declaration Syntax | ||
| 29 | |||
| 30 | To declare methods, pass in pairs of a method-type name followed | ||
| 31 | by one or more method names. | ||
| 32 | |||
| 33 | Valid method-type names for this package are listed in L<"METHOD | ||
| 34 | GENERATOR TYPES">. | ||
| 35 | |||
| 36 | See L<Class::MakeMethods::Standard/"Declaration Syntax"> and L<Class::MakeMethods::Standard/"Parameter Syntax"> for more information. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | package Class::MakeMethods::Standard::Universal; | ||
| 41 | |||
| 42 | $VERSION = 1.000; | ||
| 43 | use strict; | ||
| 44 | use Carp; | ||
| 45 | use Class::MakeMethods::Standard '-isasubclass'; | ||
| 46 | |||
| 47 | ######################################################################## | ||
| 48 | |||
| 49 | =head1 METHOD GENERATOR TYPES | ||
| 50 | |||
| 51 | =head2 no_op - Placeholder | ||
| 52 | |||
| 53 | For each method name passed, returns a subroutine with the following characteristics: | ||
| 54 | |||
| 55 | =over 4 | ||
| 56 | |||
| 57 | =item * | ||
| 58 | |||
| 59 | Does nothing. | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | You might want to create and use such methods to provide hooks for | ||
| 64 | subclass activity. | ||
| 65 | |||
| 66 | Sample declaration and usage: | ||
| 67 | |||
| 68 | package MyObject; | ||
| 69 | use Class::MakeMethods::Standard::Universal ( | ||
| 70 | no_op => 'whatever', | ||
| 71 | ); | ||
| 72 | ... | ||
| 73 | |||
| 74 | # Doesn't do anything | ||
| 75 | MyObject->whatever(); | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub no_op { | ||
| 80 | map { | ||
| 81 | my $method = $_; | ||
| 82 | $method->{name} => sub { } | ||
| 83 | } (shift)->_get_declarations(@_) | ||
| 84 | } | ||
| 85 | |||
| 86 | ######################################################################## | ||
| 87 | |||
| 88 | =head2 abstract - Placeholder | ||
| 89 | |||
| 90 | For each method name passed, returns a subroutine with the following characteristics: | ||
| 91 | |||
| 92 | =over 4 | ||
| 93 | |||
| 94 | =item * | ||
| 95 | |||
| 96 | Fails with an error message. | ||
| 97 | |||
| 98 | =back | ||
| 99 | |||
| 100 | This is intended to support the use of abstract methods, that must | ||
| 101 | be overidden in a useful subclass. | ||
| 102 | |||
| 103 | If each subclass is expected to provide an implementation of a given method, using this abstract method will replace the generic error message below with the clearer, more explicit error message that follows it: | ||
| 104 | |||
| 105 | Can't locate object method "foo" via package "My::Subclass" | ||
| 106 | The "foo" method is abstract and can not be called on My::Subclass | ||
| 107 | |||
| 108 | However, note that the existence of this method will be detected by UNIVERSAL::can(), so it is not suitable for use in optional interfaces, for which you may wish to be able to detect whether the method is supported or not. | ||
| 109 | |||
| 110 | Sample declaration and usage: | ||
| 111 | |||
| 112 | package MyObject; | ||
| 113 | use Class::MakeMethods::Standard::Universal ( | ||
| 114 | abstract => 'whatever', | ||
| 115 | ); | ||
| 116 | ... | ||
| 117 | |||
| 118 | package MySubclass; | ||
| 119 | sub whatever { ... } | ||
| 120 | |||
| 121 | # Failure | ||
| 122 | MyObject->whatever(); | ||
| 123 | |||
| 124 | # Success | ||
| 125 | MySubclass->whatever(); | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub abstract { | ||
| 130 | map { | ||
| 131 | my $method = $_; | ||
| 132 | $method->{name} => sub { | ||
| 133 | my $self = shift; | ||
| 134 | my $class = ref($self) ? "a " . ref($self) . " object" : $self; | ||
| 135 | croak("The $method->{name} method is abstract and can not be called on $class"); | ||
| 136 | } | ||
| 137 | } (shift)->_get_declarations(@_) | ||
| 138 | } | ||
| 139 | |||
| 140 | ######################################################################## | ||
| 141 | |||
| 142 | =head2 call_methods - Call methods by name | ||
| 143 | |||
| 144 | For each method name passed, returns a subroutine with the following characteristics: | ||
| 145 | |||
| 146 | =over 4 | ||
| 147 | |||
| 148 | =item * | ||
| 149 | |||
| 150 | Accepts a hash of key-value pairs, or a reference to hash of such pairs. For each pair, the key is interpreted as the name of a method to call, and the value is the argument to be passed to that method. | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | Sample declaration and usage: | ||
| 155 | |||
| 156 | package MyObject; | ||
| 157 | use Class::MakeMethods::Standard::Universal ( | ||
| 158 | call_methods => 'init', | ||
| 159 | ); | ||
| 160 | ... | ||
| 161 | |||
| 162 | my $object = MyObject->new() | ||
| 163 | $object->init( foo => 'Foozle', bar => 'Barbados' ); | ||
| 164 | |||
| 165 | # Equivalent to: | ||
| 166 | $object->foo('Foozle'); | ||
| 167 | $object->bar('Barbados'); | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub call_methods { | ||
| 172 | map { | ||
| 173 | my $method = $_; | ||
| 174 | $method->{name} => sub { | ||
| 175 | my $self = shift; | ||
| 176 | local @_ = %{$_[0]} if ( scalar @_ == 1 and ref($_[0]) eq 'HASH'); | ||
| 177 | while (scalar @_) { | ||
| 178 | my $key = shift; | ||
| 179 | $self->$key( shift ) | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } (shift)->_get_declarations(@_) | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | ######################################################################## | ||
| 187 | |||
| 188 | =head2 join_methods - Concatenate results of other methods | ||
| 189 | |||
| 190 | For each method name passed, returns a subroutine with the following characteristics: | ||
| 191 | |||
| 192 | =over 4 | ||
| 193 | |||
| 194 | =item * | ||
| 195 | |||
| 196 | Has a list of other methods names as an arrayref in the 'methods' parameter. B<Required>. | ||
| 197 | |||
| 198 | =item * | ||
| 199 | |||
| 200 | When called, calls each of the named method on itself, in order, and returns the concatenation of their results. | ||
| 201 | |||
| 202 | =item * | ||
| 203 | |||
| 204 | If a 'join' parameter is provided it is included between each method result. | ||
| 205 | |||
| 206 | =item * | ||
| 207 | |||
| 208 | If the 'skip_blanks' parameter is omitted, or is provided with a true value, removes all undefined or empty-string values from the results. | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub join_methods { | ||
| 215 | map { | ||
| 216 | my $method = $_; | ||
| 217 | $method->{methods} or confess; | ||
| 218 | $method->{join} = '' if ( ! defined $method->{join} ); | ||
| 219 | $method->{skip_blanks} = '1' if ( ! defined $method->{skip_blanks} ); | ||
| 220 | $method->{name} => sub { | ||
| 221 | my $self = shift; | ||
| 222 | my $joiner = $method->{join}; | ||
| 223 | my @values = map { $self->$_() } @{ $method->{methods} }; | ||
| 224 | @values = grep { defined and length } @values if ( $method->{skip_blanks} ); | ||
| 225 | join $joiner, @values; | ||
| 226 | } | ||
| 227 | } (shift)->_get_declarations(@_) | ||
| 228 | } | ||
| 229 | |||
| 230 | ######################################################################## | ||
| 231 | |||
| 232 | =head2 alias - Call another method | ||
| 233 | |||
| 234 | For each method name passed, returns a subroutine with the following characteristics: | ||
| 235 | |||
| 236 | =over 4 | ||
| 237 | |||
| 238 | =item * | ||
| 239 | |||
| 240 | Calls another method on the same callee. | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | You might create such a method to extend or adapt your class' interface. | ||
| 245 | |||
| 246 | Sample declaration and usage: | ||
| 247 | |||
| 248 | package MyObject; | ||
| 249 | use Class::MakeMethods::Standard::Universal ( | ||
| 250 | alias => { name=>'click_here', target=>'complex_machinery' } | ||
| 251 | ); | ||
| 252 | sub complex_machinery { ... } | ||
| 253 | ... | ||
| 254 | |||
| 255 | $myobj->click_here(...); # calls $myobj->complex_machinery(...) | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | sub alias { | ||
| 260 | map { | ||
| 261 | my $method = $_; | ||
| 262 | $method->{name} => sub { | ||
| 263 | my $self = shift; | ||
| 264 | |||
| 265 | my $t_method = $method->{target} or confess("no target"); | ||
| 266 | my @t_args = $method->{target_args} ? @{$method->{target_args}} : (); | ||
| 267 | |||
| 268 | $self->$t_method(@t_args, @_); | ||
| 269 | } | ||
| 270 | } (shift)->_get_declarations(@_) | ||
| 271 | } | ||
| 272 | |||
| 273 | ######################################################################## | ||
| 274 | |||
| 275 | =head2 delegate - Use another object to provide method | ||
| 276 | |||
| 277 | For each method name passed, returns a subroutine with the following characteristics: | ||
| 278 | |||
| 279 | =over 4 | ||
| 280 | |||
| 281 | =item * | ||
| 282 | |||
| 283 | Calls a method on self to retrieve another object, and then calls a method on that object and returns its value. | ||
| 284 | |||
| 285 | =back | ||
| 286 | |||
| 287 | You might want to create and use such methods to faciliate composition of objects from smaller objects. | ||
| 288 | |||
| 289 | Sample declaration and usage: | ||
| 290 | |||
| 291 | package MyObject; | ||
| 292 | use Class::MakeMethods::Standard::Universal ( | ||
| 293 | 'Standard::Hash:object' => { name=>'instrument' }, | ||
| 294 | delegate => { name=>'play_music', target=>'instrument', method=>'play' } | ||
| 295 | ); | ||
| 296 | ... | ||
| 297 | |||
| 298 | my $object = MyObject->new(); | ||
| 299 | $object->instrument( MyInstrument->new ); | ||
| 300 | $object->play_music; | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | sub delegate { | ||
| 305 | map { | ||
| 306 | my $method = $_; | ||
| 307 | $method->{method} ||= $method->{name}; | ||
| 308 | $method->{name} => sub { | ||
| 309 | my $self = shift; | ||
| 310 | |||
| 311 | my $t_method = $method->{target} or confess("no target"); | ||
| 312 | my @t_args = $method->{target_args} ? @{$method->{target_args}} : (); | ||
| 313 | |||
| 314 | my $m_method = $method->{method} or confess("no method"); | ||
| 315 | my @m_args = $method->{method_args} ? @{$method->{method_args}} : (); | ||
| 316 | push @m_args, $self if ( $method->{target_args_self} ); | ||
| 317 | |||
| 318 | my $obj = $self->$t_method( @t_args ) | ||
| 319 | or croak("Can't delegate $method->{name} because $t_method is empty"); | ||
| 320 | |||
| 321 | $obj->$m_method(@m_args, @_); | ||
| 322 | } | ||
| 323 | } (shift)->_get_declarations(@_) | ||
| 324 | } | ||
| 325 | |||
| 326 | ######################################################################## | ||
| 327 | |||
| 328 | =head1 SEE ALSO | ||
| 329 | |||
| 330 | See L<Class::MakeMethods> for general information about this distribution. | ||
| 331 | |||
| 332 | See L<Class::MakeMethods::Standard> for more about this family of subclasses. | ||
| 333 | |||
| 334 | =cut | ||
| 335 | |||
| 336 | 1; |