File Coverage

blib/lib/Data/Manager.pm
Criterion Covered Total %
statement 27 27 100.0
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 39 40 97.5


line stmt bran cond sub pod time code
1             package Data::Manager;
2             {
3             $Data::Manager::VERSION = '0.10';
4             }
5 3     3   63915 use Moose;
  3         1401151  
  3         22  
6 3     3   22218 use MooseX::Storage;
  3         118155  
  3         15  
7              
8             with 'MooseX::Storage::Deferred';
9              
10             # ABSTRACT: The Marriage of Message::Stack & Data::Verifier
11              
12 3     3   2803 use Message::Stack;
  3         1622943  
  3         129  
13 3     3   2811 use Message::Stack::Parser::DataVerifier;
  3         40091  
  3         972  
14              
15              
16             has 'messages' => (
17             is => 'ro',
18             isa => 'Message::Stack',
19             lazy => 1,
20             default => sub { Message::Stack->new },
21             handles => {
22             'messages_for_scope' => 'for_scope',
23             }
24             );
25              
26             has '_parser' => (
27             is => 'ro',
28             isa => 'Message::Stack::DataVerifier',
29             );
30              
31              
32             has 'results' => (
33             traits => [ 'Hash' ],
34             is => 'ro',
35             isa => 'HashRef',
36             default => sub { {} },
37             handles => {
38             'set_results' => 'set',
39             'get_results' => 'get'
40             }
41             );
42              
43              
44             has 'verifiers' => (
45             traits => [ 'Hash', 'DoNotSerialize' ],
46             is => 'ro',
47             isa => 'HashRef',
48             default => sub { {} },
49             handles => {
50             'set_verifier' => 'set',
51             'get_verifier' => 'get'
52             }
53             );
54              
55              
56             sub success {
57 3     3 1 1254 my ($self) = @_;
58              
59 3         4 foreach my $res (keys %{ $self->results }) {
  3         102  
60 4 100       453 return 0 unless $self->get_results($res)->success;
61             }
62              
63 1         303 return 1;
64             }
65              
66              
67             sub verify {
68 4     4 1 14 my ($self, $scope, $data) = @_;
69              
70 4         158 my $verifier = $self->get_verifier($scope);
71 4 50       12 die("No verifier for scope: $scope") unless defined($verifier);
72              
73 4         19 my $results = $verifier->verify($data);
74 4         10096 $self->set_results($scope, $results);
75              
76 4         134 Message::Stack::Parser::DataVerifier::parse($self->messages, $scope, $results);
77              
78 4         2430 return $results;
79             }
80              
81              
82             __PACKAGE__->meta->make_immutable;
83 3     3   22 no Moose;
  3         4  
  3         24  
84              
85             1;
86             __END__
87             =pod
88              
89             =head1 NAME
90              
91             Data::Manager - The Marriage of Message::Stack & Data::Verifier
92              
93             =head1 VERSION
94              
95             version 0.10
96              
97             =head1 SYNOPSIS
98              
99             use Data::Manager;
100             use Data::Verifier;
101              
102             my $dm = Data::Manager->new;
103              
104             # Create a verifier for the 'billing_address'
105             my $verifier = Data::Verifier->new(
106             profile => {
107             address1 => {
108             required=> 1,
109             type => 'Str'
110             }
111             # ... more fields
112             }
113             );
114             $dm->set_verifier('billing_address', $verifier);
115              
116             # Addresses are the same, reuse the verifier
117             $dm->set_verifier('shipping_address', $verifier);
118              
119             my $ship_data = {
120             address1 => { '123 Test Street' },
121             # ... more
122             };
123             my $bill_data => {
124             address1 => { '123 Test Street' }
125             # ... more
126             };
127              
128             $dm->verify('billing_address', $bill_data);
129             $dm->verify('shipping_address', $ship_data);
130            
131             # Later...
132            
133             my $bill_results = $dm->get_results('billing_address');
134             my $bill_stack = $dm->messages_for_scope('billing_address');
135            
136             my $ship_results = $dm->get_results('shipping_address');
137             my $ship_stack = $dm->messages_for_scope('shipping_address');
138              
139             =head1 DESCRIPTION
140              
141             Data::Manager provides a convenient mechanism for managing multiple
142             L<Data::Verifier> inputs with a single L<Message::Stack>, as well as
143             convenient retrieval of the results of verification.
144              
145             This module is useful if you have complex forms and you'd prefer to create
146             separate L<Data::Verifier> objects, but want to avoid creating a complex
147             hashref of your own creation to manage things.
148              
149             It should also be noted that if married with L<MooseX::Storage>, this entire
150             object and it's contents can be serialized. This maybe be useful with
151             L<Catalyst>'s C<flash> for storing the results of verification between
152             redirects.
153              
154             =head1 SERIALIZATION
155              
156             The Data::Manager object may be serialized thusly:
157              
158             my $ser = $dm->freeze({ format => 'JSON' });
159             # later
160             my $dm = Data::Manager->thaw($ser, { format => 'JSON' });
161              
162             This is possible thanks to the magic of L<MooseX::Storage>. All attributes
163             B<except> C<verifiers> are stored. B<Serialization causes the verifiers
164             attribute to be set to undefined, as those objects are not serializable>.
165              
166             =head1 ATTRIBUTES
167              
168             =head2 messages
169              
170             The L<Message::Stack> object for this manager. This attribute is lazily
171             populated, parsing the L<Data::Verifier::Results> objects. After fetching
172             this attribute any changes to the results B<will not be reflected in the
173             message stack>.
174              
175             =head2 results
176              
177             HashRef of L<Data::Verifier::Results> objects, keyed by scope.
178              
179             =head2 verifiers
180              
181             HashRef of L<Data::Verifier> objects, keyed by scope.
182              
183             =head1 METHODS
184              
185             =head2 messages_for_scope ($scope)
186              
187             Returns a L<Message::Stack> object containing messages for the specified
188             scope.
189              
190             =head2 get_results ($scope)
191              
192             Gets the L<Data::Verifier::Results> object for the specified scope.
193              
194             =head2 set_results ($scope, $results)
195              
196             Sets the L<Data::Verifier::Results> object for the specified scope.
197              
198             =head2 success
199              
200             Convenience method that checks C<success> on each of the results in this
201             manager. Returns false if any are false.
202              
203             =head2 verify ($scope, $data);
204              
205             Verify the data against the specified scope. After verification the results
206             and messages will be automatically created and stored. The
207             L<Data::Verifier::Results> class will be returned.
208              
209             =head1 ACKNOWLEDGEMENTS
210              
211             Justin Hunter
212              
213             Jay Shirley
214              
215             Brian Cassidy
216              
217             =head1 AUTHOR
218              
219             Cory G Watson <gphat@cpan.org>
220              
221             =head1 COPYRIGHT AND LICENSE
222              
223             This software is copyright (c) 2012 by Cory G Watson.
224              
225             This is free software; you can redistribute it and/or modify it under
226             the same terms as the Perl 5 programming language system itself.
227              
228             =cut
229