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