File Coverage

lib/WRT/MethodSpit.pm
Criterion Covered Total %
statement 30 45 66.6
branch 2 6 33.3
condition n/a
subroutine 9 12 75.0
pod 1 5 20.0
total 42 68 61.7


line stmt bran cond sub pod time code
1             =pod
2              
3             =head1 NAME
4              
5             WRT::MethodSpit - quickie method generation
6              
7             =head1 SYNOPSIS
8              
9             =head1 DESCRIPTION
10              
11             The following bits are cheap method generation, in place of using
12             Class::Accessor or Object::Tiny.
13              
14             =cut
15              
16             package WRT::MethodSpit;
17              
18 3     3   17 use strict;
  3         5  
  3         63  
19 3     3   12 use warnings;
  3         5  
  3         70  
20 3     3   14 no warnings 'uninitialized';
  3         5  
  3         267  
21              
22             sub methodspit {
23 3     3 0 18 my ($class, @names) = @_;
24              
25             # These are simple accessors.
26 3         7 foreach my $name (@names) {
27 72         85 makemethod($class, $name);
28             }
29              
30 3         10 return;
31             }
32              
33             # Handy-dandy basic closure:
34             sub makemethod {
35 72     72 0 84 my ($class, $name) = @_;
36              
37 3     3   14 no strict 'refs';
  3         7  
  3         454  
38              
39             # Install a generated sub:
40 72         186 *{ "${class}::${name}" } =
41             sub {
42 539     539   910 my ($self, $param) = @_;
43 539 100       745 $self->{$name} = $param if defined $param;
44 539         1684 return $self->{$name};
45             }
46 72         148 }
47              
48             sub methodspit_depend {
49 0     0 0 0 my ($class, $dependency, $names) = @_;
50              
51 0         0 my %names = %{ $names };
  0         0  
52              
53 0         0 foreach my $name (keys %names) {
54 0         0 my $default = $names{$name};
55 0         0 makemethod_depend($class, $dependency, $name, $default);
56             }
57             }
58              
59             # A more complicated closure. Makes a return value dependent on another
60             # method, if not already explicitly defined.
61              
62             sub makemethod_depend {
63 0     0 0 0 my ($class, $dependency, $name, $default) = @_;
64              
65 3     3   14 no strict 'refs';
  3         7  
  3         464  
66              
67 0         0 *{ "${class}::${name}" } =
68             sub {
69 0     0   0 my ($self, $param) = @_;
70              
71 0 0       0 if (defined $param) {
72 0         0 $self->{$name} = $param;
73             }
74              
75 0 0       0 if (defined $self->{$name}) {
76 0         0 return $self->{$name};
77             } else {
78 0         0 return $self->$dependency . $default;
79             }
80             }
81 0         0 }
82              
83             =item configure(param => 'value')
84              
85             Set specified parameters.
86              
87             =cut
88              
89             sub configure {
90 3     3 1 7 my $self = shift;
91 3         11 my %params = @_;
92              
93 3         13 for my $p (keys %params) {
94 24         59 $self->{$p} = $params{$p};
95             }
96              
97 3         10 return;
98             }
99              
100             1;