NAME

Rose::Object::MakeMethods - A simple method maker base class.

SYNOPSIS

package 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;
...

DESCRIPTION

Rose::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

allow_apparent_reload [BOOL]

Get or set an attribute that determines whether or not to allow an attempt to re-make the same method using the same class that made it earlier. The default is true.

This issue comes up when a module is forcibly reloaded, e.g., by Apache::Reload or Apache::StatINC. When this happens, all the "make methods" actions will be attempted again. In the absence of the preserve_existing or override_existing options, the allow_apparent_reload attribute will be consulted. If it's true, and if it appears that the method in question was made by this method-maker class, then it behaves as if the preserve_existing option had been passed. If it is false, then a fatal "method redefined" error will occur.

import SPEC

The import class method is mean to be called implicitly as a result of a use statement. For example:

use Rose::Object::MakeMethods::Generic
(
  SPEC
);

is roughly equivalent to:

require Rose::Object::MakeMethods::Generic;
Rose::Object::MakeMethods::Generic->import(SPEC);

where SPEC is a series of specifications for the methods to be created. (But don't call import explicitly; use make_methods instead.)

In response to each method specification, one or more methods are created.

The first part of the SPEC argument is an optional hash reference whose contents are intended to modify the behavior of the method maker class itself, rather than the individual methods being made. There are currently only two valid arguments for this hash:

target_class CLASS

Specifies that class that the methods will be added to. Defaults to the class from which the call was made. For example, this:

use Rose::Object::MakeMethods::Generic
(
  { target_class => 'Foo' },
  ...
);

Is equivalent to this:

package Foo;

use Rose::Object::MakeMethods::Generic
(
  ...
);

In general, the target_class argument is omitted since methods are usually indented to end up in the class of the caller.

override_existing BOOL

By default, attempting to create a method that already exists will result in a fatal error. But if the override_existing option is set to a true value, the existing method will be replaced with the generated method.

preserve_existing BOOL

By default, attempting to create a method that already exists will result in a fatal error. But if the preserve_existing option is set to a true value, the existing method will be left unmodified. This option takes precedence over the override_existing option.

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:

  • The Method Type

    This is the name of the subroutine that will be called in order to generated the methods (see SUBCLASSING for more information). It describes the nature of the generated method. For example, "scalar", "array", "bitfield", "object"

  • Method Type Arguments

    Name/value pairs that are passed to the method maker of the specified type in order to modify its behavior.

  • Method Names

    One or more names for the methods that are to be created. Note that a method maker of a particular type may choose to modify or ignore these names. In the common case, for each method name argument, a single method is created with the same name as the method name argument.

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.

make_methods SPEC

This method is equivalent to the import method, but makes the intent of the code clearer when it is called explicitly. (The import method is only meant to be called implicitly by use.)

SUBCLASSING

In 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 class of the method maker as a string. This argument is usually ignored unless you are going to call some other class method.

  • The method name. In the common case, a single method with this name is defined, but you are free to do whatever you want with it, including ignoring it.

  • A reference to a hash containing the options for the method.

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.

AUTHOR

John C. Siracusa (siracusa@gmail.com)

LICENSE

Copyright (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.