| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Elive::DAO::Array; | 
| 2 | 7 |  |  | 7 |  | 3027 | use warnings; use strict; | 
|  | 7 |  |  | 7 |  | 9 |  | 
|  | 7 |  |  |  |  | 214 |  | 
|  | 7 |  |  |  |  | 26 |  | 
|  | 7 |  |  |  |  | 8 |  | 
|  | 7 |  |  |  |  | 169 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 7 |  |  | 7 |  | 27 | use Mouse; | 
|  | 7 |  |  |  |  | 9 |  | 
|  | 7 |  |  |  |  | 43 |  | 
| 5 | 7 |  |  | 7 |  | 2104 | use Mouse::Util::TypeConstraints; | 
|  | 7 |  |  |  |  | 9 |  | 
|  | 7 |  |  |  |  | 39 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 7 |  |  | 7 |  | 903 | use parent qw{Elive::DAO::_Base}; | 
|  | 7 |  |  |  |  | 239 |  | 
|  | 7 |  |  |  |  | 63 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 7 |  |  | 7 |  | 475 | use Scalar::Util; | 
|  | 7 |  |  |  |  | 10 |  | 
|  | 7 |  |  |  |  | 306 |  | 
| 10 | 7 |  |  | 7 |  | 35 | use Elive::Util; | 
|  | 7 |  |  |  |  | 16 |  | 
|  | 7 |  |  |  |  | 3159 |  | 
| 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 |  | 9 | my $class = shift; | 
| 33 | 8 |  |  |  |  | 7 | my $spec = shift; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 8 |  |  |  |  | 13 | my $type = Elive::Util::_reftype( $spec ); | 
| 36 | 8 | 50 | 33 |  |  | 28 | $spec = [$spec] if ($type && $type ne 'ARRAY'); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 8 |  |  |  |  | 9 | my @args; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 8 | 50 |  |  |  | 10 | if ($type) { | 
| 41 | 8 |  |  |  |  | 13 | @args = map {Elive::Util::string($_)} @$spec; | 
|  | 24 |  |  |  |  | 34 |  | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | else { | 
| 44 | 0 |  |  |  |  | 0 | @args = split($class->separator, Elive::Util::string($spec)); | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 8 |  |  |  |  | 20 | 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 | 113 | my $class = shift; | 
| 64 | 66 |  |  |  |  | 68 | my $spec  = shift; | 
| 65 | 66 |  | 66 |  |  | 211 | my $type = shift || $class->element_class; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 66 | 50 | 33 |  |  | 533 | $spec = $class | 
| 68 |  |  |  |  |  |  | if !defined $spec && ref $class; | 
| 69 | 66 |  |  |  |  | 177 | my $arr = $class->_build_array( $spec ); | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 66 |  |  |  |  | 178 | return join($class->separator, sort map {Elive::Util::string($_, $type)} @$arr) | 
|  | 130 |  |  |  |  | 569 |  | 
| 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; |