| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Class::Methods | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Class mixin module for adding methods to a class. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | #======================================================================== | ||
| 12 | |||
| 13 | package Badger::Class::Methods; | ||
| 14 | |||
| 15 | use Carp; | ||
| 16 | use Badger::Class | ||
| 17 | version => 0.01, | ||
| 18 | debug => 0, | ||
| 19 | base => 'Badger::Base', | ||
| 20 | import => 'class BCLASS', | ||
| 21 | constants => 'DELIMITER ARRAY HASH PKG CODE', | ||
| 22 | utils => 'is_object', | ||
| 23 | exports => { | ||
| 24 | hooks => { | ||
| 25 | init => \&initialiser, | ||
| 26 | map { $_ => [\&generate, 1] } | ||
| 27 | qw( accessors mutators get set slots hash auto_can ) | ||
| 28 | }, | ||
| 29 | }, | ||
| 30 | messages => { | ||
| 31 | no_target => 'No target class specified to generate methods for', | ||
| 32 | no_type => 'No method type specified to generate', | ||
| 33 | no_methods => 'No %s specified to generate', | ||
| 34 | bad_method => 'Invalid %s method: %s', | ||
| 35 | bad_type => 'Invalid method generator specified: %s', | ||
| 36 | }; | ||
| 37 | |||
| 38 | # method aliases | ||
| 39 | *get = \&accessors; | ||
| 40 | *set = \&mutators; | ||
| 41 | |||
| 42 | our $AUTOLOAD; | ||
| 43 | |||
| 44 | sub generate { | ||
| 45 | my $class = shift; | ||
| 46 | my $target = shift | ||
| 47 | || return $class->error_msg('no_target'); | ||
| 48 | my $type = shift | ||
| 49 | || return $class->error_msg('no_type'); | ||
| 50 | my $methods = shift | ||
| 51 | || return $class->error_msg( no_methods => $type ); | ||
| 52 | my $code = $class->can($type) | ||
| 53 | || return $class->error_msg( bad_type => $type ); | ||
| 54 | |||
| 55 | $class->debug("generate($target, $type, $methods)") if DEBUG; | ||
| 56 | |||
| 57 | $code->($class, $target, $methods); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub accessors { | ||
| 61 | my ($class, $target, $methods) = shift->args(@_); | ||
| 62 | |||
| 63 | $target->import_symbol( | ||
| 64 | $_ => $class->accessor($_) | ||
| 65 | ) for @$methods; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub accessor { | ||
| 69 | my ($self, $name) = @_; | ||
| 70 | return sub { | ||
| 71 | $_[0]->{ $name }; | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub mutators { | ||
| 76 | my ($class, $target, $methods) = shift->args(@_); | ||
| 77 | |||
| 78 | $target->import_symbol( | ||
| 79 | $_ => $class->mutator($_) | ||
| 80 | ) for @$methods; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub mutator { | ||
| 84 | my ($self, $name) = @_; | ||
| 85 | return sub { | ||
| 86 | # You wouldn't ever want to write a real subroutine like this. | ||
| 87 | # But that's OK, because we're here to do it for you. You get | ||
| 88 | # the efficiency without having to ever look at code like this: | ||
| 89 | @_ == 2 | ||
| 90 | ? ($_[0]->{ $name } = $_[1]) | ||
| 91 | : $_[0]->{ $name }; | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub hash { | ||
| 96 | my ($class, $target, $methods) = shift->args(@_); | ||
| 97 | |||
| 98 | foreach (@$methods) { | ||
| 99 | my $name = $_; # new lexical var for closure | ||
| 100 | $target->import_symbol( | ||
| 101 | $name => sub { | ||
| 102 | # return hash ref when called without args | ||
| 103 | return $_[0]->{ $name } if @_ == 1; | ||
| 104 | |||
| 105 | # return hash item when called with one non-ref arg | ||
| 106 | return $_[0]->{ $name }->{ $_[1] } if @_ == 2 && ! ref $_[1]; | ||
| 107 | |||
| 108 | # add items to hash when called with hash ref or multiple args | ||
| 109 | my $self = shift; | ||
| 110 | my $items = @_ == 1 && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 111 | my $hash = $self->{ $name }; | ||
| 112 | @$hash{ keys %$items } = values %$items; | ||
| 113 | return $hash; | ||
| 114 | } | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub initialiser { | ||
| 120 | my ($class, $target, $methods) = shift->args(@_); | ||
| 121 | |||
| 122 | $target->import_symbol( | ||
| 123 | init => sub { | ||
| 124 | my ($self, $config) = @_; | ||
| 125 | $self->{ config } = $config; | ||
| 126 | foreach my $name (@$methods) { | ||
| 127 | $self->$name($config); | ||
| 128 | } | ||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub slots { | ||
| 135 | my ($class, $target, $methods) = shift->args(@_); | ||
| 136 | my $index = 0; | ||
| 137 | |||
| 138 | foreach my $method (@$methods) { | ||
| 139 | my $i = $index++; # new lexical var for closure | ||
| 140 | $target->import_symbol( | ||
| 141 | $method => sub { | ||
| 142 | return @_ > 1 | ||
| 143 | ? ($_[0]->[$i] = $_[1]) | ||
| 144 | : $_[0]->[$i]; | ||
| 145 | } | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub auto_can { | ||
| 151 | my ($class, $target, $methods) = shift->args(@_); | ||
| 152 | |||
| 153 | die "auto_can only support a single method at this time\n" | ||
| 154 | if @$methods != 1; | ||
| 155 | |||
| 156 | my $method = shift @$methods; | ||
| 157 | |||
| 158 | croak "Invalid auto_can method specified: $method\n" | ||
| 159 | if ref $method eq CODE; | ||
| 160 | |||
| 161 | # avoid runaways | ||
| 162 | my $seen = { }; | ||
| 163 | |||
| 164 | $class->debug("installing AUTOLOAD and can() in $target") if DEBUG; | ||
| 165 | |||
| 166 | $target->import_symbol( | ||
| 167 | can => sub { | ||
| 168 | my ($this, $name, @args) = @_; | ||
| 169 | $class->debug("looking to see if $this can $name()") if DEBUG; | ||
| 170 | |||
| 171 | # This avoids runaways where can() calls itself repeatedly, but | ||
| 172 | # doesn't prevent can() from being called several times for the | ||
| 173 | # same item. | ||
| 174 | return if $seen->{ $name }; | ||
| 175 | local $seen->{ $name } = 1; | ||
| 176 | |||
| 177 | return $this->SUPER::can($name) | ||
| 178 | || $this->$method($name, @args); | ||
| 179 | } | ||
| 180 | ); | ||
| 181 | |||
| 182 | $target->import_symbol( | ||
| 183 | AUTOLOAD => sub { | ||
| 184 | my ($this, @args) = @_; | ||
| 185 | my ($name) = ($AUTOLOAD =~ /([^:]+)$/ ); | ||
| 186 | return if $name eq 'DESTROY'; | ||
| 187 | if (my $method = $this->can($name, @args)) { | ||
| 188 | my $that = class($this); | ||
| 189 | $class->debug("$class installing $name method in $that") if DEBUG; | ||
| 190 | $that->method( $name => $method ); | ||
| 191 | return $method->($this, @args); | ||
| 192 | } | ||
| 193 | |||
| 194 | # Hmmm... what if $this isn't a subclass of Badger::Base? | ||
| 195 | return $this->error_msg( bad_method => $name, ref $this, (caller())[1,2] ); | ||
| 196 | } | ||
| 197 | ); | ||
| 198 | |||
| 199 | $class->debug("installed AUTOLOAD and can() in $target") if DEBUG; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub args { | ||
| 203 | my $class = shift; | ||
| 204 | my $target = shift; | ||
| 205 | my $methods = @_ == 1 ? shift : [ @_ ]; | ||
| 206 | |||
| 207 | # update $target to a Badger::Class object if not already one | ||
| 208 | $target = class($target) | ||
| 209 | unless is_object(BCLASS, $target); | ||
| 210 | |||
| 211 | # split text string into list ref of method names | ||
| 212 | $methods = [ split(DELIMITER, $methods) ] | ||
| 213 | unless ref $methods eq ARRAY; | ||
| 214 | |||
| 215 | return ($class, $target, $methods); | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | 1; | ||
| 221 | |||
| 222 | __END__ |