| 1 | package EntityModel::Definition; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | $EntityModel::Definition::VERSION = '0.102'; | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use EntityModel::Class { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | model => { type => 'EntityModel::Model' }, | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | no if $] >= 5.017011, warnings => "experimental::smartmatch"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | EntityModel::Definition - definition support for L<EntityModel> | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | version 0.102 | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | See L<EntityModel>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | See L<EntityModel>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | =head2 load | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | Generic load method, passing file or string to the appropriate L</load_file> or L</load_string> methods. | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=17 SCAR=40.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | my $src = delete $args{source}; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | my ($k, $v); | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | if(ref $src ~~ 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | ($k, $v) = %$src; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | } elsif(ref $src ~~ 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ($k, $v) = @$src; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $k = $src; | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | logDebug("Trying [%s] as [%s] => [%s]", $self, $k, $v); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | die 'Nothing passed' unless defined $k; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | my $structure; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | $structure ||= $self->load_file($v) if $k eq 'file' && defined $v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$k eq "file" and defined $v
$structure ||= $self->load_file($v)
MC/DC0%0/2
$k eq "file" and defined $v
$k eq "file"
defined $v
MC/DC0%0/2
$structure ||= $self->load_file($v)
$structure
$self->load_file($v)
Truth table33%1/3
A and B
A= $k eq "file"B= defined $v
Truth table0%0/3
A ||= B
A= $structureB= $self->load_file($v)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | $structure ||= $self->load_string($v) if $k eq 'string' && defined $v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$k eq "string" and defined $v
$structure ||= $self->load_string($v)
MC/DC0%0/2
$k eq "string" and defined $v
$k eq "string"
defined $v
MC/DC0%0/2
$structure ||= $self->load_string($v)
$structure
$self->load_string($v)
Truth table33%1/3
A and B
A= $k eq "string"B= defined $v
Truth table33%1/3
A ||= B
A= $structureB= $self->load_string($v)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Support older interface - single parameter, scalarref for string, plain scalar for XML filename | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | $structure ||= $self->load_file($k) if !ref($k) && !$v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
ref $k or $v
$structure ||= $self->load_file($k)
MC/DC0%0/2
ref $k or $v
ref $k
$v
MC/DC0%0/2
$structure ||= $self->load_file($k)
$structure
$self->load_file($k)
Truth table33%1/3
A or B
A= ref $kB= $v
Truth table0%0/3
A ||= B
A= $structureB= $self->load_file($k)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | $structure ||= $self->load_string($$k) if ref($k) && !$v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
ref $k and not $v
$structure ||= $self->load_string($$k)
MC/DC0%0/2
ref $k and not $v
ref $k
not $v
MC/DC0%0/2
$structure ||= $self->load_string($$k)
$structure
$self->load_string($$k)
Truth table33%1/3
A and not(B)
A= ref $kB= not $v
Truth table0%0/3
A ||= B
A= $structureB= $self->load_string($$k)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | die 'Unable to load ' . $self . " from [$k] and [$v]" unless $structure; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | return $self->apply_model_from_structure( | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | model => $args{model}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | structure => $structure | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | =head2 save | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | Generic save method, passing file or string to the appropriate L</save_file> or L</save_string> methods. | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $target = delete $args{target}; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my ($k, $v); | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if(ref $target ~~ 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ($k, $v) = %$target; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } elsif(ref $target ~~ 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ($k, $v) = @$target; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $k = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | logDebug("Trying [%s] as [%s] => [%s]", $self, $k, $v); | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | die 'Nothing passed' unless defined $k; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my %data = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | model => $self->model, | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return $self->save_file( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$k eq "file" and defined $v
MC/DC0%0/2
$k eq "file" and defined $v
$k eq "file"
defined $v
Truth table0%0/3
A and B
A= $k eq "file"B= defined $v
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | target => $v, | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | %data | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ) if $k eq 'file' && defined $v; | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | return $self->save_string( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | output => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | %data | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | ) if $k eq 'string'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | die 'Unable to save ' . $self . " from [$k] and [$v]"; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | =head2 field_structure | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub field_structure { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
field_structure: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ($self, $field) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | name => $field->name, | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | type => $field->type, | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =head2 entity_structure | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub entity_structure { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
entity_structure: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my ($self, $entity) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | name => $entity->name, | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | primary => $entity->primary, | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | field => [ map $self->field_structure($_), $entity->field->list ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | =head2 structure_from_model | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | Return a hashref representing the given model. | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub structure_from_model { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
structure_from_model: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my ($self, $model) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | name => $model->name, | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | entity => [ | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | map $self->entity_structure($_), $model->entity->list | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | =head2 apply_model_from_structure | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | Applies a definition (given as a hashref) to generate or update a model. | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 6 | ▶ | sub apply_model_from_structure { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_model_from_structure: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | 6 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | 6 | my $model = delete $args{model}; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 6 | my $definition = delete $args{structure}; | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | ▶ | if(my $name = delete $definition->{name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | $model->name($name); | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | ▶ | if(my $entity = delete $definition->{entity}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 6 | my @entity_list = @$entity; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | $self->add_entity_to_model( | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | model => $model, | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | definition => $_ | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | ) foreach @$entity; | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | foreach my $k (sort keys %$definition) { | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | $model->handle_item( | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | item => $k, | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | data => $definition->{$k} | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | $model->resolve_entity_dependencies; | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | =head2 add_entity_to_model | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | Create a new entity and add it to the given model. | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 13 | ▶ | sub add_entity_to_model { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_entity_to_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 13 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 13 | my $model = delete $args{model}; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 13 | my $def = delete $args{definition}; | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | 13 | my $entity = EntityModel::Entity->create_from_definition($def); | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 13 | $model->add_entity($entity); | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | 13 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =head2 register | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | Empty default method, implemented by subclasses to register themselves with the model. | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 6 | ▶ | sub register { } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||