File Coverage

blib/lib/Data/Transpose/Field.pm
Criterion Covered Total %
statement 32 32 100.0
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 45 46 97.8


line stmt bran cond sub pod time code
1             package Data::Transpose::Field;
2              
3 8     8   681 use strict;
  8         7  
  8         169  
4 8     8   29 use warnings;
  8         8  
  8         154  
5              
6             =head1 NAME
7              
8             Data::Transpose::Field - Field class for Data::Transpose
9              
10             =head1 SYNOPSIS
11              
12             $field = $tp->field('email');
13              
14             =head1 METHODS
15              
16             =head2 new
17              
18             $field = Data::Transpose::Field->new(name => 'email');
19              
20             =cut
21              
22 8     8   2622 use Moo;
  8         56642  
  8         32  
23 8     8   11235 use MooX::Types::MooseLike::Base qw(:all);
  8         29547  
  8         2056  
24 8     8   2938 use namespace::clean;
  8         60801  
  8         24  
25              
26             has name => (is => 'rw',
27             required => 1,
28             isa => Str);
29              
30             has target => (is => 'rw',
31             isa => Str);
32              
33             has _raw => (is => 'rwp');
34             has _output => (is => 'rwp');
35             has _filters => (is => 'ro',
36             isa => ArrayRef,
37             default => sub { [] },
38             );
39              
40             =head2 name
41              
42             Set name of the field:
43              
44             $field->name('fullname');
45              
46             Get name of the field:
47              
48             $field->name;
49              
50             =head2 value
51              
52             Initializes field value and returns value for output:
53            
54             $new_value = $self->value($raw_value);
55              
56             =cut
57              
58             sub value {
59 33     33 1 138 my $self = shift;
60 33         24 my $token;
61            
62 33 100       58 if (@_) {
63 22         47 $self->_set__raw(shift);
64 22         31 $token = $self->_raw;
65 22         35 $self->_set__output($self->_apply_filters($token));
66             }
67              
68 33         65 return $self->_output;
69             }
70              
71             =head2 target
72              
73             Set target name for target operation:
74              
75             $field->target('name');
76              
77             Get target name:
78              
79             $field->target;
80              
81             =head2 filter
82              
83             Adds a filter to the filter chain:
84            
85             $field->filter($code);
86              
87             Returns field object.
88              
89             =cut
90              
91             sub filter {
92 1     1 1 4 my ($self, $filter) = @_;
93              
94 1 50       3 if (ref($filter) eq 'CODE') {
95 1         2 push @{$self->_filters}, $filter;
  1         2  
96             }
97              
98 1         2 return $self;
99             }
100              
101             sub _apply_filters {
102 22     22   20 my ($self, $token) = @_;
103            
104 22         18 for my $f (@{$self->_filters}) {
  22         40  
105 1         2 $token = $f->($token);
106             }
107              
108 22         45 return $token;
109             }
110              
111             =head1 LICENSE AND COPYRIGHT
112              
113             Copyright 2012-2016 Stefan Hornburg (Racke) .
114              
115             This program is free software; you can redistribute it and/or modify it
116             under the terms of either: the GNU General Public License as published
117             by the Free Software Foundation; or the Artistic License.
118              
119             See http://dev.perl.org/licenses/ for more information.
120              
121             =cut
122              
123             1;