File Coverage

blib/lib/Data/Context/BEM/Merge.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::Context::BEM::Merge;
2              
3             # Created on: 2013-11-15 05:13:46
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   62016 use Moose;
  0            
  0            
10             use namespace::autoclean;
11             use version;
12             use Carp;
13             use List::Util qw/max /;
14             use List::MoreUtils qw/uniq pairwise/;
15             use Data::Dumper qw/Dumper/;
16             use English qw/ -no_match_vars /;
17              
18             our $VERSION = version->new('0.0.3');
19              
20             sub merge {
21             my ($self, $child, $parent) = @_;
22              
23             if ( ! ref $child ) {
24             return $child;
25             }
26             elsif ( ref $child eq 'ARRAY' ) {
27             my $new = [];
28             my $max_child = @$child - 1;
29             my $max_parent = @$parent - 1;
30              
31             for my $i ( 0 .. max $max_child, $max_parent ) {
32             $new->[$i]
33             = exists $child->[$i]
34             ? $self->merge( $child->[$i], $parent->[$i] )
35             : $parent->[$i];
36             }
37              
38             return $new;
39             }
40             elsif ( ref $child eq 'HASH' ) {
41             my $new = {};
42              
43             for my $key ( uniq sort +(keys %$child), (keys %$parent) ) {
44             if ( $key eq 'content' ) {
45             $child->{$key} = [ $child->{$key} ] if ref $child->{$key} ne 'ARRAY';
46             $parent->{$key} = [ $parent->{$key} ] if ref $parent->{$key} ne 'ARRAY';
47             }
48              
49             $new->{$key}
50             = exists $child->{$key}
51             ? $self->merge( $child->{$key}, $parent->{$key} )
52             : $parent->{$key};
53             }
54              
55             return $new;
56             }
57             else {
58             return $child;
59             }
60             }
61              
62             __PACKAGE__->meta->make_immutable;
63              
64             1;
65              
66             __END__
67              
68             =head1 NAME
69              
70             Data::Context::BEM::Merge - Merge algorithm that merges arrays (not appending them)
71              
72             =head1 VERSION
73              
74             This documentation refers to Data::Context::BEM::Merge version 0.0.3
75              
76             =head1 SYNOPSIS
77              
78             use Data::Context::BEM::Merge;
79              
80             my $merge = Data::Context::BEM::Merge->new();
81             my $merged = $merge->merge({a => [1,2]}, {a => [2,3]});
82              
83             # $merged = { a => [2,3] }
84              
85             =head1 DESCRIPTION
86              
87             =head1 SUBROUTINES/METHODS
88              
89             =head2 C<merge ($ref1, $ref2)>
90              
91             Merges $ref2 into clone of $ref1.
92              
93             =head1 DIAGNOSTICS
94              
95             =head1 CONFIGURATION AND ENVIRONMENT
96              
97             =head1 DEPENDENCIES
98              
99             =head1 INCOMPATIBILITIES
100              
101             =head1 BUGS AND LIMITATIONS
102              
103             There are no known bugs in this module.
104              
105             Please report problems to Ivan Wills (ivan.wills@gmail.com).
106              
107             Patches are welcome.
108              
109             =head1 AUTHOR
110              
111             Ivan Wills - (ivan.wills@gmail.com)
112              
113             =head1 LICENSE AND COPYRIGHT
114              
115             Copyright (c) 2013 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
116             All rights reserved.
117              
118             This module is free software; you can redistribute it and/or modify it under
119             the same terms as Perl itself. See L<perlartistic>. This program is
120             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
121             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
122             PARTICULAR PURPOSE.
123              
124             =cut