File Coverage

blib/lib/DBIx/Class/Helper/Row/NumifyGet.pm
Criterion Covered Total %
statement 23 23 100.0
branch 3 4 75.0
condition 3 6 50.0
subroutine 8 8 100.0
pod 2 2 100.0
total 39 43 90.7


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Row::NumifyGet;
2             $DBIx::Class::Helper::Row::NumifyGet::VERSION = '2.034002';
3             # ABSTRACT: Force numeric "context" on numeric columns
4              
5 55     55   157063 use strict;
  55         117  
  55         1527  
6 55     55   245 use warnings;
  55         93  
  55         1326  
7              
8 55     55   250 use parent 'DBIx::Class::Row';
  55         96  
  55         323  
9              
10 55     55   2952 use Try::Tiny;
  55         102  
  55         12434  
11              
12             sub get_column {
13 83     83 1 7384 my ($self, $col) = @_;
14              
15 83         229 my $value = $self->next::method($col);
16              
17             $value += 0 if defined($value) and # for nullable and autoinc fields
18 83 100 66 83   1369 try { $self->_is_column_numeric($col) };
  83         1810  
19              
20 83         3663 return $value;
21             }
22              
23             sub get_columns {
24 5     5 1 34 my ($self, $col) = @_;
25              
26 5         15 my %columns = $self->next::method($col);
27              
28 5         78 for (keys %columns) {
29             $columns{$_} += 0
30             if defined($columns{$_}) and # for nullable and autoinc fields
31 10 50 33 10   190 try { $self->_is_column_numeric($_) };
  10         180  
32             }
33              
34 5         160 return %columns;
35             }
36              
37             1;
38              
39             __END__
40              
41             =pod
42              
43             =head1 NAME
44              
45             DBIx::Class::Helper::Row::NumifyGet - Force numeric "context" on numeric columns
46              
47             =head1 SYNOPSIS
48              
49             package MyApp::Schema::Result::Foo_Bar;
50              
51             __PACKAGE__->load_components(qw{Helper::Row::NumifyGet Core});
52              
53             __PACKAGE__->table('Foo');
54             __PACKAGE__->add_columns(
55             foo => {
56             data_type => 'integer',
57             is_nullable => 0,
58             is_numeric => 1,
59             },
60             );
61              
62             sub TO_JSON {
63             return {
64             foo => $self->foo, # this becomes 0 instead of "0" due to context
65             }
66             }
67              
68             =head1 METHODS
69              
70             =head2 get_column
71              
72             This is the method that "converts" the values. It just checks for
73             C<is_numeric> and if that is true it will numify the value.
74              
75             =head2 get_columns
76              
77             This method also "converts" values, but this one is called a lot more rarely.
78             Again, It just checks for C<is_numeric> and if that is true it will numify the
79             value.
80              
81             =head1 AUTHOR
82              
83             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
88              
89             This is free software; you can redistribute it and/or modify it under
90             the same terms as the Perl 5 programming language system itself.
91              
92             =cut