File Coverage

blib/lib/EntityModel/Hash.pm
Criterion Covered Total %
statement 56 59 94.9
branch 4 6 66.6
condition 7 11 63.6
subroutine 15 16 93.7
pod 11 11 100.0
total 93 103 90.2


line stmt bran cond sub pod time code
1             package EntityModel::Hash;
2             $EntityModel::Hash::VERSION = '0.016';
3 2     2   42550 use strict;
  2         6  
  2         105  
4 2     2   11 use warnings;
  2         3  
  2         93  
5              
6 2     2   740 use EntityModel::Log ':all';
  2         36186  
  2         554  
7              
8             =head1 NAME
9              
10             EntityModel::Hash - wrapper object for dealing with hashrefs
11              
12             =head1 VERSION
13              
14             Version 0.016
15              
16             =head1 DESCRIPTION
17              
18             Primarily intended as an abstract interface for use with L backend storage.
19              
20             =head1 METHODS
21              
22             =cut
23              
24             use overload
25             '%{}' => sub {
26 1     1   3 my $self = shift;
27 1         4 return $self->hashref;
28             },
29 2     2   19 fallback => 1;
  2         5  
  2         24  
30              
31             =head2 new
32              
33             Instantiates with the given hashref.
34              
35             =cut
36              
37             sub new {
38 3     3 1 25 my ($class, $data) = @_;
39 3   100     34 bless { data => ($data // { }) }, $class;
40             }
41              
42             =head2 count
43              
44             Returns the number of items in the hashref (i.e. keys).
45              
46             =cut
47              
48             sub count {
49 4     4 1 7 my $self = shift;
50 4         6 return scalar keys %{$self->hashref};
  4         25  
51             }
52              
53             =head2 list
54              
55             Returns all values from the hashref.
56              
57             =cut
58              
59             sub list {
60 1     1 1 2 my $self = shift;
61 1 50       4 return unless $self->hashref;
62 1         3 return values %{$self->hashref};
  1         3  
63             }
64              
65             =head2 set
66              
67             Sets an entry (identified by key) to the given value.
68              
69             =cut
70              
71             sub set {
72 5     5 1 9 my $self = shift;
73 5         11 my ($k, $v) = @_;
74 5 50       15 unless(defined $k) {
75             # logStack("No k?");
76 0         0 return $self;
77             }
78 5 100 66     25 if(ref($k) && ref($k) eq 'HASH') {
79 1         7 $self->hashref->{$_} = $k->{$_} foreach keys %$k;
80             } else {
81 4         11 $self->hashref->{$k} = $v;
82             }
83 5         17 return $self;
84             }
85              
86             =head2 erase
87              
88             Deletes the given key from the hashref.
89              
90             =cut
91              
92             sub erase {
93 1     1 1 3 my $self = shift;
94 1         3 my ($k) = @_;
95 1   50     4 $k //= '';
96 1         3 delete $self->hashref->{$k};
97 1         3 return $self;
98             }
99              
100             =head2 get
101              
102             Retrieves the value for the given key.
103              
104             =cut
105              
106             sub get {
107 7     7 1 17 my ($self, $k) = @_;
108 7   50     21 $k //= '';
109 7         21 return $self->hashref->{$k};
110             }
111              
112             =head2 hashref
113              
114             Returns the contained hashref.
115              
116             =cut
117              
118             sub hashref {
119 26     26 1 12357 my $self = shift;
120 26         42 my $class = ref $self;
121 26         76 bless $self, 'overload::dummy';
122 26         42 my $out = $self->{data};
123 26         54 bless $self, $class;
124 26         319 return $out;
125             }
126              
127             =head2 exists
128              
129             Returns true if the given key exists in the hashref.
130              
131             =cut
132              
133             sub exists : method {
134 2     2 1 4 my ($self, $k) = @_;
135 2   50     9 $k //= '';
136 2         6 return exists($self->hashref->{$k});
137             }
138              
139             =head2 keys
140              
141             Returns a list of all keys.
142              
143             =cut
144              
145             sub keys : method {
146 1     1 1 3 my $self = shift;
147 1         2 return keys %{$self->hashref};
  1         2  
148             }
149              
150             =head2 clear
151              
152             Clears the hashref.
153              
154             =cut
155              
156             sub clear : method {
157 1     1 1 3 my $self = shift;
158 1         45 my $class = ref $self;
159 1         4 bless $self, 'overload::dummy';
160 1         3 $self->{data} = { };
161 1         3 bless $self, $class;
162 1         5 return $self;
163             }
164              
165             =head2 is_empty
166              
167             Returns true if there's nothing in the hashref.
168              
169             =cut
170              
171             sub is_empty {
172 0     0 1   my $self = shift;
173 0           return !$self->keys;
174             }
175              
176             1;
177              
178             __END__