| 1 | package WebAPI::DBIC::Resource::ActiveModel::Role::DBIC; | ||||||||||||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::ActiveModel::Role::DBIC::VERSION = '0.004002'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 25 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 25 | ▶ | use Devel::Dwarn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 25 | ▶ | use JSON::MaybeXS qw(JSON); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 25 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 25 | requires 'get_url_for_item_relationship'; | |||||||||||||||||||
| 12 | 25 | requires 'render_item_as_plain_hash'; | |||||||||||||||||||
| 13 | 25 | requires 'path_for_item'; | |||||||||||||||||||
| 14 | 25 | requires 'add_params_to_url'; | |||||||||||||||||||
| 15 | 25 | requires 'prefetch'; | |||||||||||||||||||
| 16 | 25 | requires 'type_namer'; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 20 | ▶ | sub activemodel_type { | ||||||||||||||||||
|
Subroutine
activemodel_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 21 | 20 | my ($self) = @_; | |||||||||||||||||||
| 22 | 20 | return $self->type_namer->type_name_for_resultset($self->set); | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 23 | ▶ | sub render_activemodel_prefetch_rel { | ||||||||||||||||||
|
Subroutine
render_activemodel_prefetch_rel: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 29 | 23 | my ($self, $set, $parent_relname, $relname, $rel_sets, $item_edit_rel_hooks) = @_; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 23 | my $parent_class = $set->result_class; | |||||||||||||||||||
| 32 | 23 | ▶ | my $child_class = $parent_class->relationship_info($relname)->{class} || die "panic"; | ||||||||||||||||||
|
Condition50%1/2
$parent_class->relationship_info($relname)->{'class'} || die('panic')
MC/DC0%0/1
$parent_class->relationship_info($relname)->{'class'} || die('panic')
$parent_class->relationship_info($relname)->{'class'}
Truth table50%1/2
A
A= $parent_class->relationship_info($relname)->{'class'}
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 23 | my @idcolumns = $child_class->unique_constraint_columns('primary'); # XXX wrong | |||||||||||||||||||
| 35 | 23 | ▶ | if (@idcolumns > 1) { # eg many-to-many that doesn't have a separate id | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | warn "Child result class $child_class has multiple keys (@idcolumns) so relations like $relname won't have links generated.\n" | ||||||||||||||||||||
| 37 | 3 | ▶ | unless our $warn_once->{"$child_class $relname"}++; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 3 | return; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 20 | my $rel_typename = $self->type_namer->type_name_for_result_class($child_class); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 20 | ▶ | return if $item_edit_rel_hooks->{$parent_relname}->{$relname}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 26 | ▶ | $item_edit_rel_hooks->{$parent_relname}->{$relname} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.2
| |||||||||||||||||||||
| 46 | 26 | my ($activemodel_obj, $row) = @_; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 26 | my $subitem = $row->$relname(); | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 26 | ▶ | my $rel_set = $rel_sets->{$rel_typename} ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$rel_sets->{$rel_typename} ||= {}
MC/DC100%1/1
$rel_sets->{$rel_typename} ||= {}
$rel_sets->{$rel_typename}
Truth table100%2/2
A
A= $rel_sets->{$rel_typename}
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 26 | my $rel_ids; | |||||||||||||||||||
| 53 | 26 | ▶ | if (not defined $subitem) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 54 | 0 | $rel_ids = undef; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | elsif ($subitem->isa('DBIx::Class::ResultSet')) { # one-to-many rel | ||||||||||||||||||||
| 57 | 9 | $rel_ids = []; | |||||||||||||||||||
| 58 | 9 | ▶ | while (my $subrow = $subitem->next) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 21 | my $id = $subrow->id; | |||||||||||||||||||
| 60 | 21 | push @$rel_ids, $id; | |||||||||||||||||||
| 61 | 21 | ▶ | my $rel_object = $self->render_row_as_activemodel_resource_object($subrow, undef, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 62 | 21 | my ($activemodel_obj, $row) = @_; | |||||||||||||||||||
| 63 | 21 | $_->($activemodel_obj, $row) for values %{$item_edit_rel_hooks->{$relname}}; | |||||||||||||||||||
| 64 | 21 | }); | |||||||||||||||||||
| 65 | # In case this object has been pulled in before, do what we can | ||||||||||||||||||||
| 66 | # to preserve the existing keys and add to them as appropriate. | ||||||||||||||||||||
| 67 | 21 | ▶ | $rel_set->{$id} //= {}; | ||||||||||||||||||
|
Condition50%1/2
$rel_set->{$id} //= {}
MC/DC0%0/1
$rel_set->{$id} //= {}
$rel_set->{$id}
Truth table50%1/2
A
A= $rel_set->{$id}
| |||||||||||||||||||||
| 68 | 21 | $rel_set->{$id} = { %{$rel_object}, %{$rel_set->{$id}} }; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | elsif ($subitem->isa('DBIx::Class::Row')) { # one-to-one rel | ||||||||||||||||||||
| 72 | 17 | $rel_ids = $subitem->id; | |||||||||||||||||||
| 73 | 17 | ▶ | my $rel_object = $self->render_row_as_activemodel_resource_object($subitem, undef, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 74 | 17 | my ($activemodel_obj, $row) = @_; | |||||||||||||||||||
| 75 | 17 | $_->($activemodel_obj, $row) for values %{$item_edit_rel_hooks->{$relname}}; | |||||||||||||||||||
| 76 | 17 | }); | |||||||||||||||||||
| 77 | # In case this object has been pulled in before, do what we can | ||||||||||||||||||||
| 78 | # to preserve the existing keys and add to them as appropriate. | ||||||||||||||||||||
| 79 | 17 | ▶ | $rel_set->{$subitem->id} //= {}; | ||||||||||||||||||
|
Condition100%2/2
$$rel_set{$subitem->id} //= {}
MC/DC100%1/1
$$rel_set{$subitem->id} //= {}
$$rel_set{$subitem->id}
Truth table100%2/2
A
A= $$rel_set{$subitem->id}
| |||||||||||||||||||||
| 80 | 17 | $rel_set->{$subitem->id} = { %{$rel_object}, %{$rel_set->{$subitem->id}} }; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||
| 83 | 0 | die "panic: don't know how to handle $row $relname value $subitem"; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # XXX We could either create a 'relationship_namer' similar to the 'type_namer', | ||||||||||||||||||||
| 87 | # or create a mechanism to facilitate adapter/serializer classes. | ||||||||||||||||||||
| 88 | # Per http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html: | ||||||||||||||||||||
| 89 | # This should use the relationship name, singularized, and suffixed with | ||||||||||||||||||||
| 90 | # '_id' for belongsTo relationships or '_ids' for hasMany relationships. | ||||||||||||||||||||
| 91 | 26 | ▶ | if ($rel_ids) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 26 | ▶ | my $suffix = ref($rel_ids) ? '_ids' : '_id'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 26 | my $relname_id = Lingua::EN::Inflect::Number::to_S($relname).$suffix; | |||||||||||||||||||
| 94 | 26 | $activemodel_obj->{$relname_id} = $rel_ids; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | 20 | } | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 20 | ▶ | sub render_activemodel_response { # return top-level document hashref | ||||||||||||||||||
|
Subroutine
render_activemodel_response: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||
| 101 | 20 | my ($self) = @_; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 20 | my $set = $self->set; | |||||||||||||||||||
| 104 | 20 | my $prefetch = $self->prefetch; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 20 | my $rel_sets = {}; | |||||||||||||||||||
| 107 | 20 | my $item_edit_rel_hooks = {}; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 23 | ▶ | $self->traverse_prefetch($set, [ 'top' ], $prefetch, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 110 | 23 | my ($self, $set, $parent_rel, $prefetch) = @_; | |||||||||||||||||||
| 111 | #warn "$self: $set, $parent_rel, $prefetch\n"; | ||||||||||||||||||||
| 112 | 23 | $self->render_activemodel_prefetch_rel($set, $parent_rel->[-1], $prefetch, $rel_sets, $item_edit_rel_hooks) | |||||||||||||||||||
| 113 | 20 | }); | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 20 | my $result_class = $set->result_class; | |||||||||||||||||||
| 116 | 32 | ▶ | my $set_data = $self->render_set_as_array_of_activemodel_resource_objects($set, undef, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 117 | 32 | my ($activemodel_obj, $row) = @_; | |||||||||||||||||||
| 118 | 32 | $_->($activemodel_obj, $row) for values %{$item_edit_rel_hooks->{'top'}}; | |||||||||||||||||||
| 119 | 20 | }); | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # construct top document to return | ||||||||||||||||||||
| 122 | 20 | ▶ | my $top_set_key = ($self->param('distinct')) ? 'data' : $self->activemodel_type; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 20 | my $top_doc = { # http://jsonapi.org/format/#document-structure-top-level | |||||||||||||||||||
| 124 | $top_set_key => $set_data, | ||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 20 | ▶ | if (keys %$rel_sets) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 12 | ▶ | while ( my ($k, $v) = each %$rel_sets) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | # sort just for test stability, | ||||||||||||||||||||
| 130 | 17 | $top_doc->{$k} = [ @{$v}{ sort keys %$v } ]; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 20 | my $total_items; | |||||||||||||||||||
| 135 | 20 | ▶ | if (($self->param('with')||'') =~ /count/) { # XXX | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->param('with') || ''
MC/DC0%0/1
$self->param('with') || ''
$self->param('with')
Truth table50%1/2
A
A= $self->param('with')
| |||||||||||||||||||||
| 136 | 0 | $total_items = $set->pager->total_entries; | |||||||||||||||||||
| 137 | 0 | $top_doc->{meta}{count} = $total_items; # XXX detail not in spec | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 20 | return $top_doc; | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 70 | ▶ | sub render_item_as_activemodel_hash { | ||||||||||||||||||
|
Subroutine
render_item_as_activemodel_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 146 | 70 | my ($self, $item) = @_; | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 70 | my $data = $self->render_item_as_plain_hash($item); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 70 | return $data; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 20 | ▶ | sub render_set_as_array_of_activemodel_resource_objects { | ||||||||||||||||||
|
Subroutine
render_set_as_array_of_activemodel_resource_objects: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 155 | 20 | my ($self, $set, $render_method, $edit_hook) = @_; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 20 | my @activemodel_objs; | |||||||||||||||||||
| 158 | 20 | ▶ | while (my $row = $set->next) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 32 | push @activemodel_objs, $self->render_row_as_activemodel_resource_object($row, $render_method, $edit_hook); | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 20 | return \@activemodel_objs; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 70 | ▶ | sub render_row_as_activemodel_resource_object { | ||||||||||||||||||
|
Subroutine
render_row_as_activemodel_resource_object: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||
| 166 | 70 | my ($self, $row, $render_method, $edit_hook) = @_; | |||||||||||||||||||
| 167 | 70 | ▶ | $render_method ||= 'render_item_as_activemodel_hash'; | ||||||||||||||||||
|
Condition50%1/2
$render_method ||= "render_item_as_activemodel_hash"
MC/DC0%0/1
$render_method ||= "render_item_as_activemodel_hash"
$render_method
Truth table50%1/2
A
A= $render_method
| |||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 70 | my $obj = $self->$render_method($row); | |||||||||||||||||||
| 170 | 70 | ▶ | $edit_hook->($obj, $row) if $edit_hook; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 70 | return $obj; | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 25 | 1; | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||