| 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 | 70 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 70 | 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 | 70 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # method aliases | ||||||||||||||||||||||||||||
| 39 | 70 | *get = \&accessors; | |||||||||||||||||||||||||||
| 40 | 70 | *set = \&mutators; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 70 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub generate { | ||||||||||||||||||||||||||||
| 45 | 8 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 8 | ▶ | my $target = shift | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return $class->error_msg('no_target'))
MC/DC0%0/1
shift() || (return $class->error_msg('no_target'))
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 47 | || return $class->error_msg('no_target'); | ||||||||||||||||||||||||||||
| 48 | 8 | ▶ | my $type = shift | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return $class->error_msg('no_type'))
MC/DC0%0/1
shift() || (return $class->error_msg('no_type'))
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 49 | || return $class->error_msg('no_type'); | ||||||||||||||||||||||||||||
| 50 | 8 | ▶ | my $methods = shift | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return $class->error_msg('no_methods', $type))
MC/DC0%0/1
shift() || (return $class->error_msg('no_methods', $type))
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 51 | || return $class->error_msg( no_methods => $type ); | ||||||||||||||||||||||||||||
| 52 | 8 | ▶ | my $code = $class->can($type) | ||||||||||||||||||||||||||
|
Condition50%1/2
$class->can($type) || (return $class->error_msg('bad_type', $type))
MC/DC0%0/1
$class->can($type) || (return $class->error_msg('bad_type', $type))
$class->can($type)
Truth table50%1/2
A
A= $class->can($type)
| |||||||||||||||||||||||||||||
| 53 | || return $class->error_msg( bad_type => $type ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 8 | $class->debug("generate($target, $type, $methods)") if DEBUG; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 8 | $code->($class, $target, $methods); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub accessors { | ||||||||||||||||||||||||||||
| 61 | 258 | ▶ | my ($class, $target, $methods) = shift->args(@_); | ||||||||||||||||||||||||||
|
Subroutine
accessors: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | $target->import_symbol( | ||||||||||||||||||||||||||||
| 64 | $_ => $class->accessor($_) | ||||||||||||||||||||||||||||
| 65 | 258 | ) for @$methods; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub accessor { | ||||||||||||||||||||||||||||
| 69 | 516 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
accessor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | return sub { | ||||||||||||||||||||||||||||
| 71 | 677 | ▶ | $_[0]->{ $name }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 516 | }; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub mutators { | ||||||||||||||||||||||||||||
| 76 | 87 | ▶ | my ($class, $target, $methods) = shift->args(@_); | ||||||||||||||||||||||||||
|
Subroutine
mutators: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | $target->import_symbol( | ||||||||||||||||||||||||||||
| 79 | $_ => $class->mutator($_) | ||||||||||||||||||||||||||||
| 80 | 87 | ) for @$methods; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub mutator { | ||||||||||||||||||||||||||||
| 84 | 92 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
mutator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | 55 | ▶ | : $_[0]->{ $name }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 92 | }; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub hash { | ||||||||||||||||||||||||||||
| 96 | 1 | ▶ | my ($class, $target, $methods) = shift->args(@_); | ||||||||||||||||||||||||||
|
Subroutine
hash: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | foreach (@$methods) { | |||||||||||||||||||||||||||
| 99 | 1 | my $name = $_; # new lexical var for closure | |||||||||||||||||||||||||||
| 100 | $target->import_symbol( | ||||||||||||||||||||||||||||
| 101 | $name => sub { | ||||||||||||||||||||||||||||
| 102 | # return hash ref when called without args | ||||||||||||||||||||||||||||
| 103 | 8 | ▶ | return $_[0]->{ $name } if @_ == 1; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # return hash item when called with one non-ref arg | ||||||||||||||||||||||||||||
| 106 | 7 | ▶ | return $_[0]->{ $name }->{ $_[1] } if @_ == 2 && ! ref $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 2 and not ref $_[1]
MC/DC100%2/2
@_ == 2 and not ref $_[1]
@_ == 2
not ref $_[1]
Truth table100%3/3
A and not(B)
A= @_ == 2B= not ref $_[1]
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # add items to hash when called with hash ref or multiple args | ||||||||||||||||||||||||||||
| 109 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 2 | ▶ | my $items = @_ == 1 && ref $_[0] eq HASH ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 111 | 2 | my $hash = $self->{ $name }; | |||||||||||||||||||||||||||
| 112 | 2 | @$hash{ keys %$items } = values %$items; | |||||||||||||||||||||||||||
| 113 | 2 | return $hash; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 1 | ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub initialiser { | ||||||||||||||||||||||||||||
| 120 | 3 | ▶ | my ($class, $target, $methods) = shift->args(@_); | ||||||||||||||||||||||||||
|
Subroutine
initialiser: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | $target->import_symbol( | ||||||||||||||||||||||||||||
| 123 | init => sub { | ||||||||||||||||||||||||||||
| 124 | 3 | ▶ | my ($self, $config) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 125 | 3 | $self->{ config } = $config; | |||||||||||||||||||||||||||
| 126 | 3 | foreach my $name (@$methods) { | |||||||||||||||||||||||||||
| 127 | 3 | $self->$name($config); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 3 | return $self; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 3 | ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub slots { | ||||||||||||||||||||||||||||
| 135 | 3 | ▶ | my ($class, $target, $methods) = shift->args(@_); | ||||||||||||||||||||||||||
|
Subroutine
slots: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 3 | my $index = 0; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 3 | foreach my $method (@$methods) { | |||||||||||||||||||||||||||
| 139 | 9 | my $i = $index++; # new lexical var for closure | |||||||||||||||||||||||||||
| 140 | $target->import_symbol( | ||||||||||||||||||||||||||||
| 141 | $method => sub { | ||||||||||||||||||||||||||||
| 142 | 9 | ▶ | return @_ > 1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | ? ($_[0]->[$i] = $_[1]) | ||||||||||||||||||||||||||||
| 144 | : $_[0]->[$i]; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 9 | ); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | sub auto_can { | ||||||||||||||||||||||||||||
| 151 | 143 | ▶ | my ($class, $target, $methods) = shift->args(@_); | ||||||||||||||||||||||||||
|
Subroutine
auto_can: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 143 | ▶ | die "auto_can only support a single method at this time\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | if @$methods != 1; | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 143 | my $method = shift @$methods; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 143 | ▶ | croak "Invalid auto_can method specified: $method\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | if ref $method eq CODE; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # avoid runaways | ||||||||||||||||||||||||||||
| 162 | 143 | my $seen = { }; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 143 | $class->debug("installing AUTOLOAD and can() in $target") if DEBUG; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | $target->import_symbol( | ||||||||||||||||||||||||||||
| 167 | can => sub { | ||||||||||||||||||||||||||||
| 168 | 18 | ▶ | my ($this, $name, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 169 | 18 | $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 | 18 | ▶ | return if $seen->{ $name }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 18 | local $seen->{ $name } = 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 18 | ▶ | return $this->SUPER::can($name) | ||||||||||||||||||||||||||
|
Condition100%3/3
$this->SUPER::can($name) || $this->$method($name, @args)
MC/DC100%2/2
$this->SUPER::can($name) || $this->$method($name, @args)
$this->SUPER::can($name)
$this->$method($name, @args)
Truth table100%3/3
A || B
A= $this->SUPER::can($name)B= $this->$method($name, @args)
| |||||||||||||||||||||||||||||
| 178 | || $this->$method($name, @args); | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 143 | ); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | $target->import_symbol( | ||||||||||||||||||||||||||||
| 183 | AUTOLOAD => sub { | ||||||||||||||||||||||||||||
| 184 | 20 | ▶ | my ($this, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 185 | 20 | my ($name) = ($AUTOLOAD =~ /([^:]+)$/ ); | |||||||||||||||||||||||||||
| 186 | 20 | ▶ | return if $name eq 'DESTROY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 10 | ▶ | if (my $method = $this->can($name, @args)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 7 | my $that = class($this); | |||||||||||||||||||||||||||
| 189 | 7 | $class->debug("$class installing $name method in $that") if DEBUG; | |||||||||||||||||||||||||||
| 190 | 7 | $that->method( $name => $method ); | |||||||||||||||||||||||||||
| 191 | 7 | return $method->($this, @args); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # Hmmm... what if $this isn't a subclass of Badger::Base? | ||||||||||||||||||||||||||||
| 195 | 3 | return $this->error_msg( bad_method => $name, ref $this, (caller())[1,2] ); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 143 | ); | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 143 | $class->debug("installed AUTOLOAD and can() in $target") if DEBUG; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub args { | ||||||||||||||||||||||||||||
| 203 | 495 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
args: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | 495 | my $target = shift; | |||||||||||||||||||||||||||
| 205 | 495 | ▶ | my $methods = @_ == 1 ? shift : [ @_ ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # update $target to a Badger::Class object if not already one | ||||||||||||||||||||||||||||
| 208 | 495 | ▶ | $target = class($target) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | unless is_object(BCLASS, $target); | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # split text string into list ref of method names | ||||||||||||||||||||||||||||
| 212 | 495 | ▶ | $methods = [ split(DELIMITER, $methods) ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | unless ref $methods eq ARRAY; | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 495 | return ($class, $target, $methods); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 70 | 1; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | __END__ | ||||||||||||||||||||||||||||