|
NAMERose::Object::MakeMethods - A simple method maker base class.SYNOPSISpackage MyMethodMaker; use Rose::Object::MakeMethods; our @ISA = qw(Rose::Object::MakeMethods); sub widget { my($class, $name, $args) = @_; my $key = $args->{'hash_key'} || $name; my $interface = $args->{'interface'} || 'get_set'; my %methods; if($interface =~ /^get_set/) { $methods{$name} = sub { my($self) = shift; if(@_) { ... } ... return $self->{$key}; }; } if($interface eq 'get_set_delete') { $methods{"delete_$name"} = sub { ... }; ) return \%methods; } ... package MyObject; sub new { ... } use MyMethodMaker ( 'widget --get_set_delete' => 'foo', 'widget' => [ 'bar', 'baz', ] ); ... $o = MyObject->new; $o->foo($bar); $o->delete_foo(); print $o->bar . $o->baz; ... DESCRIPTIONRose::Object::MakeMethods is the base class for a family of method makers. A method maker is a module that's used to define methods in other packages. The actual method makers are subclasses of Rose::Object::MakeMethods that define the names and options of the different kinds of methods that they can make.There are method makers that make both object methods and class methods. The object method makers are in the "Rose::Object::MakeMethods::*" namespace. The class method makers are in the "Rose::Class::MakeMethods::*" namespace for the sake of clarity, but still inherit from Class::MethodMaker and therefore share the same method making interface. Several useful method makers are included under the "Rose::Object::MakeMethods::*" and "Rose::Class::MakeMethods::*" namespaces, mostly for use by other "Rose::*" objects and classes. This family of modules is not as powerful or flexible as the one that inspired it: Class::MethodMaker. I found that I was only using a tiny corner of the functionality provided by Class::MethodMaker, so I wrote this as a simple, smaller replacement. The fact that many "Rose::*" modules use Rose::Object::MakeMethods subclasses to make their methods should be considered an implementation detail that can change at any time. CLASS METHODS
After the optional hash reference full off options intended for the method maker class itself, a series of method specifications should be provided. Each method specification defines one or more named methods. The components of such a specification are:
Given the method type "bitfield" and the method arguments "opt1" and "opt2", the following examples show all valid forms for method specifications, with equivalent forms grouped together. Create a bitfield method named "my_bits": bitfield => 'my_bits' bitfield => [ 'my_bits' ], bitfield => [ 'my_bits' => {} ], Create a bitfield method named "my_bits", passing the "opt1" argument with a value of 2. 'bitfield --opt1=2' => 'my_bits' 'bitfield --opt1=2' => [ 'my_bits' ] bitfield => [ 'my_bits' => { opt1 => 2 } ] Create a bitfield method named "my_bits", passing the "opt1" argument with a value of 2 and the "opt2" argument with a value of 7. 'bitfield --opt1=2 --opt2=7' => 'my_bits' 'bitfield --opt1=2 --opt2=7' => [ 'my_bits' ] bitfield => [ 'my_bits' => { opt1 => 2, opt2 => 7 } ] 'bitfield --opt2=7' => [ 'my_bits' => { opt1 => 2 } ] In the case of a conflict between the options specified with the "--name=value" syntax and those provided in the hash reference, the ones in the hash reference take precedence. For example, these are equivalent: 'bitfield --opt1=99' => 'my_bits' 'bitfield --opt1=5' => [ 'my_bits' => { opt1 => 99 } ] If no value is provided for the first option, and if it is specified using the "--name" syntax, then it is taken as the value of the "interface" option. That is, this: 'bitfield --foobar' => 'my_bits' is equivalent to these: 'bitfield --interface=foobar' => 'my_bits' bitfield => [ my_bits => { interface => 'foobar' } ] This shortcut supports the convention that the "interface" option is used to decide which set of methods to create. But it's just a convention; the "interface" option is no different from any of the other options when it is eventually passed to the method maker of a given type. Any option other than the very first that is specified using the "--name" form and that lacks an explicit value is simply set to 1. That is, this: 'bitfield --foobar --baz' => 'my_bits' is equivalent to these: 'bitfield --interface=foobar --baz=1' => 'my_bits' bitfield => [ my_bits => { interface => 'foobar', baz => 1 } ] Multiple method names can be specified simultaneously for a given method type and set of options. For example, to create methods named "my_bits[1-3]", all of the same type and with the same options, any of these would work: 'bitfield --opt1=2' => [ 'my_bits1', 'my_bits2', 'my_bits3', ] bitfield => [ 'my_bits1' => { opt1 => 2 }, 'my_bits2' => { opt1 => 2 }, 'my_bits3' => { opt1 => 2 }, ] When options are provided using the "--name=value" format, they apply to all methods listed inside the array reference, unless overridden. Here's an example of an override: 'bitfield --opt1=2' => [ 'my_bits1', 'my_bits2', 'my_bits3' => { opt1 => 999 }, ] In this case, "my_bits1" and "my_bits2" use "opt1" values of 2, but "my_bits3" uses an "opt1" value of 999. Also note that it's okay to mix bare method names ("my_bits1" and "my_bits2") with method names that have associated hash reference options ("my_bits3"), all inside the same array reference. Finally, putting it all together, here's a full example using several different formats. use Rose::Object::MakeMethods::Generic ( { override_existing => 1 }, 'bitfield' => [ qw(my_bits other_bits) ], 'bitfield --opt1=5' => [ 'a', 'b', ], 'bitfield' => [ 'c', 'd' => { opt2 => 7 }, 'e' => { opt1 => 1 }, 'f' => { }, # empty is okay too ] ); In the documentation for the various Rose::Object::MakeMethods subclasses, any of the valid forms may be used in the examples.
SUBCLASSINGIn order to make a Rose::Object::MakeMethods subclass that can actually make some methods, simply subclass Rose::Object::MakeMethods and define one subroutine for each method type you want to support.The subroutine will be passed three arguments when it is called:
The subroutine is expected to return a reference to a hash containing name/code reference pairs. Note that the subroutine does not actually install the methods. It simple returns the name of each method that is to be installed, along with references to the closures that contain the code for those methods. This subroutine is called for each name in the method specifier. For example, this would result in three separate calls to the "bitfield" subroutine of the "MyMethodMaker" class: use MyMethodMaker ( bitfield => [ 'my_bits', 'your_bits' => { size => 32 }, 'other_bits' => { size => 128 }, ] ); So why not have the subroutine return a single code reference rather than a reference to a hash of name.code reference pairs? There are two reasons. First, remember that the name argument ("my_bits", "your_bits", "other_bits") may be modified or ignored by the method maker. The actual names of the methods created are determined by the keys of the hash reference returned by the subroutine. Second, a single call with a single method name argument may result in the creation more than one method--usually a "family" of methods. For example: package MyObject; use MyMethodMaker ( # creates add_book(), delete_book(), and books() methods 'hash --manip' => 'book', ); ... $o = MyObject->new(...); $o->add_book($book); print join("\n", map { $_->title } $o->books); $o->delete_book($book); Here, the "hash" method type elected to create three methods by prepending "add_" and "delete_" and appending "s" to the supplied method name argument, "book". Anything not specified in this documentation is simply a matter of convention. For example, the Rose::Object::MakeMethods subclasses all use a common set of method options: "hash_key", "interface", etc. As you read their documentation, this will become apparent. Finally, here's an example of a subclass that makes scalar accessors: package Rose::Object::MakeMethods::Generic; use strict; use Carp(); use Rose::Object::MakeMethods; our @ISA = qw(Rose::Object::MakeMethods); sub scalar { my($class, $name, $args) = @_; my %methods; my $key = $args->{'hash_key'} || $name; my $interface = $args->{'interface'} || 'get_set'; if($interface eq 'get_set_init') { my $init_method = $args->{'init_method'} || "init_$name"; $methods{$name} = sub { return $_[0]->{$key} = $_[1] if(@_ > 1); return defined $_[0]->{$key} ? $_[0]->{$key} : ($_[0]->{$key} = $_[0]->$init_method()); } } elsif($interface eq 'get_set') { $methods{$name} = sub { return $_[0]->{$key} = $_[1] if(@_ > 1); return $_[0]->{$key}; } } else { Carp::croak "Unknown interface: $interface" } return \%methods; } It can be used like this: package MyObject; use Rose::Object::MakeMethods::Generic ( scalar => [ 'power', 'error', ], 'scalar --get_set_init' => 'name', ); sub init_name { 'Fred' } ... $o = MyObject->new(power => 5); print $o->name; # Fred $o->power(99) or die $o->error; This is actually a subset of the code in the actual Rose::Object::MakeMethods::Generic module. See the rest of the "Rose::Object::MakeMethods::*" and "Rose::Class::MakeMethods::*" modules for more examples. AUTHORJohn C. Siracusa (siracusa@gmail.com)LICENSECopyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |