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   1336 use strict;
  8         14  
  8         208  
4 8     8   49 use warnings;
  8         16  
  8         214  
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   10820 use Moo;
  8         96283  
  8         48  
23 8     8   20233 use MooX::Types::MooseLike::Base qw(:all);
  8         48169  
  8         3685  
24 8     8   6355 use namespace::clean;
  8         99447  
  8         44  
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 267 my $self = shift;
60 33         44 my $token;
61            
62 33 100       94 if (@_) {
63 22         74 $self->_set__raw(shift);
64 22         50 $token = $self->_raw;
65 22         105 $self->_set__output($self->_apply_filters($token));
66             }
67              
68 33         139 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 6 my ($self, $filter) = @_;
93              
94 1 50       5 if (ref($filter) eq 'CODE') {
95 1         2 push @{$self->_filters}, $filter;
  1         4  
96             }
97              
98 1         3 return $self;
99             }
100              
101             sub _apply_filters {
102 22     22   40 my ($self, $token) = @_;
103            
104 22         29 for my $f (@{$self->_filters}) {
  22         71  
105 1         4 $token = $f->($token);
106             }
107              
108 22         81 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;