File Coverage

blib/lib/BSON/Int32.pm
Criterion Covered Total %
statement 33 57 57.8
branch 6 34 17.6
condition 6 6 100.0
subroutine 11 20 55.0
pod 1 2 50.0
total 57 119 47.9


line stmt bran cond sub pod time code
1 71     71   28448 use 5.010001;
  71         250  
2 71     71   368 use strict;
  71         132  
  71         1472  
3 71     71   329 use warnings;
  71         136  
  71         3053  
4              
5             package BSON::Int32;
6             # ABSTRACT: BSON type wrapper for Int32
7              
8 71     71   436 use version;
  71         127  
  71         326  
9             our $VERSION = 'v1.12.1';
10              
11 71     71   5057 use Carp;
  71         151  
  71         3983  
12 71     71   447 use Moo;
  71         182  
  71         444  
13              
14             #pod =attr value
15             #pod
16             #pod A numeric scalar. It will be coerced to an integer. The default is 0.
17             #pod
18             #pod =cut
19              
20             has 'value' => (
21             is => 'ro'
22             );
23              
24 71     71   24015 use namespace::clean -except => 'meta';
  71         155  
  71         450  
25              
26             my $max_int32 = 2147483647;
27             my $min_int32 = -2147483648;
28              
29             sub BUILD {
30 91     91 0 13877 my $self = shift;
31             # coerce to IV internally
32 91 100       301 $self->{value} = defined( $self->{value} ) ? int( $self->{value} ) : 0;
33 91 100 100     826 if ( $self->{value} > $max_int32 || $self->{value} < $min_int32 ) {
34 4         648 croak("The value '$self->{value}' can't fit in a signed Int32");
35             }
36             }
37              
38             #pod =method TO_JSON
39             #pod
40             #pod Returns the value as an integer.
41             #pod
42             #pod If the C environment variable is true and the
43             #pod C environment variable is false, returns a hashref
44             #pod compatible with
45             #pod MongoDB's L
46             #pod format, which represents it as a document as follows:
47             #pod
48             #pod {"$numberInt" : "42"}
49             #pod
50             #pod =cut
51              
52             sub TO_JSON {
53 40 100 100 40 1 359 return int($_[0]->{value}) if ! $ENV{BSON_EXTJSON} || $ENV{BSON_EXTJSON_RELAXED};
54 33         118 return { '$numberInt' => "$_[0]->{value}" };
55             }
56              
57             use overload (
58             # Unary
59 0     0   0 q{""} => sub { "$_[0]->{value}" },
60 6     6   1772 q{0+} => sub { $_[0]->{value} },
61 0     0   0 q{~} => sub { ~( $_[0]->{value} ) },
62             # Binary
63 142     0   11788 ( map { $_ => eval "sub { return \$_[0]->{value} $_ \$_[1] }" } qw( + * ) ), ## no critic
  0     0      
  0            
64             (
65             map {
66 852 0   0   68509 $_ => eval ## no critic
  0 0   0      
  0 0   0      
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0            
67             "sub { return \$_[2] ? \$_[1] $_ \$_[0]->{value} : \$_[0]->{value} $_ \$_[1] }"
68             } qw( - / % ** << >> x <=> cmp & | ^ )
69             ),
70             (
71 426     0   27309 map { $_ => eval "sub { return $_(\$_[0]->{value}) }" } ## no critic
  0            
  0            
  0            
  0            
  0            
  0            
72             qw( cos sin exp log sqrt int )
73             ),
74             q{atan2} => sub {
75 0 0   0   0 return $_[2] ? atan2( $_[1], $_[0]->{value} ) : atan2( $_[0]->{value}, $_[1] );
76             },
77              
78             # Special
79 71         633 fallback => 1,
80 71     71   50054 );
  71         162  
81              
82             1;
83              
84             =pod
85              
86             =encoding UTF-8
87              
88             =head1 NAME
89              
90             BSON::Int32 - BSON type wrapper for Int32
91              
92             =head1 VERSION
93              
94             version v1.12.1
95              
96             =head1 SYNOPSIS
97              
98             use BSON::Types ':all';
99              
100             bson_int32( $number );
101              
102             =head1 DESCRIPTION
103              
104             This module provides a BSON type wrapper for a numeric value that
105             would be represented in BSON as a 32-bit integer.
106              
107             If the value won't fit in a 32-bit integer, an error will be thrown.
108              
109             =head1 ATTRIBUTES
110              
111             =head2 value
112              
113             A numeric scalar. It will be coerced to an integer. The default is 0.
114              
115             =head1 METHODS
116              
117             =head2 TO_JSON
118              
119             Returns the value as an integer.
120              
121             If the C environment variable is true and the
122             C environment variable is false, returns a hashref
123             compatible with
124             MongoDB's L
125             format, which represents it as a document as follows:
126              
127             {"$numberInt" : "42"}
128              
129             =for Pod::Coverage BUILD
130              
131             =head1 OVERLOADING
132              
133             The numification operator, C<0+> is overloaded to return the C,
134             the full "minimal set" of overloaded operations is provided (per L
135             documentation) and fallback overloading is enabled.
136              
137             =head1 AUTHORS
138              
139             =over 4
140              
141             =item *
142              
143             David Golden
144              
145             =item *
146              
147             Stefan G.
148              
149             =back
150              
151             =head1 COPYRIGHT AND LICENSE
152              
153             This software is Copyright (c) 2019 by Stefan G. and MongoDB, Inc.
154              
155             This is free software, licensed under:
156              
157             The Apache License, Version 2.0, January 2004
158              
159             =cut
160              
161             __END__