| 1 | package Role::Identifiable::HasTags 0.009; | ||||||||||
| 2 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | # ABSTRACT: a thing with a list of tags | ||||||||||
| 4 | |||||||||||
| 5 | #pod =head1 OVERVIEW | ||||||||||
| 6 | #pod | ||||||||||
| 7 | #pod This role adds the ability for your class and its composed parts (roles, | ||||||||||
| 8 | #pod superclasses) as well as instances of it to contribute to a pool of tags | ||||||||||
| 9 | #pod describing each instance. | ||||||||||
| 10 | #pod | ||||||||||
| 11 | #pod The behavior of this role is not yet very stable. Do not rely on it yet. | ||||||||||
| 12 | #pod | ||||||||||
| 13 | #pod =cut | ||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 3 | ▶ | sub has_tag { | ||||||||
|
Subroutine
has_tag: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 18 | 3 | my ($self, $tag) = @_; | |||||||||
| 19 | |||||||||||
| 20 | 3 | ▶ | $_ eq $tag && return 1 for $self->tags; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | return; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 3 | ▶ | sub tags { | ||||||||
|
Subroutine
tags: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 26 | 3 | my ($self) = @_; | |||||||||
| 27 | |||||||||||
| 28 | # Poor man's uniq: | ||||||||||
| 29 | 9 | my %tags = map {; $_ => 1 } | |||||||||
| 30 | 3 | (@{ $self->_default_tags }, @{ $self->_instance_tags }); | |||||||||
| 31 | |||||||||||
| 32 | 3 | ▶ | return wantarray ? keys %tags : (keys %tags)[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 2 | ▶ | subtype 'Role::Identifiable::_Tag', as 'Str', where { length }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | has instance_tags => ( | ||||||||||
| 38 | is => 'ro', | ||||||||||
| 39 | isa => 'ArrayRef[Role::Identifiable::_Tag]', | ||||||||||
| 40 | reader => '_instance_tags', | ||||||||||
| 41 | init_arg => 'tags', | ||||||||||
| 42 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | ); | |||||||||
| 44 | |||||||||||
| 45 | 1 | has _default_tags => ( | |||||||||
| 46 | is => 'ro', | ||||||||||
| 47 | builder => '_build_default_tags', | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | 3 | ▶ | sub _build_default_tags { | ||||||||
|
Subroutine
_build_default_tags: called CC=3 SCAR=11.0
| |||||||||||
| 51 | # This code stolen happily from Moose::Object::BUILDALL -- rjbs, 2010-10-18 | ||||||||||
| 52 | |||||||||||
| 53 | # NOTE: we ask Perl if we even need to do this first, to avoid extra meta | ||||||||||
| 54 | # level calls | ||||||||||
| 55 | 3 | ▶ | return [] unless $_[0]->can('x_tags'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 3 | my @tags; | |||||||||
| 58 | |||||||||||
| 59 | 3 | my ($self, $params) = @_; | |||||||||
| 60 | 3 | foreach my $method ( | |||||||||
| 61 | reverse Class::MOP::class_of($self)->find_all_methods_by_name('x_tags') | ||||||||||
| 62 | ) { | ||||||||||
| 63 | 3 | push @tags, $method->{code}->execute($self, $params); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 3 | return \@tags; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 1 | ▶ | no Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 1 | ▶ | no Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 1 | 1; | |||||||||
| 72 | |||||||||||
| 73 | __END__ | ||||||||||