File Coverage

blib/lib/EO/Array.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             package EO::Array;
2              
3 7     7   34744 use strict;
  7         15  
  7         219  
4 7     7   39 use warnings;
  7         12  
  7         216  
5 7     7   3518 use EO::Collection;
  0            
  0            
6              
7             our $VERSION = 0.96;
8             our @ISA = qw( EO::Collection );
9              
10             use overload '@{}' => 'get_reference',
11             'fallback' => 1;
12              
13             sub init {
14             my $self = CORE::shift;
15             if ($self->SUPER::init( @_ )) {
16             $self->element( [] );
17             return 1;
18             }
19             return 0;
20             }
21              
22             sub get_reference {
23             my $self = shift;
24             return $self->element;
25             }
26              
27             sub new_with_array {
28             my $class = CORE::shift;
29             my $array;
30             if (@_ > 1) {
31             $array = [ @_ ];
32             } else {
33             $array = CORE::shift;
34             if (defined($array) && !ref($array)) {
35             print "Not an array, making it one...\n";
36             $array = [ $array ];
37             }
38             }
39             if (!$array) {
40             throw EO::Error::InvalidParameters text => 'no array specified';
41             }
42             if (!ref($array)) {
43             throw EO::Error::InvalidParameters text => 'not a reference';
44             }
45             if (ref($array) && ref($array) ne 'ARRAY') {
46             throw EO::Error::InvalidParameters text => 'not an array reference';
47             }
48             my $self = $class->new();
49             $self->element( $array );
50             return $self;
51             }
52              
53             sub do {
54             my $self = CORE::shift;
55             my $code = CORE::shift;
56             my $array = ref($self)->new();
57             foreach my $element (@{ $self->element }) {
58             local $_ = $element;
59             $array->push( $code->( $element ) );
60             }
61             return $array;
62             }
63              
64             sub reverse {
65             my $self = CORE::shift;
66             ref($self)->new_with_array( CORE::reverse( @{ $self->element } ) );
67             }
68              
69             sub select {
70             my $self = CORE::shift;
71             my $code = CORE::shift;
72             my $array = ref($self)->new();
73             foreach my $element (@{ $self->element }) {
74             local $_ = $element;
75             $array->push( $element ) if $code->( $element );
76             }
77             return $array;
78             }
79              
80             sub at {
81             my $self = CORE::shift;
82             my $idx = CORE::shift;
83             if (!defined($idx)) {
84             throw EO::Error::InvalidParameters
85             text => 'no index provided for the array';
86             }
87             if ($idx =~ /\D/) {
88             throw EO::Error::InvalidParameters
89             text => 'non-integer index specified';
90             }
91             if (@_) {
92             $self->element->[ $idx ] = CORE::shift;
93             return $self;
94             }
95             return $self->element->[ $idx ];
96             }
97              
98             sub object_at_index : Deprecated {
99             my $self = shift;
100             $self->at( @_ );
101             }
102              
103             sub delete {
104             my $self = CORE::shift;
105             my $idx = CORE::shift;
106             if (!defined($idx)) {
107             throw EO::Error::InvalidParameters text => "no index provided for the array";
108             }
109             if ($idx =~ /\D/) {
110             throw EO::Error::InvalidParameters text => 'non-integer index specified';
111             }
112             $self->splice( $idx, 1 );
113             }
114              
115             sub splice {
116             my $self = CORE::shift;
117             my $offset = CORE::shift;
118             my $length = CORE::shift;
119             if (!@_ && $length) {
120              
121             return CORE::splice( @$self, $offset, $length );
122              
123             } elsif (!defined $length) {
124              
125             return CORE::splice( @$self, $offset );
126              
127             } elsif (!defined $offset) {
128              
129             return CORE::splice( @$self );
130              
131             } else {
132              
133             return CORE::splice(@$self, $offset, $length, @_);
134              
135             }
136             }
137              
138             sub count {
139             my $self = CORE::shift;
140             return scalar( $self->iterator );
141             }
142              
143             sub push {
144             my $self = CORE::shift;
145             $self->splice( $self->count, 0, @_ );
146             return $self;
147             }
148              
149             sub pop {
150             my $self = CORE::shift;
151             $self->splice( -1 );
152             }
153              
154             sub shift {
155             my $self = CORE::shift;
156             $self->splice( 0, 1 );
157             }
158              
159             sub unshift {
160             my $self = CORE::shift;
161             $self->splice( 0, 0, @_ );
162             return $self;
163             }
164              
165             sub join {
166             my $self = CORE::shift;
167             my $joiner = CORE::shift;
168             return join($joiner, $self->iterator);
169             }
170              
171             sub iterator {
172             my $self = CORE::shift;
173             my @list = @{ $self->element };
174             return @list;
175             }
176              
177             1;
178              
179             __END__