File Coverage

blib/lib/Mixin/ExtraFields/Param.pm
Criterion Covered Total %
statement 42 43 97.6
branch 12 14 85.7
condition 4 6 66.6
subroutine 11 11 100.0
pod 4 4 100.0
total 73 78 93.5


line stmt bran cond sub pod time code
1 2     2   49139 use warnings;
  2         6  
  2         74  
2 2     2   19 use strict;
  2         4  
  2         138  
3             package Mixin::ExtraFields::Param;
4             # ABSTRACT: make your class provide a familiar "param" method
5             $Mixin::ExtraFields::Param::VERSION = '0.021';
6 2     2   1884 use Mixin::ExtraFields 0.002 ();
  2         37615  
  2         47  
7 2     2   1708 use parent qw(Mixin::ExtraFields);
  2         589  
  2         12  
8              
9 2     2   100 use Carp ();
  2         6  
  2         739  
10              
11             # =head1 SYNOPSIS
12             #
13             # package Widget::Parametric;
14             # use Mixin::ExtraFields::Param -fields => { driver => 'HashGuts' };;
15             #
16             # ...
17             #
18             # my $widget = Widget::Parametric->new({ flavor => 'vanilla' });
19             #
20             # printf "%s: %s\n", $_, $widget->param($_) for $widget->param;
21             #
22             # =head1 DESCRIPTION
23             #
24             # This module mixes in to your class to provide a C<param> method like the ones
25             # provided by L<CGI>, L<CGI::Application>, and other classes. It uses
26             # Mixin::ExtraFields, which means it can use any Mixin::ExtraFields driver to
27             # store your data.
28             #
29             # By default, the methods provided are:
30             #
31             # =for :list
32             # * param
33             # * exists_param
34             # * delete_param
35             #
36             # These methods are imported by the C<fields> group, which must be requested. If
37             # a C<moniker> argument is supplied, the moniker is used instead of "param". For
38             # more information, see L<Mixin::ExtraFields>.
39             #
40             # =cut
41              
42 2     2 1 3555 sub default_moniker { 'param' }
43              
44 3     3 1 4217 sub methods { qw(param exists delete) }
45              
46             sub method_name {
47 24     24 1 156 my ($self, $method, $moniker) = @_;
48              
49 24 100       103 return $moniker if $method eq 'param';
50 21         86 return $self->SUPER::method_name($method, $moniker);
51             }
52              
53             sub build_method {
54 9     9 1 75 my ($self, $method_name, $arg) = @_;
55              
56 9 100       24 return $self->_build_param_method($arg) if $method_name eq 'param';
57 6         27 return $self->SUPER::build_method($method_name, $arg);
58             }
59              
60             # =method param
61             #
62             # my @params = $object->param; # get names of existing params
63             #
64             # my $value = $object->param('name'); # get value of a param
65             #
66             # my $value = $object->param(name => $value); # set a param's value
67             #
68             # my @values = $object->param(n1 => $v1, n2 => $v2, ...); # set many values
69             #
70             # This method sets or retrieves parameters.
71             #
72             # =cut
73              
74             sub _build_param_method {
75 3     3   6 my ($self, $arg) = @_;
76              
77 3         7 my $id_method = $arg->{id_method};
78 3         5 my $driver = $arg->{driver};
79              
80 3         17 my $names_method = $self->driver_method_name('get_all_names');
81 3         42 my $get_method = $self->driver_method_name('get');
82 3         25 my $set_method = $self->driver_method_name('set');
83              
84             sub {
85 20     20   11062 my $self = shift;
86 20         61 my $id = $self->$$id_method;
87              
88             # If called as ->param, return all names.
89 20 100       154 return $$driver->$names_method($self, $id) unless @_;
90              
91             # If given a hashref, as first arg, operate on its contents. In the
92             # future, we might want to complain if we get a hashref /and/ further
93             # arguments.
94 11 50 66     56 @_ = %{$_[0]} if @_ == 1 and ref $_[0] eq 'HASH';
  0         0  
95              
96 11 50 66     50 Carp::croak "invalid call to param: odd, non-one number of params"
97             if @_ > 1 and @_ % 2 == 1;
98              
99             # If called as ->param($name), return the value
100 11 100       49 return $$driver->$get_method($self, $id, $_[0]) if @_ == 1;
101              
102             # Otherwise we're doing... BULK ASSIGNMENT!
103 4         6 my @assigned;
104 4         12 while (@_) {
105             # We don't put @_ into a hash because we guarantee processing (and more
106             # importantly return) order. -- rjbs, 2006-03-14
107 5         14 my ($key, $value) = splice @_, 0, 2;
108 5         22 $$driver->$set_method($self, $id, $key => $value);
109 5         76 push @assigned, $value;
110             }
111 4 100       21 return wantarray ? @assigned : $assigned[0];
112 3         41 };
113             }
114              
115             1;
116              
117             __END__
118              
119             =pod
120              
121             =encoding UTF-8
122              
123             =head1 NAME
124              
125             Mixin::ExtraFields::Param - make your class provide a familiar "param" method
126              
127             =head1 VERSION
128              
129             version 0.021
130              
131             =head1 SYNOPSIS
132              
133             package Widget::Parametric;
134             use Mixin::ExtraFields::Param -fields => { driver => 'HashGuts' };;
135              
136             ...
137              
138             my $widget = Widget::Parametric->new({ flavor => 'vanilla' });
139              
140             printf "%s: %s\n", $_, $widget->param($_) for $widget->param;
141              
142             =head1 DESCRIPTION
143              
144             This module mixes in to your class to provide a C<param> method like the ones
145             provided by L<CGI>, L<CGI::Application>, and other classes. It uses
146             Mixin::ExtraFields, which means it can use any Mixin::ExtraFields driver to
147             store your data.
148              
149             By default, the methods provided are:
150              
151             =over 4
152              
153             =item *
154              
155             param
156              
157             =item *
158              
159             exists_param
160              
161             =item *
162              
163             delete_param
164              
165             =back
166              
167             These methods are imported by the C<fields> group, which must be requested. If
168             a C<moniker> argument is supplied, the moniker is used instead of "param". For
169             more information, see L<Mixin::ExtraFields>.
170              
171             =head1 METHODS
172              
173             =head2 param
174              
175             my @params = $object->param; # get names of existing params
176              
177             my $value = $object->param('name'); # get value of a param
178              
179             my $value = $object->param(name => $value); # set a param's value
180              
181             my @values = $object->param(n1 => $v1, n2 => $v2, ...); # set many values
182              
183             This method sets or retrieves parameters.
184              
185             =head1 AUTHOR
186              
187             Ricardo SIGNES <rjbs@cpan.org>
188              
189             =head1 COPYRIGHT AND LICENSE
190              
191             This software is copyright (c) 2005 by Ricardo SIGNES.
192              
193             This is free software; you can redistribute it and/or modify it under
194             the same terms as the Perl 5 programming language system itself.
195              
196             =cut