| 1 | package Mixin::ExtraFields::Param 0.022; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: make your class provide a familiar "param" method | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Mixin::ExtraFields 0.002 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use parent qw(Mixin::ExtraFields); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||||||||||
| 14 | #pod package Widget::Parametric; | ||||||||||||||||||||||||||||
| 15 | #pod use Mixin::ExtraFields::Param -fields => { driver => 'HashGuts' };; | ||||||||||||||||||||||||||||
| 16 | #pod | ||||||||||||||||||||||||||||
| 17 | #pod ... | ||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||
| 19 | #pod my $widget = Widget::Parametric->new({ flavor => 'vanilla' }); | ||||||||||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||||||||||
| 21 | #pod printf "%s: %s\n", $_, $widget->param($_) for $widget->param; | ||||||||||||||||||||||||||||
| 22 | #pod | ||||||||||||||||||||||||||||
| 23 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||
| 25 | #pod This module mixes in to your class to provide a C<param> method like the ones | ||||||||||||||||||||||||||||
| 26 | #pod provided by L<CGI>, L<CGI::Application>, and other classes. It uses | ||||||||||||||||||||||||||||
| 27 | #pod Mixin::ExtraFields, which means it can use any Mixin::ExtraFields driver to | ||||||||||||||||||||||||||||
| 28 | #pod store your data. | ||||||||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||||||||
| 30 | #pod By default, the methods provided are: | ||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||
| 32 | #pod =for :list | ||||||||||||||||||||||||||||
| 33 | #pod * param | ||||||||||||||||||||||||||||
| 34 | #pod * exists_param | ||||||||||||||||||||||||||||
| 35 | #pod * delete_param | ||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||
| 37 | #pod These methods are imported by the C<fields> group, which must be requested. If | ||||||||||||||||||||||||||||
| 38 | #pod a C<moniker> argument is supplied, the moniker is used instead of "param". For | ||||||||||||||||||||||||||||
| 39 | #pod more information, see L<Mixin::ExtraFields>. | ||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||
| 41 | #pod =cut | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sub default_moniker { 'param' } | ||||||||||||||||||||||||||
|
Subroutine
default_moniker: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | sub methods { qw(param exists delete) } | ||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 24 | ▶ | sub method_name { | ||||||||||||||||||||||||||
|
Subroutine
method_name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 24 | my ($self, $method, $moniker) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 24 | ▶ | return $moniker if $method eq 'param'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 21 | return $self->SUPER::method_name($method, $moniker); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 9 | ▶ | sub build_method { | ||||||||||||||||||||||||||
|
Subroutine
build_method: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 9 | my ($self, $method_name, $arg) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 9 | ▶ | return $self->_build_param_method($arg) if $method_name eq 'param'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 6 | return $self->SUPER::build_method($method_name, $arg); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | #pod =method param | ||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||
| 63 | #pod my @params = $object->param; # get names of existing params | ||||||||||||||||||||||||||||
| 64 | #pod | ||||||||||||||||||||||||||||
| 65 | #pod my $value = $object->param('name'); # get value of a param | ||||||||||||||||||||||||||||
| 66 | #pod | ||||||||||||||||||||||||||||
| 67 | #pod my $value = $object->param(name => $value); # set a param's value | ||||||||||||||||||||||||||||
| 68 | #pod | ||||||||||||||||||||||||||||
| 69 | #pod my @values = $object->param(n1 => $v1, n2 => $v2, ...); # set many values | ||||||||||||||||||||||||||||
| 70 | #pod | ||||||||||||||||||||||||||||
| 71 | #pod This method sets or retrieves parameters. | ||||||||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||||||||
| 73 | #pod =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | ▶ | sub _build_param_method { | ||||||||||||||||||||||||||
|
Subroutine
_build_param_method: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 3 | my ($self, $arg) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 3 | my $id_method = $arg->{id_method}; | |||||||||||||||||||||||||||
| 79 | 3 | my $driver = $arg->{driver}; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | my $names_method = $self->driver_method_name('get_all_names'); | |||||||||||||||||||||||||||
| 82 | 3 | my $get_method = $self->driver_method_name('get'); | |||||||||||||||||||||||||||
| 83 | 3 | my $set_method = $self->driver_method_name('set'); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 20 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||
| 86 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 20 | my $id = $self->$$id_method; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # If called as ->param, return all names. | ||||||||||||||||||||||||||||
| 90 | 20 | ▶ | return $$driver->$names_method($self, $id) unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # If given a hashref, as first arg, operate on its contents. In the | ||||||||||||||||||||||||||||
| 93 | # future, we might want to complain if we get a hashref /and/ further | ||||||||||||||||||||||||||||
| 94 | # arguments. | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | @_ = %{$_[0]} if @_ == 1 and ref $_[0] eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC0%0/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"
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 11 | ▶ | Carp::croak "invalid call to param: odd, non-one number of params" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ > 1 and @_ % 2 == 1
MC/DC0%0/2
@_ > 1 and @_ % 2 == 1
@_ > 1
@_ % 2 == 1
Truth table66%2/3
A and B
A= @_ > 1B= @_ % 2 == 1
| |||||||||||||||||||||||||||||
| 98 | if @_ > 1 and @_ % 2 == 1; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # If called as ->param($name), return the value | ||||||||||||||||||||||||||||
| 101 | 11 | ▶ | return $$driver->$get_method($self, $id, $_[0]) if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Otherwise we're doing... BULK ASSIGNMENT! | ||||||||||||||||||||||||||||
| 104 | 4 | my @assigned; | |||||||||||||||||||||||||||
| 105 | 4 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | # We don't put @_ into a hash because we guarantee processing (and more | ||||||||||||||||||||||||||||
| 107 | # importantly return) order. -- rjbs, 2006-03-14 | ||||||||||||||||||||||||||||
| 108 | 5 | my ($key, $value) = splice @_, 0, 2; | |||||||||||||||||||||||||||
| 109 | 5 | $$driver->$set_method($self, $id, $key => $value); | |||||||||||||||||||||||||||
| 110 | 5 | push @assigned, $value; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 4 | ▶ | return wantarray ? @assigned : $assigned[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 3 | }; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||