File Coverage

blib/lib/Elive/DAO/Array.pm
Criterion Covered Total %
statement 37 47 78.7
branch 3 10 30.0
condition 4 12 33.3
subroutine 9 11 81.8
pod 3 3 100.0
total 56 83 67.4


line stmt bran cond sub pod time code
1             package Elive::DAO::Array;
2 6     6   3799 use warnings; use strict;
  6     6   13  
  6         195  
  6         34  
  6         12  
  6         209  
3              
4 6     6   34 use Mouse;
  6         12  
  6         46  
5 6     6   2020 use Mouse::Util::TypeConstraints;
  6         12  
  6         47  
6              
7 6     6   1405 use parent qw{Elive::DAO::_Base};
  6         365  
  6         98  
8              
9 6     6   435 use Scalar::Util;
  6         18  
  6         255  
10 6     6   31 use Elive::Util;
  6         13  
  6         3204  
11              
12             __PACKAGE__->mk_classdata('element_class');
13             __PACKAGE__->mk_classdata('separator' => ',');
14             __PACKAGE__->has_metadata( '_is_copy' );
15              
16             =head1 NAME
17              
18             Elive::DAO::Array - Base class for arrays
19              
20             =head1 DESCRIPTION
21              
22             Base class for arrays within entities. E.g. members property of
23             Elive::Entity::participantList.
24              
25             =cut
26              
27             =head1 METHODS
28              
29             =cut
30              
31             sub _build_array {
32 8     8   14 my $class = shift;
33 8         11 my $spec = shift;
34              
35 8         20 my $type = Elive::Util::_reftype( $spec );
36 8 50 33     41 $spec = [$spec] if ($type && $type ne 'ARRAY');
37              
38 8         10 my @args;
39              
40 8 50       16 if ($type) {
41 8         17 @args = map {Elive::Util::string($_)} @$spec;
  24         58  
42             }
43             else {
44 0         0 @args = split($class->separator, Elive::Util::string($spec));
45             }
46              
47 8         23 return \@args;
48             }
49              
50             our $class = __PACKAGE__;
51             coerce $class => from 'ArrayRef|Str'
52             => via {
53             $class->new( $_ );
54             };
55              
56             =head2 stringify
57              
58             Serialises array members by joining individual elements.
59              
60             =cut
61              
62             sub stringify {
63 66     66 1 150 my $class = shift;
64 66         162 my $spec = shift;
65 66   66     291 my $type = shift || $class->element_class;
66              
67 66 50 33     706 $spec = $class
68             if !defined $spec && ref $class;
69 66         237 my $arr = $class->_build_array( $spec );
70              
71 66         232 return join($class->separator, sort map {Elive::Util::string($_, $type)} @$arr)
  130         761  
72             }
73              
74             =head2 new
75              
76             my $array_obj = Elive::DAO::Array->new($array_ref);
77              
78             =cut
79              
80             sub new {
81 0     0 1   my ($class, $spec) = @_;
82 0           my $array = $class->_build_array( $spec );
83 0           return bless($array, $class);
84             }
85              
86             =head2 add
87              
88             $group->members->add('111111', '222222');
89              
90             Add elements to an array.
91              
92             =cut
93              
94             sub add {
95 0     0 1   my ($self, @elems) = @_;
96              
97 0 0         if (my $element_class = $self->element_class) {
98 0           foreach (@elems) {
99 0 0 0       $_ = $element_class->new($_)
100             if ref && ! Scalar::Util::blessed($_);
101             }
102             }
103              
104 0           push (@$self, @elems);
105              
106 0           return $self;
107             }
108              
109             1;