| 1 | package DBIx::Class::Helper::Row::NumifyGet; | ||||||||||||||||||||||||||||
| 2 | 54 | $DBIx::Class::Helper::Row::NumifyGet::VERSION = '2.032002'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Force numeric "context" on numeric columns | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 54 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 54 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 54 | ▶ | use parent 'DBIx::Class::Row'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 54 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub get_column { | ||||||||||||||||||||||||||||
| 13 | 83 | ▶ | my ($self, $col) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_column: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 83 | my $value = $self->next::method($col); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | $value += 0 if defined($value) and # for nullable and autoinc fields | ||||||||||||||||||||||||||||
| 18 | 83 | ▶ | try { $self->_is_column_numeric($col) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
defined $value and &try(sub {
MC/DC50%1/2
defined $value and &try(sub {
$self->_is_column_numeric($col);
}
)
defined $value
&try(sub {
$self->_is_column_numeric($col);
}
)
Truth table66%2/3
A and B
A= defined $valueB= &try(sub {
$self->_is_column_numeric($col);
}
)
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 83 | return $value; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub get_columns { | ||||||||||||||||||||||||||||
| 24 | 5 | ▶ | my ($self, $col) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_columns: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | my %columns = $self->next::method($col); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 5 | for (keys %columns) { | |||||||||||||||||||||||||||
| 29 | $columns{$_} += 0 | ||||||||||||||||||||||||||||
| 30 | if defined($columns{$_}) and # for nullable and autoinc fields | ||||||||||||||||||||||||||||
| 31 | 10 | ▶ | try { $self->_is_column_numeric($_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.8
Branch: 1 branch
Condition33%1/3
defined $columns{$_} and &try(sub {
MC/DC0%0/2
defined $columns{$_} and &try(sub {
$self->_is_column_numeric($_);
}
)
defined $columns{$_}
&try(sub {
$self->_is_column_numeric($_);
}
)
Truth table33%1/3
A and B
A= defined $columns{$_}B= &try(sub {
$self->_is_column_numeric($_);
}
)
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | return %columns; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 54 | 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) 2016 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 | ||||||||||||||||||||||||||||