Mango/BSON/ObjectID.pm

filter: Statementstmt 96.8% 31 / 32 Branchbran 100% 4 / 4 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 91.9% 57 / 62 CC 5 SCAR 4.7
CC
5
Cov
95%
new
3.0
to_bytes
2.1
_generate
2.0
CRAP 1.6
« Mango/BSON/Document.pm ↑ Summary Mango/BSON/Time.pm »
1 package Mango::BSON::ObjectID;
2 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
4  
5 11 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Mojo::Util 'md5_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Sys::Hostname 'hostname';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # 3 byte machine identifier
10 11 my $MACHINE = substr md5_bytes(hostname), 0, 3;
11  
12 # Global counter
13 11 my $COUNTER = int(rand(0xffffff));
14  
15 2 ▶sub from_epoch {
Subroutine from_epoch: called CC=1 SCAR=0.0
Pod documented
16 2 my ($self, $epoch) = @_;
17 2 $self->{oid} = _generate($epoch);
18 2 return $self;
19 }
20  
21 13 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
22 13 my ($class, $oid) = @_;
23 13 ▶ return $class->SUPER::new unless defined $oid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oid 3 10 13
24 10 ▶ croak qq{Invalid object id "$oid"} if $oid !~ /^[0-9a-fA-F]{24}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $oid =~ /^[0-9a-fA-F]{24}\z/u 4 6 10
25 6 return $class->SUPER::new(oid => pack('H*', $oid));
26 }
27  
28 13 ▶sub to_bytes { shift->{oid} //= _generate() }
Subroutine to_bytes: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
+(shift())->{"oid"} //= _generate()
l!l&&r!l&&!r
1210
MC/DC0%0/2
+(shift())->{"oid"} //= _generate()
+(shift())->{"oid"} _generate()
Truth table66%2/3
A //= B
A= +(shift())->{"oid"}B= _generate()
ABresult
1X1
011
000
29  
30 3 ▶sub to_epoch { unpack 'N', substr(shift->to_bytes, 0, 4) }
Subroutine to_epoch: called CC=1 SCAR=0.0
Pod documented
31  
32 8 ▶sub to_string { unpack 'H*', shift->to_bytes }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
33  
34 3 ▶sub _generate {
Subroutine _generate: called CC=2 SCAR=6.9
35  
36 3 $COUNTER = ($COUNTER + 1) % 0xffffff;
37  
38 3 ▶ return pack('N', shift // time) # 4 byte time
Condition100%2/2
shift() // time
l!l
21
MC/DC100%1/1
shift() // time
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
39 . $MACHINE # 3 byte machine identifier
40 . pack('n', $$ % 0xffff) # 2 byte process id
41 . substr pack('V', $COUNTER), 0, 3; # 3 byte counter
42 }
43  
44 11 1;
45  
46 =encoding utf8
47  
48 =head1 NAME
49  
50 Mango::BSON::ObjectID - Object ID type
51  
52 =head1 SYNOPSIS
53  
54 use Mango::BSON::ObjectID;
55  
56 my $oid = Mango::BSON::ObjectID->new('1a2b3c4e5f60718293a4b5c6');
57 say $oid->to_epoch;
58  
59 =head1 DESCRIPTION
60  
61 L<Mango::BSON::ObjectID> is a container for the BSON object id type used by
62 L<Mango::BSON>.
63  
64 =head1 METHODS
65  
66 L<Mango::BSON::ObjectID> inherits all methods from L<Mojo::Base> and
67 implements the following new ones.
68  
69 =head2 from_epoch
70  
71 my $oid = $oid->from_epoch(1359840145);
72  
73 Generate new object id with specific epoch time.
74  
75 =head2 new
76  
77 my $oid = Mango::BSON::ObjectID->new;
78 my $oid = Mango::BSON::ObjectID->new('1a2b3c4e5f60718293a4b5c6');
79  
80 Construct a new L<Mango::BSON::ObjectID> object.
81  
82 =head2 to_bytes
83  
84 my $bytes = $oid->to_bytes;
85  
86 Object id in binary form.
87  
88 =head2 to_epoch
89  
90 my $epoch = $oid->to_epoch;
91  
92 Extract epoch seconds from object id.
93  
94 =head2 to_string
95  
96 my $str = $oid->to_string;
97  
98 Stringify object id.
99  
100 =head1 OPERATORS
101  
102 L<Mango::BSON::ObjectID> overloads the following operators.
103  
104 =head2 bool
105  
106 my $bool = !!$oid;
107  
108 Always true.
109  
110 =head2 stringify
111  
112 my $str = "$oid";
113  
114 Alias for L</to_string>.
115  
116 =head1 SEE ALSO
117  
118 L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
119  
120 =cut
« Mango/BSON/Document.pm ↑ Summary Mango/BSON/Time.pm »