File Coverage

blib/lib/Brickyard/Accessor.pm
Criterion Covered Total %
statement 39 39 100.0
branch 6 8 75.0
condition n/a
subroutine 11 11 100.0
pod n/a
total 56 58 96.5


line stmt bran cond sub pod time code
1             package Brickyard::Accessor;
2              
3 4     4   75 use 5.010;
  4         16  
  4         191  
4 4     4   25 use warnings;
  4         8  
  4         134  
5 4     4   29 use strict;
  4         7  
  4         946  
6              
7             sub import {
8 20     20   229402 shift;
9 20         77 my %args = @_;
10 20         58 my $pkg = caller(0);
11 20         115 my %key_ctor = (rw => \&_mk_accessors);
12 20         90 for my $key (sort keys %key_ctor) {
13 20 50       79 next unless $args{$key};
14 20 50       77 die "value of the '$key' parameter should be an arrayref"
15             unless ref $args{$key} eq 'ARRAY';
16 20         33 $key_ctor{$key}->($pkg, @{ $args{$key} });
  20         111  
17             }
18 20 100       90 _mk_new($pkg) if $args{new};
19 20         5169 1;
20             }
21              
22             sub _mk_new {
23 9     9   21 my $pkg = shift;
24 4     4   33 no strict 'refs';
  4         8  
  4         689  
25 9         60 *{"${pkg}::new"} = sub {
26 21     21   69 my $class = shift;
27 21         259 bless {@_}, $class;
28 9         36 };
29             }
30              
31             sub _mk_accessors {
32 20     20   32 my $pkg = shift;
33 20         50 for my $n (@_) {
34 4     4   24 no strict 'refs';
  4         9  
  4         684  
35 39         94 *{"${pkg}::${n}"} = __make_rw($n);
  39         298  
36             }
37             }
38              
39             sub __make_rw {
40 39     39   63 my $n = shift;
41             sub {
42 95 100   95   746 $_[0]->{$n} = $_[1] if @_ == 2;
43 95         482 $_[0]->{$n};
44 39         376 };
45             }
46             1;
47              
48             =head1 NAME
49              
50             Brickyard::Accessor - Accessor generator for Brickyard classes
51              
52             =head1 SYNOPSIS
53              
54             package MyPackage;
55              
56             use Brickyard::Accessor (
57             new => 1,
58             rw => [ qw(foo bar) ]
59             );
60              
61             =head1 DESCRIPTION
62              
63             This module is based on L, adapted to suit the needs of
64             L.
65              
66             =head1 THE USE STATEMENT
67              
68             The use statement (i.e. the C function) of the module takes a single
69             hash as an argument that specifies the types and the names of the properties.
70             It recognizes the following keys.
71              
72             =over 4
73              
74             =item C => $true_or_false
75              
76             Creates a default constructor if the value evaluates to true. Normally no
77             constructor is created. The constructor accepts a hash of arguments to
78             initialize a new object.
79              
80             =item C => \@name_of_the_properties
81              
82             Creates a scalar read-write accessor for the property names in the array
83             reference.
84              
85             =back