line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Config::Chronicle::Attribute; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
88776
|
use Moose; |
|
2
|
|
|
|
|
309605
|
|
|
2
|
|
|
|
|
10
|
|
4
|
|
|
|
|
|
|
extends 'App::Config::Chronicle::Node'; |
5
|
2
|
|
|
2
|
|
10193
|
use namespace::autoclean; |
|
2
|
|
|
|
|
11027
|
|
|
2
|
|
|
|
|
9
|
|
6
|
2
|
|
|
2
|
|
1256
|
use JSON::XS qw( decode_json ); |
|
2
|
|
|
|
|
7537
|
|
|
2
|
|
|
|
|
123
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
289
|
use MooseX::Types -declare => ['LongStr']; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
10
|
|
|
|
|
|
|
use Try::Tiny; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.06'; ## VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
subtype 'LongStr', as 'Str'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
App::Config::Chronicle::Attribute |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has 'version' => ( |
25
|
|
|
|
|
|
|
is => 'rw', |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _check_type { |
29
|
|
|
|
|
|
|
my ($self, $value) = @_; |
30
|
|
|
|
|
|
|
my $def = $self->{definition}; |
31
|
|
|
|
|
|
|
$self->{_json_string} //= $def->{isa} eq 'json_string' ? 1 : 0; |
32
|
|
|
|
|
|
|
if ($self->{_json_string}) { |
33
|
|
|
|
|
|
|
try { $value = decode_json($value) } catch { die "Couldn't decode JSON attribute $value: $_" }; |
34
|
|
|
|
|
|
|
} else { |
35
|
|
|
|
|
|
|
$self->{_type_constraint} //= find_type_constraint($def->{isa}); |
36
|
|
|
|
|
|
|
unless ($self->{_type_constraint}) { |
37
|
|
|
|
|
|
|
die "Couldn't find type constraint for " . $def->{isa} . " for " . $self->name; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
$self->{_type_constraint}->check($value) or die $self->name . " expecting a value of type " . $def->{isa}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
return; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 value |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub value { |
49
|
|
|
|
|
|
|
my ($self, $value) = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
if (defined $value) { |
52
|
|
|
|
|
|
|
$self->_check_type($value); |
53
|
|
|
|
|
|
|
$self->{_value} = $value; |
54
|
|
|
|
|
|
|
$self->_set_value($value); |
55
|
|
|
|
|
|
|
$self->version($self->data_set->{version}); #Avoids building after set unless version changed. |
56
|
|
|
|
|
|
|
} else { |
57
|
|
|
|
|
|
|
if (not $self->version or $self->version ne $self->data_set->{version}) { |
58
|
|
|
|
|
|
|
my $val = $self->_build_value; |
59
|
|
|
|
|
|
|
if (defined $val) { |
60
|
|
|
|
|
|
|
$self->value($val); |
61
|
|
|
|
|
|
|
$self->version($self->data_set->{version}); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return $self->{_value}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 build |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub build { |
74
|
|
|
|
|
|
|
my $self = shift; |
75
|
|
|
|
|
|
|
my $default = $self->definition->{default}; |
76
|
|
|
|
|
|
|
$self->_check_type($default); |
77
|
|
|
|
|
|
|
$self->{_value} = $default; |
78
|
|
|
|
|
|
|
return $self; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _build_value { |
82
|
|
|
|
|
|
|
my $self = shift; |
83
|
|
|
|
|
|
|
my $value; |
84
|
|
|
|
|
|
|
$value //= $self->data_set->{app_config}->get($self->path) if ($self->data_set->{app_config}); |
85
|
|
|
|
|
|
|
$value //= $self->definition->{default} if ($self->definition); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return $value; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _set_value { |
91
|
|
|
|
|
|
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |