File Coverage

blib/lib/BSON/DBRef.pm
Criterion Covered Total %
statement 40 41 97.5
branch 7 8 87.5
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 58 60 96.6


line stmt bran cond sub pod time code
1 71     71   25721 use 5.010001;
  71         229  
2 71     71   325 use strict;
  71         117  
  71         1396  
3 71     71   331 use warnings;
  71         116  
  71         2182  
4              
5             package BSON::DBRef;
6             # ABSTRACT: BSON type wrapper for MongoDB DBRefs
7              
8 71     71   336 use version;
  71         121  
  71         274  
9             our $VERSION = 'v1.12.0';
10              
11 71     71   5223 use Tie::IxHash;
  71         193  
  71         1825  
12 71     71   341 use Moo 2.002004;
  71         929  
  71         346  
13 71     71   19643 use namespace::clean -except => 'meta';
  71         173  
  71         601  
14              
15 71     71   17595 use BSON ();
  71         121  
  71         43357  
16              
17             #pod =attr id
18             #pod
19             #pod Required. The C<_id> value of the referenced document. If the
20             #pod C<_id> is an ObjectID, then you must use a L object.
21             #pod
22             #pod =cut
23              
24             # no type constraint since an _id can be anything
25             has id => (
26             is => 'ro',
27             required => 1
28             );
29              
30             #pod =attr ref
31             #pod
32             #pod Required. The name of the collection in which the referenced document
33             #pod lives. Either a L object or a string containing the
34             #pod collection name. The object will be coerced to string form.
35             #pod
36             #pod This may also be specified in the constructor as C<'$ref'>.
37             #pod
38             #pod =cut
39              
40             has 'ref' => (
41             is => 'ro',
42             required => 1,
43             coerce => sub { CORE::ref($_[0]) eq 'MongoDB::Collection' ? $_[0]->name : $_[0] },
44             isa => sub { die "must be a non-empty string" unless defined($_[0]) && length($_[0]) },
45             );
46              
47             #pod =attr db
48             #pod
49             #pod Optional. The database in which the referenced document lives. Either a
50             #pod L object or a string containing the database name. The
51             #pod object will be coerced to string form.
52             #pod
53             #pod Not all other language drivers support the C<$db> field, so using this
54             #pod field is not recommended.
55             #pod
56             #pod This may also be specified in the constructor as C<'$db'>.
57             #pod
58             #pod =cut
59              
60             has db => (
61             is => 'ro',
62             coerce => sub { CORE::ref($_[0]) eq 'MongoDB::DataBase' ? $_[0]->name : $_[0] },
63             isa => sub { return if ! defined($_[0]); die "must be a non-empty string" unless length($_[0]) },
64             );
65              
66             #pod =attr extra
67             #pod
68             #pod Optional. A hash reference of additional fields in the DBRef document.
69             #pod Not all MongoDB drivers support this feature and you B rely on
70             #pod it. This attribute exists solely to ensure DBRefs generated by drivers that
71             #pod do allow extra fields will round-trip correctly.
72             #pod
73             #pod B
74             #pod
75             #pod =cut
76              
77             has extra => (
78             is => 'ro',
79             isa => sub { return if ! defined($_[0]); die "must be a hashref" unless eval { scalar %{$_[0]}; 1 } },
80             default => sub { {} },
81             );
82              
83             around BUILDARGS => sub {
84             my $orig = shift;
85             my $class = shift;
86             my $hr = $class->$orig(@_);
87             return {
88             id => (
89             exists( $hr->{'$id'} ) ? delete $hr->{'$id'}
90             : exists( $hr->{id} ) ? delete $hr->{id}
91             : undef
92             ),
93             'ref' => (
94             exists( $hr->{'$ref'} ) ? delete $hr->{'$ref'}
95             : exists( $hr->{'ref'}) ? delete $hr->{'ref'}
96             : undef
97             ),
98             db => (
99             exists( $hr->{'$db'} ) ? delete $hr->{'$db'}
100             : exists( $hr->{db} ) ? delete $hr->{db}
101             : undef
102             ),
103             extra => $hr,
104             };
105             };
106              
107             sub _ordered {
108 15     15   26 my $self = shift;
109              
110             return Tie::IxHash->new(
111             '$ref' => $self->ref,
112             '$id' => $self->id,
113             ( defined($self->db) ? ( '$db' => $self->db ) : () ),
114 15 100       80 %{ $self->extra },
  15         91  
115             );
116             }
117              
118             #pod =method TO_JSON
119             #pod
120             #pod If the C option is true, returns a hashref compatible with
121             #pod MongoDB's L
122             #pod format, which represents it as a document as follows:
123             #pod
124             #pod { "$ref": "", "$id": "" }
125             #pod
126             #pod If the C option is false, an error is thrown, as this value
127             #pod can't otherwise be represented in JSON.
128             #pod
129             #pod =cut
130              
131             sub TO_JSON {
132 13     13 1 24 my $self = shift;
133              
134 13 50       38 if ( $ENV{BSON_EXTJSON} ) {
135 13         30 my $id = $self->id;
136              
137 13 100       40 if (ref $id) {
138 12         39 $id = $id->TO_JSON;
139             }
140             else {
141 1         6 $id = BSON->perl_to_extjson($id);
142             }
143              
144 13         20 my %data;
145 13         41 tie( %data, 'Tie::IxHash' );
146 13         194 $data{'$ref'} = $self->ref;
147 13         167 $data{'$id'} = $id;
148 13 100       167 $data{'$db'} = $self->db
149             if defined $self->db;
150              
151 13         87 my $extra = $self->extra;
152             $data{$_} = $extra->{$_}
153 13         64 for keys %$extra;
154              
155 13         73 return \%data;
156             }
157              
158 0           Carp::croak( "The value '$self' is illegal in JSON" );
159             }
160              
161             1;
162              
163             =pod
164              
165             =encoding UTF-8
166              
167             =head1 NAME
168              
169             BSON::DBRef - BSON type wrapper for MongoDB DBRefs
170              
171             =head1 VERSION
172              
173             version v1.12.0
174              
175             =head1 SYNOPSIS
176              
177             use BSON::Types ':all';
178              
179             my $dbref = bson_dbref( $oid, $collection_name );
180              
181             =head1 DESCRIPTION
182              
183             This module provides a BSON type wrapper for L
184             References|http://docs.mongodb.org/manual/reference/database-references/>.
185              
186             A DBRef is a special document format which references another document in
187             the database. DBRefs are not the same as foreign keys and do not provide
188             any referential integrity or constraint checking. For example, a DBRef may
189             point to a document that no longer exists (or never existed.)
190              
191             Use of DBRefs is discouraged, so this module is provided for backwards
192             compatibility. L<"Manual
193             references"|https://docs.mongodb.com/manual/reference/database-references/#document-references>
194             are preferred when there is a need to reference other documents.
195              
196             =head1 ATTRIBUTES
197              
198             =head2 id
199              
200             Required. The C<_id> value of the referenced document. If the
201             C<_id> is an ObjectID, then you must use a L object.
202              
203             =head2 ref
204              
205             Required. The name of the collection in which the referenced document
206             lives. Either a L object or a string containing the
207             collection name. The object will be coerced to string form.
208              
209             This may also be specified in the constructor as C<'$ref'>.
210              
211             =head2 db
212              
213             Optional. The database in which the referenced document lives. Either a
214             L object or a string containing the database name. The
215             object will be coerced to string form.
216              
217             Not all other language drivers support the C<$db> field, so using this
218             field is not recommended.
219              
220             This may also be specified in the constructor as C<'$db'>.
221              
222             =head2 extra
223              
224             Optional. A hash reference of additional fields in the DBRef document.
225             Not all MongoDB drivers support this feature and you B rely on
226             it. This attribute exists solely to ensure DBRefs generated by drivers that
227             do allow extra fields will round-trip correctly.
228              
229             B
230              
231             =head1 METHODS
232              
233             =head2 TO_JSON
234              
235             If the C option is true, returns a hashref compatible with
236             MongoDB's L
237             format, which represents it as a document as follows:
238              
239             { "$ref": "", "$id": "" }
240              
241             If the C option is false, an error is thrown, as this value
242             can't otherwise be represented in JSON.
243              
244             =for Pod::Coverage BUILDARGS
245              
246             =head1 AUTHORS
247              
248             =over 4
249              
250             =item *
251              
252             David Golden
253              
254             =item *
255              
256             Stefan G.
257              
258             =back
259              
260             =head1 COPYRIGHT AND LICENSE
261              
262             This software is Copyright (c) 2019 by Stefan G. and MongoDB, Inc.
263              
264             This is free software, licensed under:
265              
266             The Apache License, Version 2.0, January 2004
267              
268             =cut
269              
270             __END__