| 1 | package KiokuX::Model::Role::Annotations; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use KiokuDB::Util qw(set); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = "0.01"; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | parameter namespace => ( | |||||||||||||||||||||||||||
| 12 | isa => "Str", | ||||||||||||||||||||||||||||
| 13 | is => "ro", | ||||||||||||||||||||||||||||
| 14 | default => "annotations", | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | parameter method_namespace => ( | ||||||||||||||||||||||||||||
| 18 | isa => "Str", | ||||||||||||||||||||||||||||
| 19 | is => "ro", | ||||||||||||||||||||||||||||
| 20 | lazy => 1, | ||||||||||||||||||||||||||||
| 21 | 1 | ▶ | default => sub { shift->namespace }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | parameter key_callback => ( | ||||||||||||||||||||||||||||
| 25 | isa => "Str|CodeRef", | ||||||||||||||||||||||||||||
| 26 | is => "ro", | ||||||||||||||||||||||||||||
| 27 | lazy => 1, | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | my $name = $self->method_namespace; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | return sprintf "_${name}_set_id"; | |||||||||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||||||||||
| 35 | 1 | ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | parameter id_callback => ( | |||||||||||||||||||||||||||
| 38 | isa => "Str|CodeRef", | ||||||||||||||||||||||||||||
| 39 | is => "ro", | ||||||||||||||||||||||||||||
| 40 | default => "object_to_id", | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | role { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 44 | 1 | with qw(KiokuDB::Role::API); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | my $p = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | my $name = $p->method_namespace; | |||||||||||||||||||||||||||
| 49 | 1 | my $ns = $p->namespace; | |||||||||||||||||||||||||||
| 50 | 1 | my $set_id = $p->key_callback; | |||||||||||||||||||||||||||
| 51 | 1 | my $object_to_id = $p->id_callback; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | requires $set_id unless ref $set_id or $set_id eq "_${name}_set_id"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $set_id or $set_id eq "_${name}_set_id"
MC/DC0%0/2
ref $set_id or $set_id eq "_${name}_set_id"
ref $set_id
$set_id eq "_${name}_set_id"
Truth table33%1/3
A or B
A= ref $set_idB= $set_id eq "_${name}_set_id"
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | requires $object_to_id unless ref $object_to_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 15 | ▶ | my $annotation_set = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 15 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 15 | $self->lookup( $self->$set_id($key) ); | |||||||||||||||||||||||||||
| 61 | 1 | }; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 10 | ▶ | method "has_${name}" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 10 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 10 | $self->exists( $self->$set_id(@args) ); | |||||||||||||||||||||||||||
| 67 | 1 | }; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9 | ▶ | method "${name}_for" => sub { | ||||||||||||||||||||||||||
|
Subroutine
annotations_for: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 9 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 9 | ▶ | if ( my $set = $self->$annotation_set($key) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 7 | return $set->members; | |||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 2 | return (); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 1 | }; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | my $insert_into_set = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 3 | my ( $self, $key, @annotations ) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | if ( my $set = $self->$annotation_set($key) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | $set->insert(@annotations); | |||||||||||||||||||||||||||
| 84 | 2 | $self->insert_nonroot(@annotations); | |||||||||||||||||||||||||||
| 85 | 2 | $self->update($set); | |||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 1 | $self->insert( $self->$set_id($key) => set(@annotations) ); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 1 | }; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | method "add_${name}_for" => sub { | ||||||||||||||||||||||||||
|
Subroutine
add_annotations_for: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 1 | my ( $self, $key, @annotations ) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | $self->txn_do(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 1 | $self->$insert_into_set( $key, @annotations ); | |||||||||||||||||||||||||||
| 96 | 1 | }); | |||||||||||||||||||||||||||
| 97 | 1 | }; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | method "add_${name}" => sub { | ||||||||||||||||||||||||||
|
Subroutine
add_annotations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | 2 | my ( $self, @annotations ) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | $self->txn_do(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | 2 | foreach my $annotation ( @annotations ) { | |||||||||||||||||||||||||||
| 104 | 2 | $self->$insert_into_set( $annotation->subject, $annotation ); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 2 | }); | |||||||||||||||||||||||||||
| 107 | 1 | }; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | ▶ | my $remove_from_set = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 110 | 3 | my ( $self, $key, @annotations ) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | ▶ | if ( my $set = $self->$annotation_set($key) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 3 | $set->remove(@annotations); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | if ( $set->size ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 2 | $self->update($set); | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 1 | $self->delete($set); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | $self->delete(@annotations); | |||||||||||||||||||||||||||
| 123 | 1 | }; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | ▶ | method "remove_${name}_for" => sub { | ||||||||||||||||||||||||||
|
Subroutine
remove_annotations_for: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | 1 | my ( $self, $key, @annotations ) = @_; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | ▶ | $self->txn_do(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | 1 | $self->$remove_from_set( $key, @annotations ); | |||||||||||||||||||||||||||
| 130 | 1 | }); | |||||||||||||||||||||||||||
| 131 | 1 | }; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | method "remove_${name}" => sub { | ||||||||||||||||||||||||||
|
Subroutine
remove_annotations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | 1 | my ( $self, @annotations ) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | $self->txn_do(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 137 | 1 | foreach my $annotation ( @annotations ) { | |||||||||||||||||||||||||||
| 138 | 2 | $self->$remove_from_set( $annotation->subject, $annotation ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 1 | }); | |||||||||||||||||||||||||||
| 141 | 1 | }; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 26 | ▶ | method "_${name}_set_id" => sub { | ||||||||||||||||||||||||||
|
Subroutine
_annotations_set_id: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 144 | 26 | my ( $self, $item ) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 26 | ▶ | my $id = ref($item) ? $self->$object_to_id($item) : $item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 26 | ▶ | croak "Can't determine ID for $item" unless defined $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 26 | return "${ns}:${id}"; | |||||||||||||||||||||||||||
| 151 | 1 | }; | |||||||||||||||||||||||||||
| 152 | 1 | }; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # ex: set sw=4 et: | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | __PACKAGE__ | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||