line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Addable; |
2
|
|
|
|
|
|
|
|
3
|
41
|
|
|
41
|
|
140613
|
use Catmandu::Sane; |
|
41
|
|
|
|
|
112
|
|
|
41
|
|
|
|
|
276
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
41
|
|
|
41
|
|
328
|
use Catmandu::Util qw(:is :check); |
|
41
|
|
|
|
|
187
|
|
|
41
|
|
|
|
|
18406
|
|
8
|
41
|
|
|
41
|
|
329
|
use Moo::Role; |
|
41
|
|
|
|
|
102
|
|
|
41
|
|
|
|
|
290
|
|
9
|
41
|
|
|
41
|
|
23265
|
use namespace::clean; |
|
41
|
|
|
|
|
118
|
|
|
41
|
|
|
|
|
293
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Fixable'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
requires 'add'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has autocommit => (is => 'ro', default => sub {0}); |
16
|
|
|
|
|
|
|
has _commit => (is => 'rw', default => sub {0}); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
around add => sub { |
19
|
|
|
|
|
|
|
my ($orig, $self, $data) = @_; |
20
|
|
|
|
|
|
|
return unless defined $data; |
21
|
|
|
|
|
|
|
$data = $self->_fixer->fix($data) if $self->_fixer; |
22
|
|
|
|
|
|
|
$orig->($self, $data) if defined $data; |
23
|
|
|
|
|
|
|
$data; |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
around commit => sub { |
27
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
28
|
|
|
|
|
|
|
my (@res) = $orig->($self); |
29
|
|
|
|
|
|
|
$self->_commit(1); |
30
|
|
|
|
|
|
|
@res; |
31
|
|
|
|
|
|
|
}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub add_many { |
34
|
44
|
|
|
44
|
0
|
2978
|
my ($self, $many) = @_; |
35
|
|
|
|
|
|
|
|
36
|
44
|
100
|
|
|
|
299
|
if (is_hash_ref($many)) { |
37
|
1
|
|
|
|
|
28
|
$self->add($many); |
38
|
1
|
|
|
|
|
5
|
return 1; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
43
|
100
|
|
|
|
213
|
if (is_array_ref($many)) { |
42
|
20
|
|
|
|
|
518
|
$self->add($_) for @$many; |
43
|
20
|
|
|
|
|
145
|
return scalar @$many; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
23
|
100
|
|
|
|
104
|
if (is_invocant($many)) { |
47
|
22
|
|
|
|
|
158
|
$many = check_able($many, 'generator')->generator; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
22
|
|
|
|
|
115
|
check_code_ref($many); |
51
|
|
|
|
|
|
|
|
52
|
22
|
|
|
|
|
5393
|
my $data; |
53
|
22
|
|
|
|
|
70
|
my $n = 0; |
54
|
22
|
|
|
|
|
83
|
while (defined($data = $many->())) { |
55
|
272
|
|
|
|
|
12537
|
$self->add($data); |
56
|
272
|
|
|
|
|
1588
|
$n++; |
57
|
|
|
|
|
|
|
} |
58
|
20
|
|
|
|
|
347
|
$n; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
20
|
1
|
|
sub commit { } |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub DESTROY { |
64
|
90
|
|
|
90
|
|
28255
|
my ($self) = shift; |
65
|
90
|
100
|
66
|
|
|
3460
|
$self->commit if $self->autocommit && !$self->_commit; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
__END__ |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=pod |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 NAME |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Catmandu::Addable - Base class for all Catmandu modules need to implement add |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 SYNOPSIS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
package My::Adder; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
use Moo; |
83
|
|
|
|
|
|
|
use Data::Dumper; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
with 'Catmandu::Addable'; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub add { |
88
|
|
|
|
|
|
|
my ($self,$object) = @_; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
print "So you want to add:\n"; |
91
|
|
|
|
|
|
|
print Dumper($object); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub commit { |
97
|
|
|
|
|
|
|
my $self = shift; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
print "And now you are done?\n"; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
package main; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
my $adder = My::Adder->new(fix => ['upcase(foo)']); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# prints foo => BAR |
107
|
|
|
|
|
|
|
$adder->add({ foo => 'bar' }); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# prints: |
110
|
|
|
|
|
|
|
# foo => BAR |
111
|
|
|
|
|
|
|
# foo => BAR |
112
|
|
|
|
|
|
|
$adder->add_many([ { foo => 'bar' } , { foo => 'bar' }]); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# prints a commit statement |
115
|
|
|
|
|
|
|
$adder->commit; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 OPTIONS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=over |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item autocommit |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Autocommit when the exporter gets out of scope. Default 0. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=back |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 METHODS |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 add($hash) |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Receives a Perl hash and should return true or false. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 commit |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
This method is usually called at the end of many add or add_many operations. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 INHERIT |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
If you provide an 'add' method, then automatically your package gets a add_many |
140
|
|
|
|
|
|
|
method, plus a fix attribute which transforms all Perl hashes provided to the |
141
|
|
|
|
|
|
|
add method. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 SEE ALSO |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
L<Catmandu::Fixable>, L<Catmandu::Exporter> , L<Catmandu::Store> |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=cut |