| 1 | package Catmandu::Addable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 32 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 32 | our $VERSION = '1.0201_02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 32 | ▶ | use Catmandu::Util qw(:is :check); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 32 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 32 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 32 | with 'Catmandu::Fixable'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 32 | requires 'add'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 86 | ▶ | has autocommit => (is => 'ro', default => sub {0}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 89 | ▶ | has _commit => (is => 'rw', default => sub {0}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | around add => sub { | ||||||||||||||||||||||||||||
| 19 | 340 | ▶ | my ($orig, $self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 20 | 340 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 339 | ▶ | $data = $self->_fixer->fix($data) if $self->_fixer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 339 | ▶ | $orig->($self, $data) if defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 339 | $data; | |||||||||||||||||||||||||||
| 24 | 32 | }; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | around commit => sub { | ||||||||||||||||||||||||||||
| 27 | 46 | ▶ | my ($orig, $self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 46 | my $res = $orig->($self); | |||||||||||||||||||||||||||
| 29 | 46 | $self->_commit(1); | |||||||||||||||||||||||||||
| 30 | 46 | $res; | |||||||||||||||||||||||||||
| 31 | 32 | }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub add_many { | ||||||||||||||||||||||||||||
| 34 | 29 | ▶ | my ($self, $many) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_many: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 29 | ▶ | if (is_hash_ref($many)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | $self->add($many); | |||||||||||||||||||||||||||
| 38 | 1 | return 1; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 28 | ▶ | if (is_array_ref($many)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 16 | $self->add($_) for @$many; | |||||||||||||||||||||||||||
| 43 | 16 | return scalar @$many; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 12 | ▶ | if (is_invocant($many)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 11 | $many = check_able($many, 'generator')->generator; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 11 | check_code_ref($many); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 11 | my $data; | |||||||||||||||||||||||||||
| 53 | 11 | my $n = 0; | |||||||||||||||||||||||||||
| 54 | 11 | ▶ | while (defined($data = $many->())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 187 | $self->add($data); | |||||||||||||||||||||||||||
| 56 | 187 | $n++; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 10 | $n; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | ▶ | sub commit { } | |||||||||||||||||||||||||||
|
Subroutine
commit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub DESTROY { | ||||||||||||||||||||||||||||
| 64 | 68 | ▶ | my ($self) = shift; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 65 | 68 | ▶ | $self->commit if $self->autocommit && !$self->_commit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->autocommit and not $self->_commit
MC/DC50%1/2
$self->autocommit and not $self->_commit
$self->autocommit
not $self->_commit
Truth table66%2/3
A and not(B)
A= $self->autocommitB= not $self->_commit
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 32 | 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 | ||||||||||||||||||||||||||||