| 1 | package Bb::Collaborate::Ultra::DAO; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use warnings; use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use Mouse; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use parent qw{Class::Data::Inheritable}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Bb::Collaborate::Ultra::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Data::Compare; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Clone; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use 5.008003; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | __PACKAGE__->mk_classdata('_types'); | |||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | __PACKAGE__->mk_classdata('_db_data'); | |||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | __PACKAGE__->mk_classdata('resource'); | |||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | __PACKAGE__->mk_classdata('_query_params' => { | |||||||||||||||||||||||||||||||||||||||||
| 17 | limit => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | offset => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | fields => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | has '_connection' => ('is' => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | has '_parent' => ('is' => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | our %enums; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | L<Bb::Collaborate::Ultra::DAO> is an abstract base class for various resource classes (e.g. L<Bb::Collaborate::Ultra::Session>) and contains both builder and inherited methods from implementing these classes. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 ABSTRACT METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | The following methods are inherited from this class. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | Creates a new object. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head2 post | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Creates object on the server. E.g. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | my $start = time() + 60; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | my $end = $start + 900; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | my $session = Bb::Collaborate::Ultra::Session->post($connection, { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | name => 'Test Session', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | startTime => $start, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | endTime => $end, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub post { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $connection = shift; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | die 'usage: '.$class.'->post($connection, $data)' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$connection and $data and $connection->can("POST")
$connection and $data
MC/DC0%0/3
$connection and $data and $connection->can("POST")
$connection
$data
$connection->can("POST")
Truth table0%0/4
A and B and C
A= $connectionB= $dataC= $connection->can("POST")
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | unless $connection && $data && $connection->can('POST'); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $json = $class->_freeze($data); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $path = $opt{path} || $class->path | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt{'path'} || $class->path
MC/DC0%0/2
$opt{'path'} || $class->path
$opt{'path'}
$class->path
Truth table0%0/3
A || B
A= $opt{'path'}B= $class->path
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | or die "no POST path"; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $msg = $connection->POST($path, $json, @_); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $class->construct($msg, connection => $connection); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =head2 patch | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | Updates an existing object | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | $session->name('Test Session - Updated'); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | $session->endTime($session->endTime + 60); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | $session->patch; # enact updates | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | sub patch { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
patch: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $connection = shift || $self->connection | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
shift() || $self->connection || die('no connected')
shift() or $self->connection
MC/DC0%0/2
shift() || $self->connection || die('no connected')
shift()
$self->connection
Truth table0%0/3
A or B
A= shift()B= $self->connection
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | || die "no connected"; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $update_data = shift || $self->_pending_updates; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
shift() || $self->_pending_updates
MC/DC0%0/2
shift() || $self->_pending_updates
shift()
$self->_pending_updates
Truth table0%0/3
A || B
A= shift()B= $self->_pending_updates
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $path = $self->path; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $json = $class->_freeze($update_data); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $msg = $connection->PATCH($path, $json); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $obj = $self->construct($msg, connection => $connection); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $self->_db_data( $obj->_db_data ); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $obj->parent($self->parent); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $obj; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =head2 get | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | Fetches one or more objects from the server. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | my @future_sessions = Bb::Collaborate::Ultra::Session->get($connection, {endTime => time(), limit => 50}, ) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | sub get { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $connection = shift; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $query_data = shift // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() // {}
MC/DC0%0/1
shift() // {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | die 'usage: '.$class.'->get($connection, [$query_data], %opt)' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$connection and $connection->can("GET")
MC/DC0%0/2
$connection and $connection->can("GET")
$connection
$connection->can("GET")
Truth table0%0/3
A and B
A= $connectionB= $connection->can("GET")
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | unless $connection && $connection->can('GET'); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $path = $opt{path}; | |||||||||||||||||||||||||||||||||||||||||
| 117 | $path ||= $query_data->{id} | ||||||||||||||||||||||||||||||||||||||||||
| 118 | ? $class->resource . '/' . $query_data->{id} | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | : $class->resource; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$path ||= $query_data->{"id"} ? $class->resource . '/' . $query_data->{"id"} : $class->resource
MC/DC0%0/2
$path ||= $query_data->{"id"} ? $class->resource . '/' . $query_data->{"id"} : $class->resource
$path
$query_data->{"id"} ? $class->resource . '/' . $query_data->{"id"} : $class->resource
Truth table0%0/3
A ||= B
A= $pathB= $query_data->{"id"} ? $class->resource . '/' . $query_data->{"id"} : $class->resource
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (keys %$query_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $path .= $connection->client->buildQuery($class->TO_JSON($query_data)); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $msg = $connection->GET($path); | |||||||||||||||||||||||||||||||||||||||||
| 124 | $msg->{results} | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ? map { $class->construct($_, connection => $connection, parent => $opt{parent}) } @{ $msg->{results} } | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | : $class->construct($msg, connection => $connection, parent => $opt{parent}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =head2 delete | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Deletes an object from the server | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | $session->delete; | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | sub delete { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my $connection = shift | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
shift() || $self->connection || die('Not connected')
shift() or $self->connection
MC/DC0%0/2
shift() || $self->connection || die('Not connected')
shift()
$self->connection
Truth table0%0/3
A or B
A= shift()B= $self->connection
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | || $self->connection | ||||||||||||||||||||||||||||||||||||||||||
| 141 | || die 'Not connected'; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $data = shift || {id => $self->id}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {'id', $self->id}
MC/DC0%0/1
shift() || {'id', $self->id}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my $path = $self->resource; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $connection->DELETE($path, $data); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head2 find_or_create | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Attempts a C<get> on the object. If that fails, creates an new object on the server. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | sub find_or_create { | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_or_create: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my $connection = shift; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my $param_types = $class->query_params; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my $prop_types = $class->_property_types; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my %query; | |||||||||||||||||||||||||||||||||||||||||
| 161 | my %body; | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | for my $fld (keys %$data) { | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $val = $data->{$fld}; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if (exists $param_types->{$fld}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $query{$fld} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | elsif (exists $prop_types->{$fld}) { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | $body{$fld} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | warn "$class: ignoring unknown field: $fld"; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my @recs = $class->get($connection, \%query); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $rec; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if (@recs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | warn "$class: ambiguous find_or_create query: @{[ keys %query ]}\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | if @recs > 1; | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | $rec = $recs[0]; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | for (keys %body) { | |||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | $rec->$_($body{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | $rec = $class->post($connection => $data); | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | $rec; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =head2 path | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | Computes a RESTful resource path for the object. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | sub path { | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
path: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my $parent = $opt{parent}; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | $parent ||= $self->parent | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$parent ||= $self->parent
MC/DC0%0/2
$parent ||= $self->parent
$parent
$self->parent
Truth table0%0/3
A ||= B
A= $parentB= $self->parent
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | if ref($self); | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $path = ''; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | $path .= $parent->path . '/' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | if $parent; | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | $path .= $self->resource; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | my $id = ref $self && $self->id; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $self && $self->id
MC/DC0%0/2
ref $self && $self->id
ref $self
$self->id
Truth table0%0/3
A && B
A= ref $selfB= $self->id
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | $path .= '/' . $id if $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $path; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | =head2 parent | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | Returns any parent class for the object. May be used to compute the path. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | sub parent { shift->_parent(@_)} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parent: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | =head2 changed | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | Returns a list of fields that have been updated since the | ||||||||||||||||||||||||||||||||||||||||||
| 223 | object was last saved via a `patch`, or `post`, or fetched | ||||||||||||||||||||||||||||||||||||||||||
| 224 | via a `get`. | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | sub changed { | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
changed: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my @changed; | |||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | if (my $old_data = $self->_db_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | my $types = $self->_property_types; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | my $data = $self->_raw_data; | |||||||||||||||||||||||||||||||||||||||||
| 235 | # include only key and changed data | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | for my $fld (sort keys %$data) { | |||||||||||||||||||||||||||||||||||||||||
| 237 | # ignore time-stamps | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | next if $fld =~ /^(id|modified|created)$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my $new_val = $data->{$fld}; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | my $old_val = $old_data->{$fld}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | push @changed, $fld | ||||||||||||||||||||||||||||||||||||||||||
| 242 | if !defined($old_val) | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | || $self->_compare($types->{$fld}, $old_val, $new_val); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $old_val or $self->_compare($types->{$fld}, $old_val, $new_val)
MC/DC0%0/2
not defined $old_val or $self->_compare($types->{$fld}, $old_val, $new_val)
not defined $old_val
$self->_compare($types->{$fld}, $old_val, $new_val)
Truth table0%0/3
not(A) or B
A= not defined $old_valB= $self->_compare($types->{$fld}, $old_val, $new_val)
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | @changed; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | sub _compare { | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compare: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my $v1 = shift; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $v2 = shift; | |||||||||||||||||||||||||||||||||||||||||
| 254 | $type eq 'Bool' | ||||||||||||||||||||||||||||||||||||||||||
| 255 | ? ($v1? 1: 0) != ($v2? 1 : 0) | ||||||||||||||||||||||||||||||||||||||||||
| 256 | : ($type eq 'Date' | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | ? do { abs($v1 - $v2) > 1 } # allow for rounding | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | : !Compare($v1, $v2)); | ||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | sub _pending_updates { | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pending_updates: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | my $data = $self->_raw_data; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my %pending; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | @pending{ $self->changed } = undef; | |||||||||||||||||||||||||||||||||||||||||
| 266 | # pass the primary key | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $pending{id} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my %updates = map { $_ => $data->{$_} } (sort keys %pending); | |||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | \%updates; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =head2 connection | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | Returns the connection associated with the object. Will be set if | ||||||||||||||||||||||||||||||||||||||||||
| 275 | the object has been fetched via a `get`, added via a `post` or updated via a `patch`. | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | sub connection { shift->_connection(@_)} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connection: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | =head1 Internal METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | =head2 query_params | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | __PACKAGE__->query_params( | ||||||||||||||||||||||||||||||||||||||||||
| 289 | name => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 290 | extId => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 291 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | This is used to specify any additional payload fields that may be | ||||||||||||||||||||||||||||||||||||||||||
| 294 | passed as query parameters, or returned along with object data. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | sub query_params { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 16 | ▶ | my ($entity_class, %params) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
query_params: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 16 | for (keys %params) { | |||||||||||||||||||||||||||||||||||||||||
| 302 | 42 | $entity_class->_query_params->{$_} = $params{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 16 | return $entity_class->_query_params; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | sub _property_types { | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 15 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_property_types: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 15 | my $types = $class->_types; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 15 | ▶ | unless ($types) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 4 | my $meta = $class->meta; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 4 | my @atts = grep { $_ !~ /^_/ } ($meta->get_attribute_list); | |||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | $types = { | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 4 | map {$_ => $meta->get_attribute($_)->{type_constraint}} @atts | |||||||||||||||||||||||||||||||||||||||||
| 317 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 4 | $class->_types($types); | |||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 15 | $types; | |||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =head2 freeze | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | Serializes an object to JSON., with data conversion. | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | =item Dates are converted from numeric Unix timestamps to date-strings | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =item Booleans are converted from numeric (0, 1) to 'true', or 'false'. | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | =item Nested objects are recursively serialized. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | sub _freeze { | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_freeze: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 3 | my $frozen = $self->TO_JSON(@_); | |||||||||||||||||||||||||||||||||||||||||
| 342 | 3 | to_json $frozen, { convert_blessed => 1}; | |||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | sub _raw_data { | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_raw_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 5 | my $types = $self->_property_types; | |||||||||||||||||||||||||||||||||||||||||
| 348 | 18 | my %data = (map { $_ => $self->$_ } | |||||||||||||||||||||||||||||||||||||||||
| 349 | 5 | grep { defined $self->$_ } | |||||||||||||||||||||||||||||||||||||||||
| 350 | (keys %$types)); | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 5 | \%data; | |||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | sub TO_JSON { | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=7 SCAR=24.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 5 | my $prop_types = $self->_property_types; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 5 | my $param_types = $self->query_params; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 5 | ▶ | my $data = shift || $self->_raw_data; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->_raw_data
MC/DC0%0/2
shift() || $self->_raw_data
shift()
$self->_raw_data
Truth table33%1/3
A || B
A= shift()B= $self->_raw_data
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 5 | my %frozen; | |||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 5 | for my $fld (keys %$data) { | |||||||||||||||||||||||||||||||||||||||||
| 363 | 18 | ▶ | my $type = $prop_types->{$fld} || $param_types->{$fld} || do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$prop_types->{$fld} || $param_types->{$fld} || do {
warn +(ref $self || $self) . ": unknown field/query-parameter: $fld";
'Str'
}
$prop_types->{$fld} or $param_types->{$fld}
MC/DC0%0/3
$prop_types->{$fld} || $param_types->{$fld} || do {
warn +(ref $self || $self) . ": unknown field/query-parameter: $fld";
'Str'
}
$prop_types->{$fld}
$param_types->{$fld}
do {
warn +(ref $self || $self) . ": unknown field/query-parameter: $fld";
'Str'
}
Truth table25%1/4
A or B || C
A= $prop_types->{$fld}B= $param_types->{$fld}C= do {
warn +(ref $self || $self) . ": unknown field/query-parameter: $fld";
'Str'
}
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | warn((ref($self) || $self).": unknown field/query-parameter: $fld"); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | 'Str' | |||||||||||||||||||||||||||||||||||||||||
| 366 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 18 | my $val = $data->{$fld}; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 18 | ▶ | $frozen{$fld} = Bb::Collaborate::Ultra::Util::_freeze($val, $type) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | if defined $val; | ||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 5 | \%frozen; | |||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | =head2 thaw | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | The reverse of `freeze`. Deserializes JSON data to objects, with conversion of dates, boolean values or nested objects. | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | sub _thaw { | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_thaw: called CC=5 SCAR=18.7
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 4 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 4 | my $types = $self->_property_types; | |||||||||||||||||||||||||||||||||||||||||
| 385 | 4 | my %thawed; | |||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 4 | for my $fld (keys %$data) { | |||||||||||||||||||||||||||||||||||||||||
| 388 | 24 | ▶ | if (exists $types->{$fld}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | 24 | my $val = $data->{$fld}; | |||||||||||||||||||||||||||||||||||||||||
| 390 | 24 | ▶ | $thawed{$fld} = Bb::Collaborate::Ultra::Util::_thaw($val, $types->{$fld}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | if defined $val; | ||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | warn $class." ignoring field: $fld"; | |||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||
| 398 | 4 | \%thawed; | |||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | =head2 construct | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | Constructs a new object from server data. | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | sub construct { | ||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
construct: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | my $payload = shift; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | my $data = $class->_thaw($payload); | |||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | my $obj = $class->new($data); | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | for ($opt{connection}) { | |||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | $obj->connection($_) if $_ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | for ($opt{parent}) { | |||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | ▶ | $obj->parent($_) if $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | # make a copy, so we can detect updates | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | $obj->_db_data(Clone::clone $data); | |||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | $obj; | |||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | =head2 load_schema | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | Constructs the object class from JSON schema data | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | sub load_schema { | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 26 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_schema: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | 26 | my $data = join("", @_); | |||||||||||||||||||||||||||||||||||||||||
| 433 | 26 | my $schema = from_json($data); | |||||||||||||||||||||||||||||||||||||||||
| 434 | my $properties = $schema->{properties} | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 26 | ▶ | or die 'schema has no properties'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 26 | foreach my $prop (sort keys %$properties) { | |||||||||||||||||||||||||||||||||||||||||
| 438 | 311 | ▶ | next if $class->meta->get_attribute($prop); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 298 | my $prop_spec = $properties->{$prop}; | |||||||||||||||||||||||||||||||||||||||||
| 440 | 298 | my $isa = $class->_build_isa( $prop, $prop_spec); | |||||||||||||||||||||||||||||||||||||||||
| 441 | 298 | ▶ | my $required = $prop_spec->{required} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 442 | 298 | $class->meta->add_attribute( | |||||||||||||||||||||||||||||||||||||||||
| 443 | $prop => (isa => $isa, is => 'rw', required => $required), | ||||||||||||||||||||||||||||||||||||||||||
| 444 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | sub _build_isa { | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 302 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_isa: called CC=10 SCAR=23.9
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 302 | my $prop = shift; | |||||||||||||||||||||||||||||||||||||||||
| 451 | 302 | my $prop_spec = shift; | |||||||||||||||||||||||||||||||||||||||||
| 452 | 302 | my $isa; | |||||||||||||||||||||||||||||||||||||||||
| 453 | my $type = $prop_spec->{type} | ||||||||||||||||||||||||||||||||||||||||||
| 454 | 302 | ▶ | or die "property has no type: $prop"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | 302 | ▶ | if ($type eq 'array') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 4 | my $of_type = $class->_build_isa($prop, $prop_spec->{items}); | |||||||||||||||||||||||||||||||||||||||||
| 457 | 4 | $isa = 'ArrayRef[' . $of_type . ']'; | |||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||
| 459 | elsif (my $enum = $prop_spec->{enum}) { | ||||||||||||||||||||||||||||||||||||||||||
| 460 | 48 | my @enum = map { lc } (@$enum); | |||||||||||||||||||||||||||||||||||||||||
| 461 | # create an anonymous enumeration | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 48 | my $enum_name = 'enum_' . join('_', @enum); | |||||||||||||||||||||||||||||||||||||||||
| 463 | 48 | ▶ | $isa = $enums{$enum_name} ||= Mouse::Util::TypeConstraints::enum( $enum_name, \@enum); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Bb::Collaborate::Ultra::DAO::enums{$enum_name} ||= Mouse::Util::TypeConstraints::enum($enum_name, \@enum)
MC/DC0%0/2
$Bb::Collaborate::Ultra::DAO::enums{$enum_name} ||= Mouse::Util::TypeConstraints::enum($enum_name, \@enum)
$Bb::Collaborate::Ultra::DAO::enums{$enum_name}
Mouse::Util::TypeConstraints::enum($enum_name, \@enum)
Truth table66%2/3
A ||= B
A= $Bb::Collaborate::Ultra::DAO::enums{$enum_name}B= Mouse::Util::TypeConstraints::enum($enum_name, \@enum)
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||
| 465 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 466 | $isa = {string => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 467 | boolean => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||
| 468 | integer => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 469 | object => 'Object', | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 250 | ▶ | }->{$type} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | or die "unknown type: $type"; | ||||||||||||||||||||||||||||||||||||||||||
| 472 | 250 | ▶ | if ($isa eq 'Object' || $isa eq 'Array') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$isa eq "Object" or $isa eq "Array"
MC/DC0%0/2
$isa eq "Object" or $isa eq "Array"
$isa eq "Object"
$isa eq "Array"
Truth table33%1/3
A or B
A= $isa eq "Object"B= $isa eq "Array"
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | warn "unknown $prop object. Predeclare in $class?"; | |||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 302 | my $format = $prop_spec->{format}; | |||||||||||||||||||||||||||||||||||||||||
| 477 | 302 | ▶ | $isa = 'Date' if $format && $format eq 'DATE_TIME'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$format and $format eq "DATE_TIME"
MC/DC50%1/2
$format and $format eq "DATE_TIME"
$format
$format eq "DATE_TIME"
Truth table66%2/3
A and B
A= $formatB= $format eq "DATE_TIME"
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | 302 | $isa; | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | # | ||||||||||||||||||||||||||||||||||||||||||
| 482 | # Shared subtypes | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # | ||||||||||||||||||||||||||||||||||||||||||
| 484 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 485 | 7 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | subtype 'Date' | ||||||||||||||||||||||||||||||||||||||||||
| 488 | => as 'Num' | ||||||||||||||||||||||||||||||||||||||||||
| 489 | 11 | ▶ | => where {m{^\d+(\.\d*)?$}} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 7 | ▶ | => message {"invalid date: $_"}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | Copyright 2016 David Warring. | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 498 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||
| 499 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||