File Coverage

blib/lib/Document/Transform/Transformer.pm
Criterion Covered Total %
statement 16 36 44.4
branch 0 8 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 23 52 44.2


line stmt bran cond sub pod time code
1             package Document::Transform::Transformer;
2             BEGIN {
3 2     2   42 $Document::Transform::Transformer::VERSION = '1.110530';
4             }
5              
6             #ABSTRACT: A document transformer. More than meets the eye. Or not.
7              
8 2     2   12 use Moose;
  2         4  
  2         19  
9 2     2   12710 use namespace::autoclean;
  2         3  
  2         19  
10              
11 2     2   2639 use Data::DPath('dpathr');
  2         244029  
  2         15  
12 2     2   451 use MooseX::Types::Moose(':all');
  2         5  
  2         27  
13 2     2   20006 use MooseX::Params::Validate;
  2         5  
  2         19  
14              
15              
16              
17             has $_.'_constraint' =>
18             (
19             is => 'ro',
20             isa => 'Moose::Meta::TypeConstraint',
21             required => 1,
22             ) for qw/document transform/;
23              
24              
25             sub transform
26             {
27 0     0 1   my $self = shift;
28 0           my ($document, $transforms) = pos_validated_list
29             (
30             \@_,
31             {isa => $self->document_constraint},
32             {isa => ArrayRef[$self->transform_constraint]},
33             );
34              
35 0           foreach my $transform(@$transforms)
36             {
37 0           foreach my $operation (@{$transform->{operations}})
  0            
38             {
39 0           my @refs = dpathr($operation->{path})->match($document);
40 0 0         unless(scalar(@refs))
41             {
42 0 0         if($operation->{path} =~ m#\[|\]|\.|\*|\"|//#)
43             {
44 0           Throwable::Error->throw
45             ({
46             message => 'transform path not found and the path is '.
47             'too complex for simple structure building'
48             });
49             }
50             else
51             {
52 0           my @paths = split('/', $operation->{path});
53 0           my $place = $document;
54 0           for(0..$#paths)
55             {
56 0 0         next if $paths[$_] eq '';
57 0 0         if($_ == $#paths)
58             {
59 0           $place->{$paths[$_]} = $operation->{value};
60             }
61             else
62             {
63 0           $place = \%{$place->{$paths[$_]} = {}};
  0            
64             }
65             }
66             }
67             }
68             else
69             {
70 0           map { $$_ = $operation->{value}; } @refs;
  0            
71             }
72             }
73             }
74              
75 0           return $document;
76             }
77              
78             with 'Document::Transform::Role::Transformer';
79              
80             __PACKAGE__->meta->make_immutable();
81             1;
82              
83              
84              
85             =pod
86              
87             =head1 NAME
88              
89             Document::Transform::Transformer - A document transformer. More than meets the eye. Or not.
90              
91             =head1 VERSION
92              
93             version 1.110530
94              
95             =head1 SYNOPSIS
96              
97             use Document::Transform::Transformer;
98             use MyTypeLib(':all');
99              
100             my $transformer = Document::Transform::Transformer->new
101             (
102             document_constraint => Document,
103             transform_constraint => Transform,
104             );
105             my $altered = $transformer->transform($document, $transforms);
106              
107             =head1 DESCRIPTION
108              
109             Need a simple transformer that mashes up a transform and a document into
110             something awesome? This is your module then.
111              
112             This is the default for Document::Transformer to use. It expects data
113             structures that align with whatever type constraints are passed into the
114             constructor that represent a Document and a Transform. It implements the
115             interface role L<Document::Transform::Role::Transformer>
116              
117             =head1 PUBLIC_ATTRIBUTES
118              
119             =head2 document_constraint
120              
121             is: ro, isa: Moose::Meta::TypeConstraint, required: 1
122              
123             The default Transformer must have the constraints supplied to it via
124             constructor. This constraint should check for whatever is a valid Document for
125             the backend.
126              
127             =head2 transform_constraint
128              
129             is: ro, isa: Moose::Meta::TypeConstraint, required: 1
130              
131             The default Transformer must have the constraints supplied to it via
132             constructor. This constraint should check for whatever is a valid Transform for
133             the backend.
134              
135             =head1 PUBLIC_METHODS
136              
137             =head2 transform
138              
139             (Document, ArrayRef[Transform])
140              
141             transform takes a Document and an array of Transforms and performs the
142             operations contained with the transforms against the document. Returns the
143             transformed document.
144              
145             The type constraints for Document and Transform are stored in the attributes
146             L</document_constraint> and L</transform_constraint>, respectively.
147              
148             =head1 AUTHOR
149              
150             Nicholas R. Perez <nperez@cpan.org>
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is copyright (c) 2010 by Infinity Interactive.
155              
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158              
159             =cut
160              
161              
162             __END__
163