File Coverage

blib/lib/EO/Hash.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1              
2             package EO::Hash;
3              
4 2     2   26241 use strict;
  2         4  
  2         72  
5 2     2   10 use warnings;
  2         3  
  2         53  
6              
7 2     2   1040 use EO::Pair;
  0            
  0            
8             use EO::Array;
9             use EO::Collection;
10              
11             our $VERSION = 0.96;
12             our @ISA = qw( EO::Collection );
13              
14             use overload '%{}' => 'get_reference',
15             'fallback' => 1;
16              
17             sub init {
18             my $self = CORE::shift;
19             my $elems = CORE::shift;
20             if ($self->SUPER::init( @_ )) {
21             $self->element( {} );
22             return 1;
23             }
24             return 0;
25             }
26              
27             sub get_reference {
28             my $self = shift;
29             my $callpkg = caller();
30             my $prefix = substr($callpkg,0,4);
31             if ($prefix eq 'EO::' || $prefix eq 'EO') {
32             return $self;
33             }
34             return $self->element;
35             }
36              
37             sub new_with_hash {
38             my $class = shift;
39             my $hash;
40             if (@_ > 1) {
41             $hash = { @_ };
42             } else {
43             $hash = shift;
44             }
45             if (!$hash) {
46             throw EO::Error::InvalidParameters
47             text => 'no hash provided';
48             }
49             if (!ref($hash)) {
50             throw EO::Error::InvalidParameters
51             text => 'not a reference';
52             }
53             if (ref($hash) && ref($hash) ne 'HASH') {
54             throw EO::Error::InvalidParameters
55             text => 'not a hash reference';
56             }
57             my $self = $class->new();
58             $self->element( $hash );
59             return $self;
60             }
61              
62             sub at {
63             my $self = shift;
64             my $key = shift;
65             if (!$key) {
66             throw EO::Error::InvalidParameters
67             text => 'no key specified for at';
68             }
69             if (@_) {
70             $self->element->{ $key } = shift;
71             return $self;
72             }
73             return $self->element->{ $key };
74             }
75              
76             sub pair_for {
77             my $self = shift;
78             my $key = shift;
79             return EO::Pair->new
80             ->key( $key )
81             ->value( $self->at( $key ) );
82             }
83              
84             sub do {
85             my $self = shift;
86             my $code = shift;
87             unless( $code and ref($code) eq 'CODE') {
88             throw EO::Error::InvalidParameters
89             text => 'must have a code reference as a parameter';
90             }
91             my $hash = ref($self)->new();
92             foreach my $key (keys %{ $self->element }) {
93             my $pair = $self->pair_for( $key );
94             $hash->add( $pair->do( $code ) );
95             }
96             return $hash;
97             }
98              
99             sub select {
100             my $self = shift;
101             my $code = shift;
102             unless( $code and ref($code) eq 'CODE') {
103             throw EO::Error::InvalidParameters
104             text => 'must have a code reference as a parameter';
105             }
106             my $hash = ref($self)->new();
107             foreach my $key (keys %{ $self->element }) {
108             my $pair = $self->pair_for( $key );
109             if ( $pair->do( $code ) ) {
110             $hash->add( $pair );
111             }
112             }
113             return $hash;
114             }
115              
116             sub add {
117             my $self = shift;
118             my $pair = shift;
119             if (!$pair or !$pair->isa('EO::Pair')) {
120             throw EO::Error::InvalidParameters
121             text => 'argument to add must be a pair';
122             }
123             $self->at( $pair->key, $pair->value );
124             }
125              
126             sub object_at_index : Deprecated {
127             my $self = shift;
128             $self->at( @_ );
129             }
130              
131             sub delete {
132             my $self = shift;
133             my $key = shift;
134             if (!$key) {
135             throw EO::Error::InvalidParameters text => 'no key specified for delete';
136             }
137             delete $self->element->{ $key };
138             }
139              
140             sub count {
141             my $self = shift;
142             $self->keys->count;
143             }
144              
145             sub iterator {
146             my $self = shift;
147             my %iter = %{$self->element };
148             return %iter;
149             }
150              
151             sub keys {
152             my $self = shift;
153             my %hash = $self->iterator;
154             if (!wantarray) {
155             my $array = EO::Array->new()->push( keys %hash );
156             return $array;
157             } else {
158             return keys %hash;
159             }
160             }
161              
162             sub values {
163             my $self = shift;
164             my %hash = $self->iterator;
165             if (!wantarray) {
166             my $array = EO::Array->new()->push( values %hash );
167             return $array;
168             } else {
169             return values %hash;
170             }
171             }
172              
173             sub has {
174             my $self = shift;
175             my $key = shift;
176             exists $self->element->{ $key }
177             }
178              
179             1;
180              
181             __END__