File Coverage

blib/lib/BSON/Code.pm
Criterion Covered Total %
statement 43 44 97.7
branch 12 14 85.7
condition 5 6 83.3
subroutine 11 12 91.6
pod 2 4 50.0
total 73 80 91.2


line stmt bran cond sub pod time code
1 71     71   29172 use 5.010001;
  71         252  
2 71     71   316 use strict;
  71         109  
  71         1400  
3 71     71   277 use warnings;
  71         117  
  71         2158  
4              
5             package BSON::Code;
6             # ABSTRACT: BSON type wrapper for Javascript code
7              
8 71     71   309 use version;
  71         122  
  71         264  
9             our $VERSION = 'v1.12.2';
10              
11 71     71   5389 use Carp ();
  71         151  
  71         1326  
12 71     71   298 use Tie::IxHash;
  71         265  
  71         1752  
13              
14 71     71   485 use Moo;
  71         140  
  71         387  
15              
16             #pod =attr code
17             #pod
18             #pod A string containing Javascript code. Defaults to the empty string.
19             #pod
20             #pod =attr scope
21             #pod
22             #pod An optional hash reference containing variables in the scope of C.
23             #pod Defaults to C.
24             #pod
25             #pod =cut
26              
27             has [ qw/code scope/ ] => (
28             is => 'ro'
29             );
30              
31 71     71   22561 use namespace::clean -except => 'meta';
  71         134  
  71         433  
32              
33             #pod =method length
34             #pod
35             #pod Returns the length of the C attribute.
36             #pod
37             #pod =cut
38              
39             sub length {
40 0     0 1 0 length( $_[0]->code );
41             }
42              
43             # Support legacy constructor shortcut
44             sub BUILDARGS {
45 18517     18517 0 470858 my ($class) = shift;
46              
47 18517         22744 my %args;
48 18517 100 100     71650 if ( @_ && $_[0] !~ /^[c|s]/ ) {
49 9028         17216 $args{code} = $_[0];
50 9028 100       20290 $args{scope} = $_[1] if defined $_[1];
51             }
52             else {
53 9489 50       21114 Carp::croak( __PACKAGE__ . "::new called with an odd number of elements\n" )
54             unless @_ % 2 == 0;
55 9489         24535 %args = @_;
56             }
57              
58 18517         265438 return \%args;
59             }
60              
61             sub BUILD {
62 18517     18517 0 173268 my ($self) = @_;
63 18517 100       34907 $self->{code} = '' unless defined $self->{code};
64             Carp::croak( __PACKAGE__ . " scope must be hash reference, not $self->{scope}")
65 18517 50 66     57005 if exists($self->{scope}) && ref($self->{scope}) ne 'HASH';
66 18517         77379 return;
67             }
68              
69             #pod =method TO_JSON
70             #pod
71             #pod If the C option is true, returns a hashref compatible with
72             #pod MongoDB's L
73             #pod format, which represents it as a document as follows:
74             #pod
75             #pod {"$code" : ""}
76             #pod {"$code" : "", "$scope" : { ... } }
77             #pod
78             #pod If the C option is false, an error is thrown, as this value
79             #pod can't otherwise be represented in JSON.
80             #pod
81             #pod =cut
82              
83             sub TO_JSON {
84 23     23 1 249 require BSON;
85 23 100       52 if ( $ENV{BSON_EXTJSON} ) {
86 22         30 my %data;
87 22         77 tie( %data, 'Tie::IxHash' );
88 22         302 $data{'$code'} = $_[0]->{code};
89             $data{'$scope'} = BSON->perl_to_extjson($_[0]->{scope})
90 22 100       340 if defined $_[0]->{scope};
91 22         184 return \%data;
92             }
93              
94 1         215 Carp::croak( "The value '$_[0]' is illegal in JSON" );
95             }
96              
97             1;
98              
99             =pod
100              
101             =encoding UTF-8
102              
103             =head1 NAME
104              
105             BSON::Code - BSON type wrapper for Javascript code
106              
107             =head1 VERSION
108              
109             version v1.12.2
110              
111             =head1 SYNOPSIS
112              
113             use BSON::Types ':all';
114              
115             $code = bson_code( $javascript );
116             $code = bson_code( $javascript, $scope );
117              
118             =head1 DESCRIPTION
119              
120             This module provides a BSON type wrapper for the "Javascript code" type
121             and the "Javascript with Scope" BSON types.
122              
123             =head1 ATTRIBUTES
124              
125             =head2 code
126              
127             A string containing Javascript code. Defaults to the empty string.
128              
129             =head2 scope
130              
131             An optional hash reference containing variables in the scope of C.
132             Defaults to C.
133              
134             =head1 METHODS
135              
136             =head2 length
137              
138             Returns the length of the C attribute.
139              
140             =head2 TO_JSON
141              
142             If the C option is true, returns a hashref compatible with
143             MongoDB's L
144             format, which represents it as a document as follows:
145              
146             {"$code" : ""}
147             {"$code" : "", "$scope" : { ... } }
148              
149             If the C option is false, an error is thrown, as this value
150             can't otherwise be represented in JSON.
151              
152             =for Pod::Coverage BUILD BUILDARGS
153              
154             =head1 AUTHORS
155              
156             =over 4
157              
158             =item *
159              
160             David Golden
161              
162             =item *
163              
164             Stefan G.
165              
166             =back
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
171              
172             This is free software, licensed under:
173              
174             The Apache License, Version 2.0, January 2004
175              
176             =cut
177              
178             __END__