blib/lib/ODG/Record.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 57 CC 6 SCAR 27.4
CC
6
Cov
0%
BUILD
30.0
_data
6.0
_has_data
2.0
CRAP 15.5
1 # ----------------------------------------------------------------------
2 # CLASS: ODG::Record
3 # ----------------------------------------------------------------------
4 package ODG::Record;
5 use 5.008;
6 use Moose;
7 with 'MooseX::Meta::Attribute::Index';
8  
9 our $VERSION = '0.30';
10  
11 # We do not install any accessors since we want to make this
12 # lvalue ready,
13 has _data => (
14 isa => 'ArrayRef' ,
15 # predicate => '_has_data' ,
16 default => sub { [] } ,
17 documentaion => 'slot for holding data' ,
18 );
19  
20  
21 sub _has_data {
22 scalar( @{ $_[0]->_data } ) > 1;
23 }
24  
25  
26 # _data
27 # lvalue accessor to the data slot.
28 sub _data :lvalue {
29  
30 $_[0]->{_data} = $_[1] if ( $_[1] );
31 $_[0]->{_data};
32  
33 }
34  
35  
36 # -----------------------------------------------------------------------
37 # FIELD ACCESSORS:
38 # Create FIELD ACCESSORS for each field.
39 #
40 # We want to install instance methods and not class methods. The class
41 # method would cause these methods to be available to subsequent classes.
42 # This would be undesirable because there might be two ODG::Record objects
43 # Thanks to Paul Driver
44 #
45 # -----------------------------------------------------------------------
46 sub BUILD {
47  
48 use Data::Dumper;
49 my ( $self, @args ) = @_;
50 my $meta = $self->meta;
51 my $index;
52  
53 foreach my $field ( keys %{ $meta->get_attribute_map } ) {
54  
55 my $attr = $meta->get_attribute( $field );
56  
57 # Check to see if each index is used only once
58 if ( $attr->can('index') ) {
59  
60 $index->{ $attr->index }++ ;
61 confess( "Two fields share index: " . $attr->index ."\n" )
62 if ( $index->{ $attr->index } > 1 );
63  
64 if (
65 $attr->_is_metadata and
66 $attr->_is_metadata eq 'rw'
67 ) {
68
69 # Installs lvalue accessor
70 $meta->add_method(
71 $field ,
72 sub :lvalue {
73  
74 # Update the old value if used second argument is passed
75 $self->{_data}->[ $attr->index ] = $_[1] if ( $_[1] );
76
77 # LVALUE return
78 $self->{_data}->[ $attr->index ] ;
79 }
80 );
81  
82 } else {
83
84 $meta->add_method(
85 $field,
86 sub { $self->{_data}->[ $attr->index ] }
87 );
88  
89 }
90  
91 }
92  
93 }
94  
95 }
96  
97  
98 1;
99  
100 __END__