File Coverage

blib/lib/Data/Record/Serialize/Role/Default.pm
Criterion Covered Total %
statement 23 23 100.0
branch 1 2 50.0
condition n/a
subroutine 11 11 100.0
pod 1 3 33.3
total 36 39 92.3


line stmt bran cond sub pod time code
1             package Data::Record::Serialize::Role::Default;
2              
3             # ABSTRACT: Default methods for Data::Record::Serialize
4              
5 17     17   9475 use v5.12;
  17         71  
6 17     17   165 use Moo::Role;
  17         41  
  17         107  
7              
8             our $VERSION = '1.05';
9              
10 17     17   17990 use Hash::Util qw[ hv_store ];
  17         53046  
  17         160  
11 17     17   1714 use Ref::Util qw[ is_coderef ];
  17         45  
  17         1114  
12              
13 17     17   118 use Data::Record::Serialize::Error { errors => ['fields'] }, -all;
  17         43  
  17         254  
14              
15 17     17   2616 use namespace::clean;
  17         46  
  17         121  
16              
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36             # provide default if not already defined
37             sub send {
38 38     38 1 218 my $self = shift;
39              
40 38 50       229 $self->_needs_eol
41             ? $self->say( $self->encode( @_ ) )
42             : $self->print( $self->encode( @_ ) );
43             }
44              
45             # just in case they're not defined in preceding roles
46       50 0   sub setup { }
47       108     sub _map_types { }
48 3     3   29 sub _needs_eol { 1 }
49              
50             around 'setup' => sub {
51             my ( $orig, $self, $data ) = @_;
52              
53             # if fields has not been set yet, set it to the names in the data
54             $self->_set_fields( [ keys %$data ] )
55             unless $self->has_fields;
56              
57             # make sure there are no duplicate output fields
58             my %dups;
59             $dups{$_}++ && error( fields => "duplicate output field: $_" ) for @{ $self->fields };
60              
61             if ( $self->has_default_type ) {
62             $self->_set_types_from_default;
63             }
64             else {
65             $self->_set_types_from_record( $data );
66             }
67              
68             # trigger building of output_types, which also remaps types. ick.
69             $self->output_types;
70              
71             $orig->( $self );
72              
73             $self->_set__run_setup( 0 );
74             };
75              
76              
77             before 'send' => sub {
78             my ( $self, $data ) = @_;
79              
80             # can't do format or numify until we have types, which might need to
81             # be done from the data, which will be done in setup.
82              
83             $self->setup( $data )
84             if $self->_run_setup;
85              
86             # remove fields that won't be output
87             delete @{$data}{ grep { !exists $self->_fieldh->{$_} } keys %{$data} };
88              
89             # nullify fields (set to undef) those that are zero length
90              
91             if ( defined( my $fields = $self->_nullified ) ) {
92             $data->{$_} = undef for grep { defined $data->{$_} && !length $data->{$_} } @$fields;
93             }
94              
95             if ( defined( my $fields = $self->_numified ) ) {
96             $data->{$_} = ( $data->{$_} || 0 ) +0 for grep { defined $data->{$_} } @{$fields};
97             }
98              
99             if ( defined( my $fields = $self->_stringified ) ) {
100             $data->{$_} = "@{[ $data->{$_}]}" for grep { defined $data->{$_} } @{$fields};
101             }
102              
103             if ( my $format = $self->_format ) {
104             $data->{$_}
105             = is_coderef( $format->{$_} )
106             ? $format->{$_}( $data->{$_} )
107             : sprintf( $format->{$_}, $data->{$_} )
108             foreach grep { defined $data->{$_} && length $data->{$_} }
109             keys %{$format};
110             }
111              
112              
113             # handle boolean
114             if ( $self->_boolify ) {
115             my @fields = grep { exists $data->{$_} } @{ $self->boolean_fields };
116              
117             if ( $self->_can_bool ) {
118             $data->{$_} = $self->to_bool( $data->{$_} ) for @fields;
119             }
120              
121             # the encoder doesn't have native boolean, must convert a
122             # truthy value to 0/1;
123             else {
124             $data->{$_} = $data->{$_} ? 1 : 0 foreach @fields;
125             }
126             }
127              
128             if ( my $rename = $self->rename_fields ) {
129             for my $from ( @{ $self->fields } ) {
130             my $to = $rename->{$from}
131             or next;
132              
133             hv_store( %$data, $to, $data->{$from} );
134             delete $data->{$from};
135             }
136             }
137             };
138              
139             sub DEMOLISH {
140 56     56 0 167204 my ( $self, $in_global_destruction ) = @_;
141              
142             # we can't make the decision about whether to pay attention during
143             # Global Destruction. the objects have to do that
144 56         344 $self->close( $in_global_destruction );
145 56         959 return;
146             }
147              
148             1;
149              
150             #
151             # This file is part of Data-Record-Serialize
152             #
153             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
154             #
155             # This is free software, licensed under:
156             #
157             # The GNU General Public License, Version 3, June 2007
158             #
159              
160             __END__
161              
162             =pod
163              
164             =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory
165              
166             =head1 NAME
167              
168             Data::Record::Serialize::Role::Default - Default methods for Data::Record::Serialize
169              
170             =head1 VERSION
171              
172             version 1.05
173              
174             =head1 DESCRIPTION
175              
176             C<Data::Record::Serialize::Role::Default> provides default methods for
177             L<Data::Record::Serialize>. It is applied after all of the other roles to
178             ensure that other roles' methods have priority.
179              
180             =head1 METHODS
181              
182             =head2 B<send>
183              
184             $s->send( \%record );
185              
186             Encode and send the record to the associated sink.
187              
188             B<WARNING>: the passed hash is modified. If you need the original
189             contents, pass in a copy.
190              
191             =head1 INTERNALS
192              
193             =for Pod::Coverage cleanup
194             send
195             setup
196             DEMOLISH
197              
198             =head1 SUPPORT
199              
200             =head2 Bugs
201              
202             Please report any bugs or feature requests to bug-data-record-serialize@rt.cpan.org or through the web interface at: L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Record-Serialize>
203              
204             =head2 Source
205              
206             Source is available at
207              
208             https://gitlab.com/djerius/data-record-serialize
209              
210             and may be cloned from
211              
212             https://gitlab.com/djerius/data-record-serialize.git
213              
214             =head1 SEE ALSO
215              
216             Please see those modules/websites for more information related to this module.
217              
218             =over 4
219              
220             =item *
221              
222             L<Data::Record::Serialize|Data::Record::Serialize>
223              
224             =back
225              
226             =head1 AUTHOR
227              
228             Diab Jerius <djerius@cpan.org>
229              
230             =head1 COPYRIGHT AND LICENSE
231              
232             This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
233              
234             This is free software, licensed under:
235              
236             The GNU General Public License, Version 3, June 2007
237              
238             =cut