lib/MooseX/Trait/Tag.pm

filter: Statementstmt 100% 40 / 40 Branchbran 58.3% 7 / 12 Conditioncond 80.0% 4 / 5 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 90.0% 63 / 70 CC 11 SCAR 11.4
CC
11
Cov
89%
register_tag
5.1
set_nothing
4.0
get_tag
2.0
CRAP 3.1
↑ Summary
1 package MooseX::Trait::Tag;
2  
3 =head1 NAME
4  
5 MooseX::Trait::Tag - Add an arbitrary tag to an attribute
6  
7 =cut
8  
9 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 VERSION
13  
14 Version 1.04
15  
16 =cut
17  
18 1 our $VERSION = '1.04';
19  
20 =head1 SYNOPSIS
21  
22 This module provides a way to give a Moose attribute an arbitrary tag.
23 Various methods are installed into the package declaring the attribute,
24 allowing for actions to be taken on only the tagged attributes.
25 This module is inspired by L<http://search.cpan.org/~ether/Moose/lib/Moose/Cookbook/Meta/Labeled_AttributeTrait.pod>
26  
27 Example usage:
28  
29 package Foo;
30 use Moose;
31 use MooseX::Trait::Tag qw{metadata};
32  
33 has field1 => (is => 'rw', traits => [qw{metadata}]);
34 has field2 => (is => 'ro', traits => [qw{metadata}]);
35 has field3 => (is => 'rw', traits => []);
36  
37 __PACKAGE__->meta->make_immutable;
38  
39 package main;
40  
41 my $foo = Foo->new;
42  
43 my @metadata_fields = sort $foo->all_metadata_attributes;
44 # @metadata_fields => qw{ field1 field2 }
45  
46 print "Yes\n" if $foo->is_metadata_attribute('field1');
47 print "No\n" if !$foo->is_metadata_attribute('field3');
48  
49 $foo->set_metadata(
50 field1 => 6,
51 field2 => 7,
52 field3 => 8,
53 field4 => 9,
54 );
55 # => only field1 is modified
56  
57 my %field_to_value = $foo->get_metadata;
58 # %field_to_value => (field1 => 6, field2 => undef)
59  
60 =cut
61  
62 =head1 METHODS
63  
64 =cut
65  
66 3 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
67 3 my $class = shift;
68 3 my $importing_class = caller();
69 3 $class->register_tag(importing_class => $importing_class, tag => $_) for @_;
70 }
71  
72 =head2 register_tag(importing_class => $importing_class, tag => $tag)
73  
74 Install the methods asociated with the tag into the importing class.
75  
76 =cut
77  
78 3 ▶sub register_tag {
Subroutine register_tag: called CC=5 SCAR=16.2
Pod documented
79 3 my $class = shift;
80 3 my %args = @_;
81 3 my ($importing_class, $tag) = @args{qw{importing_class tag}};
82  
83 #Moose magic to create a new trait bound to the label
84 3 my $tag_class = "$class\::$tag";
85 3 Moose::Meta::Role->create($tag_class);
86 3 Moose::Util::meta_attribute_alias($tag, $tag_class);
87  
88 =head1 INSTALLED METHODS
89  
90 =cut
91  
92 =head2 is_<tag>_attribute( $attribute_name )
93  
94 Given an attribute name, determine if it is registered to the tag.
95 Requires an attribute name.
96  
97 =cut
98  
99 3 my $importing_class_meta = $importing_class->meta;
100 5 ▶ $importing_class_meta->add_method("is_$tag\_attribute", sub {
Subroutine is_nothing_attribute: called CC=2 SCAR=6.9
101 5 my $attribute = (shift)->meta->find_attribute_by_name(@_);
102 4 ▶ return $attribute && $attribute->does($tag);
Condition100%2/2
$attribute && $attribute->does($tag)
!ll
22
MC/DC100%2/2
$attribute && $attribute->does($tag)
$attribute $attribute->does($tag)
Truth table100%3/3
A && B
A= $attributeB= $attribute->does($tag)
ABresult
0X0
100
111
103 3 ▶ }) unless $importing_class_meta->has_method("is_$tag\_attribute");
Branch: 1 branch
BranchTrueFalseTotal
unless $importing_class_meta->has_method("is_${tag}_attribute") 3 0 3
104  
105 =head2 all_<tag>_attributes( )
106  
107 Return the names of all attributes marked with the tag.
108  
109 =cut
110  
111 4 ▶ $importing_class_meta->add_method("all_$tag\_attributes", sub {
Subroutine all_tag_attributes: called CC=1 SCAR=0.0
112 7 map { $_->name }
113 4 grep { $_->does($tag) }
114 (shift)->meta->get_all_attributes
115 3 ▶ }) unless $importing_class_meta->has_method("all_$tag\_attributes");
Branch: 1 branch
BranchTrueFalseTotal
unless $importing_class_meta->has_method("all_${tag}_attributes") 3 0 3
116  
117 =head2 get_<tag>( )
118  
119 Return all name-value pairs for each readable attribute associated with the
120 appropriate tag.
121  
122 =cut
123  
124 2 ▶ $importing_class_meta->add_method("get_$tag", sub {
Subroutine get_tag: called CC=2 SCAR=7.0
125 2 my $self = shift;
126 return
127 3 map { my $reader = $_->get_read_method; ($_->name => scalar($self->$reader)) }
128 2 ▶ grep { $_->does($tag) && $_->get_read_method }
Condition66%2/3
$_->does($tag) && $_->get_read_method
!ll&&!rl&&r
203
MC/DC50%1/2
$_->does($tag) && $_->get_read_method
$_->does($tag) $_->get_read_method
Truth table66%2/3
A && B
A= $_->does($tag)B= $_->get_read_method
ABresult
0X0
100
111
129 $self->meta->get_all_attributes
130 3 ▶ }) unless $importing_class_meta->has_method("get_$tag");
Branch: 1 branch
BranchTrueFalseTotal
unless $importing_class_meta->has_method("get_$tag") 3 0 3
131  
132 =head2 set_<tag>( attribute1 => $new_value1, ... )
133  
134 Given name-value pairs, update each writable attribute with the new value
135 if it is associated with the appropriate tag.
136  
137 =cut
138  
139 2 ▶ $importing_class_meta->add_method("set_$tag", sub {
Subroutine set_nothing: called CC=4 SCAR=13.9
140 2 my ($self, %args) = @_;
141  
142 2 for my $attribute (grep { $_->does($tag) } $self->meta->get_all_attributes) {
143 3 ▶ next unless exists $args{$attribute->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$attribute->name} 0 3 3
144 3 my $writer = $attribute->get_write_method;
145 3 ▶ next unless $writer;
Branch: 1 branch
BranchTrueFalseTotal
unless $writer 1 2 3
146 2 $self->$writer($args{$attribute->name});
147 }
148  
149 2 return;
150 3 ▶ }) unless $importing_class_meta->has_method("set_$tag");
Branch: 1 branch
BranchTrueFalseTotal
unless $importing_class_meta->has_method("set_$tag") 3 0 3
151 }
152  
153 =head1 AUTHOR
154  
155 Aaron Cohen, C<< <aarondcohen at gmail.com> >>
156  
157 =head1 ACKNOWLEDGEMENTS
158  
159 This module was made possible by L<Shutterstock|http://www.shutterstock.com/>
160 (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source
161 projects from Shutterstock can be found at
162 L<code.shutterstock.com|http://code.shutterstock.com/>.
163  
164 =head1 BUGS
165  
166 Please report any bugs or feature requests to C<bug-moosex-trait-tag at rt.cpan.org>, or through
167 the web interface at L<https://github.com/aarondcohen/perl-moosex-trait-tag/issues>. I will
168 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
169  
170 =head1 SUPPORT
171  
172 You can find documentation for this module with the perldoc command.
173  
174 perldoc MooseX::Trait::Tag
175  
176 You can also look for information at:
177  
178 =over 4
179  
180 =item * Official GitHub Repo
181  
182 L<https://github.com/aarondcohen/perl-moosex-trait-tag>
183  
184 =item * GitHub's Issue Tracker (report bugs here)
185  
186 L<https://github.com/aarondcohen/perl-moosex-trait-tag/issues>
187  
188 =item * CPAN Ratings
189  
190 L<http://cpanratings.perl.org/d/MooseX-Trait-Tag>
191  
192 =item * Official CPAN Page
193  
194 L<http://search.cpan.org/dist/MooseX-Trait-Tag/>
195  
196 =back
197  
198 =head1 LICENSE AND COPYRIGHT
199  
200 Copyright 2013 Aaron Cohen.
201  
202 This program is free software; you can redistribute it and/or modify it
203 under the terms of either: the GNU General Public License as published
204 by the Free Software Foundation; or the Artistic License.
205  
206 See http://dev.perl.org/licenses/ for more information.
207  
208 =cut
209  
210 1 1; # End of MooseX::Trait::Tag
↑ Summary