| 1 | # Copyright (c) 1998-2005 by Jonathan Swartz. All rights reserved. | ||||||||||
| 2 | # This program is free software; you can redistribute it and/or modify it | ||||||||||
| 3 | # under the same terms as Perl itself. | ||||||||||
| 4 | |||||||||||
| 5 | package HTML::Mason::MethodMaker; | ||||||||||
| 6 | 34 | $HTML::Mason::MethodMaker::VERSION = '1.60'; | |||||||||
| 7 | 34 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 34 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 34 | ▶ | use Params::Validate qw(validate_pos); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | sub import | ||||||||||
| 13 | 292 | ▶ | { | ||||||||
|
Subroutine
import: called CC=12 SCAR=25.2
| |||||||||||
| 14 | 292 | my $caller = caller; | |||||||||
| 15 | 292 | shift; # don't need class name | |||||||||
| 16 | 292 | my %p = @_; | |||||||||
| 17 | |||||||||||
| 18 | 292 | ▶ | if ($p{read_only}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | { | ||||||||||
| 20 | 254 | ▶ | foreach my $ro ( ref $p{read_only} ? @{ $p{read_only} } : $p{read_only} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | { | ||||||||||
| 22 | 34 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 2023 | ▶ | *{"$caller\::$ro"} = sub { return $_[0]->{$ro} }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 24 | } | ||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | # | ||||||||||
| 28 | # The slight weirdness to avoid calling shift in these rw subs is | ||||||||||
| 29 | # _intentional_. These subs get called a lot simply to read the | ||||||||||
| 30 | # value, and optimizing this common case actually does achieve | ||||||||||
| 31 | # something. | ||||||||||
| 32 | # | ||||||||||
| 33 | 292 | ▶ | if ($p{read_write}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | { | ||||||||||
| 35 | 135 | ▶ | foreach my $rw ( ref $p{read_write} ? @{ $p{read_write} } : $p{read_write} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | { | ||||||||||
| 37 | 453 | ▶ | if (ref $rw) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | { | ||||||||||
| 39 | 413 | my ($name, $spec) = @$rw; | |||||||||
| 40 | my $sub = | ||||||||||
| 41 | 5625 | ▶ | sub { if (@_ > 1) | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 42 | { | ||||||||||
| 43 | 67 | my $s = shift; | |||||||||
| 44 | 67 | validate_pos(@_, $spec); | |||||||||
| 45 | 66 | $s->{$name} = shift; | |||||||||
| 46 | 66 | return $s->{$name}; | |||||||||
| 47 | } | ||||||||||
| 48 | 5558 | return $_[0]->{$name}; | |||||||||
| 49 | 413 | }; | |||||||||
| 50 | 34 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 413 | *{"$caller\::$name"} = $sub | |||||||||
| 52 | } | ||||||||||
| 53 | else | ||||||||||
| 54 | { | ||||||||||
| 55 | my $sub = | ||||||||||
| 56 | 315 | ▶ | sub { if (@_ > 1) | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 57 | { | ||||||||||
| 58 | 243 | $_[0]->{$rw} = $_[1]; | |||||||||
| 59 | } | ||||||||||
| 60 | 315 | return $_[0]->{$rw}; | |||||||||
| 61 | 40 | }; | |||||||||
| 62 | 34 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 40 | *{"$caller\::$rw"} = $sub; | |||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 292 | ▶ | if ($p{read_write_contained}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | { | ||||||||||
| 70 | 32 | foreach my $object (keys %{ $p{read_write_contained} }) | |||||||||
| 71 | { | ||||||||||
| 72 | 32 | foreach my $rwc (@{ $p{read_write_contained}{$object} }) | |||||||||
| 73 | { | ||||||||||
| 74 | 288 | ▶ | if (ref $rwc) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | { | ||||||||||
| 76 | 288 | my ($name, $spec) = @$rwc; | |||||||||
| 77 | my $sub = | ||||||||||
| 78 | 411 | ▶ | sub { my $s = shift; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 79 | 411 | my %new; | |||||||||
| 80 | 411 | ▶ | if (@_) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | { | ||||||||||
| 82 | 411 | validate_pos(@_, $spec); | |||||||||
| 83 | 410 | %new = ( $name => $_[0] ); | |||||||||
| 84 | } | ||||||||||
| 85 | 410 | my %args = $s->delayed_object_params( $object, | |||||||||
| 86 | %new ); | ||||||||||
| 87 | 410 | return $args{$rwc}; | |||||||||
| 88 | 288 | }; | |||||||||
| 89 | 34 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 288 | *{"$caller\::$name"} = $sub; | |||||||||
| 91 | } | ||||||||||
| 92 | else | ||||||||||
| 93 | { | ||||||||||
| 94 | my $sub = | ||||||||||
| 95 | 0 | ▶ | sub { my $s = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 96 | 0 | ▶ | my %new = @_ ? ( $rwc => $_[0] ) : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | my %args = $s->delayed_object_params( $object, | |||||||||
| 98 | %new ); | ||||||||||
| 99 | 0 | return $args{$rwc}; | |||||||||
| 100 | 0 | }; | |||||||||
| 101 | 34 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 0 | *{"$caller\::$rwc"} = $sub; | |||||||||
| 103 | } | ||||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 34 | 1; | |||||||||
| 110 | |||||||||||
| 111 | =pod | ||||||||||
| 112 | |||||||||||
| 113 | =head1 NAME | ||||||||||
| 114 | |||||||||||
| 115 | HTML::Mason::MethodMaker - Used to create simple get & get/set methods in other classes | ||||||||||
| 116 | |||||||||||
| 117 | =head1 SYNOPSIS | ||||||||||
| 118 | |||||||||||
| 119 | use HTML::Mason::MethodMaker | ||||||||||
| 120 | ( read_only => 'foo', | ||||||||||
| 121 | read_write => [ | ||||||||||
| 122 | [ bar => { type => SCALAR } ], | ||||||||||
| 123 | [ baz => { isa => 'HTML::Mason::Baz' } ], | ||||||||||
| 124 | 'quux', # no validation | ||||||||||
| 125 | ], | ||||||||||
| 126 | read_write_contained => { other_object => | ||||||||||
| 127 | [ | ||||||||||
| 128 | [ 'thing1' => { isa => 'Thing1' } ], | ||||||||||
| 129 | 'thing2', # no validation | ||||||||||
| 130 | ] | ||||||||||
| 131 | }, | ||||||||||
| 132 | ); | ||||||||||
| 133 | |||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||
| 135 | |||||||||||
| 136 | This automates the creation of simple accessor methods. | ||||||||||
| 137 | |||||||||||
| 138 | =head1 USAGE | ||||||||||
| 139 | |||||||||||
| 140 | This module creates methods when it is C<use>'d by another module. | ||||||||||
| 141 | There are three types of methods: 'read_only', 'read_write', | ||||||||||
| 142 | 'read_write_contained'. | ||||||||||
| 143 | |||||||||||
| 144 | Attributes specified as 'read_only' get an accessor that only returns | ||||||||||
| 145 | the value of the attribute. Presumably, these attributes are set via | ||||||||||
| 146 | more complicated methods in the class or as a side effect of one of | ||||||||||
| 147 | its methods. | ||||||||||
| 148 | |||||||||||
| 149 | Attributes specified as 'read_write' will take a single optional | ||||||||||
| 150 | parameter. If given, this parameter will become the new value of the | ||||||||||
| 151 | attribute. This value is then returned from the method. If no | ||||||||||
| 152 | parameter is given, then the current value is returned. | ||||||||||
| 153 | |||||||||||
| 154 | If you want the accessor to use C<Params::Validate> to validate any | ||||||||||
| 155 | values passed to the accessor (and you _do_), then the the accessor | ||||||||||
| 156 | specification should be an array reference containing two elements. | ||||||||||
| 157 | The first element is the accessor name and the second is the | ||||||||||
| 158 | validation spec. | ||||||||||
| 159 | |||||||||||
| 160 | The 'read_write_contained' parameter is used to create accessor for | ||||||||||
| 161 | delayed contained objects. A I<delayed> contained object is one that | ||||||||||
| 162 | is B<not> created in the containing object's accessor, but rather at | ||||||||||
| 163 | some point after the containing object is constructed. For example, | ||||||||||
| 164 | the Interpreter object creates Request objects after the Interpreter | ||||||||||
| 165 | itself has been created. | ||||||||||
| 166 | |||||||||||
| 167 | The value of the 'read_write_contained' parameter should be a hash | ||||||||||
| 168 | reference. The keys are the internal name of the contained object, | ||||||||||
| 169 | such as "request" or "compiler". The values for the keys are the same | ||||||||||
| 170 | as the parameters given for 'read_write' accessors. | ||||||||||
| 171 | |||||||||||
| 172 | =cut | ||||||||||