File Coverage

blib/lib/Role/Identifiable/HasTags.pm
Criterion Covered Total %
statement 27 27 100.0
branch 2 4 50.0
condition 2 2 100.0
subroutine 7 7 100.0
pod 0 2 0.0
total 38 42 90.4


line stmt bran cond sub pod time code
1             use Moose::Role;
2 1     1   904 # ABSTRACT: a thing with a list of tags
  1         4373  
  1         3  
3              
4             #pod =head1 OVERVIEW
5             #pod
6             #pod This role adds the ability for your class and its composed parts (roles,
7             #pod superclasses) as well as instances of it to contribute to a pool of tags
8             #pod describing each instance.
9             #pod
10             #pod The behavior of this role is not yet very stable. Do not rely on it yet.
11             #pod
12             #pod =cut
13              
14             use Moose::Util::TypeConstraints;
15 1     1   4848  
  1         2  
  1         4  
16             my ($self, $tag) = @_;
17              
18 3     3 0 23 $_ eq $tag && return 1 for $self->tags;
19              
20 3   100     7 return;
21             }
22 1         7  
23             my ($self) = @_;
24              
25             # Poor man's uniq:
26 3     3 0 4 my %tags = map {; $_ => 1 }
27             (@{ $self->_default_tags }, @{ $self->_instance_tags });
28              
29 9         19 return wantarray ? keys %tags : (keys %tags)[0];
30 3         4 }
  3         81  
  3         76  
31              
32 3 50       24 subtype 'Role::Identifiable::_Tag', as 'Str', where { length };
33              
34             has instance_tags => (
35             is => 'ro',
36             isa => 'ArrayRef[Role::Identifiable::_Tag]',
37             reader => '_instance_tags',
38             init_arg => 'tags',
39             default => sub { [] },
40             );
41              
42             has _default_tags => (
43             is => 'ro',
44             builder => '_build_default_tags',
45             );
46              
47             # This code stolen happily from Moose::Object::BUILDALL -- rjbs, 2010-10-18
48              
49             # NOTE: we ask Perl if we even need to do this first, to avoid extra meta
50             # level calls
51             return [] unless $_[0]->can('x_tags');
52              
53             my @tags;
54              
55 3 50   3   2057 my ($self, $params) = @_;
56             foreach my $method (
57 3         14 reverse Class::MOP::class_of($self)->find_all_methods_by_name('x_tags')
58             ) {
59 3         7 push @tags, $method->{code}->execute($self, $params);
60 3         8 }
61              
62             return \@tags;
63 3         615 }
64              
65             no Moose::Util::TypeConstraints;
66 3         35 no Moose::Role;
67             1;
68              
69 1     1   2132  
  1         2  
  1         5  
70 1     1   252 =pod
  1         2  
  1         4  
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             Role::Identifiable::HasTags - a thing with a list of tags
77              
78             =head1 VERSION
79              
80             version 0.008
81              
82             =head1 OVERVIEW
83              
84             This role adds the ability for your class and its composed parts (roles,
85             superclasses) as well as instances of it to contribute to a pool of tags
86             describing each instance.
87              
88             The behavior of this role is not yet very stable. Do not rely on it yet.
89              
90             =head1 PERL VERSION
91              
92             This library should run on perls released even a long time ago. It should work
93             on any version of perl released in the last five years.
94              
95             Although it may work on older versions of perl, no guarantee is made that the
96             minimum required version will not be increased. The version may be increased
97             for any reason, and there is no promise that patches will be accepted to lower
98             the minimum required perl.
99              
100             =head1 AUTHOR
101              
102             Ricardo Signes <rjbs@semiotic.systems>
103              
104             =head1 COPYRIGHT AND LICENSE
105              
106             This software is copyright (c) 2022 by Ricardo Signes.
107              
108             This is free software; you can redistribute it and/or modify it under
109             the same terms as the Perl 5 programming language system itself.
110              
111             =cut