File Coverage

blib/lib/MooseX/AttributeHelpers/Trait/String.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 15 15 100.0


line stmt bran cond sub pod time code
1             package MooseX::AttributeHelpers::Trait::String;
2 22     22   82 use Moose::Role;
  22         28  
  22         123  
3              
4             our $VERSION = '0.25';
5              
6 22     22   88348 use MooseX::AttributeHelpers::MethodProvider::String;
  22         50  
  22         3967  
7              
8             with 'MooseX::AttributeHelpers::Trait::Base';
9              
10             has 'method_provider' => (
11             is => 'ro',
12             isa => 'ClassName',
13             predicate => 'has_method_provider',
14             default => 'MooseX::AttributeHelpers::MethodProvider::String',
15             );
16              
17 6     6 1 2000 sub helper_type { 'Str' }
18              
19             before 'process_options_for_provides' => sub {
20             my ($self, $options, $name) = @_;
21              
22             # Set some default attribute options here unless already defined
23             if ((my $type = $self->helper_type) && !exists $options->{isa}){
24             $options->{isa} = $type;
25             }
26            
27             $options->{is} = 'rw' unless exists $options->{is};
28             $options->{default} = '' unless exists $options->{default};
29             };
30              
31             after 'check_provides_values' => sub {
32             my $self = shift;
33             my $provides = $self->provides;
34              
35             unless (scalar keys %$provides) {
36             my $method_constructors = $self->method_constructors;
37             my $attr_name = $self->name;
38            
39             foreach my $method (keys %$method_constructors) {
40             $provides->{$method} = ($method . '_' . $attr_name);
41             }
42             }
43             };
44              
45 22     22   137 no Moose::Role;
  22         35  
  22         107  
46              
47             1;
48              
49             __END__
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             MooseX::AttributeHelpers::Trait::String
58              
59             =head1 VERSION
60              
61             version 0.25
62              
63             =head1 SYNOPSIS
64              
65             package MyHomePage;
66             use Moose;
67             use MooseX::AttributeHelpers;
68            
69             has 'text' => (
70             metaclass => 'String',
71             is => 'rw',
72             isa => 'Str',
73             default => sub { '' },
74             provides => {
75             append => "add_text",
76             replace => "replace_text",
77             }
78             );
79              
80             my $page = MyHomePage->new();
81             $page->add_text("foo"); # same as $page->text($page->text . "foo");
82              
83             =head1 DESCRIPTION
84              
85             This module provides a simple string attribute, to which mutating string
86             operations can be applied more easily (no need to make an lvalue attribute
87             metaclass or use temporary variables). Additional methods are provided for
88             completion.
89              
90             If your attribute definition does not include any of I<is>, I<isa>,
91             I<default> or I<provides> but does use the C<String> metaclass,
92             then this module applies defaults as in the L</SYNOPSIS>
93             above. This allows for a very basic counter definition:
94              
95             has 'foo' => (metaclass => 'String');
96             $obj->append_foo;
97              
98             =head1 METHODS
99              
100             =over 4
101              
102             =item B<meta>
103              
104             =item B<method_provider>
105              
106             =item B<has_method_provider>
107              
108             =item B<helper_type>
109              
110             =item B<process_options_for_provides>
111              
112             Run before its superclass method.
113              
114             =item B<check_provides_values>
115              
116             Run after its superclass method.
117              
118             =back
119              
120             =head1 PROVIDED METHODS
121              
122             It is important to note that all those methods do in place
123             modification of the value stored in the attribute.
124              
125             =over 4
126              
127             =item I<inc>
128              
129             Increments the value stored in this slot using the magical string autoincrement
130             operator. Note that Perl doesn't provide analogeous behavior in C<-->, so
131             C<dec> is not available.
132              
133             =item I<append> C<$string>
134              
135             Append a string, like C<.=>.
136              
137             =item I<prepend> C<$string>
138              
139             Prepend a string.
140              
141             =item I<replace> C<$pattern> C<$replacement>
142              
143             Performs a regexp substitution (L<perlop/s>). There is no way to provide the
144             C<g> flag, but code references will be accepted for the replacement, causing
145             the regex to be modified with a single C<e>. C</smxi> can be applied using the
146             C<qr> operator.
147              
148             =item I<match> C<$pattern>
149              
150             Like I<replace> but without the replacement. Provided mostly for completeness.
151              
152             =item C<chop>
153              
154             L<perlfunc/chop>
155              
156             =item C<chomp>
157              
158             L<perlfunc/chomp>
159              
160             =item C<clear>
161              
162             Sets the string to the empty string (not the value passed to C<default>).
163              
164             =back
165              
166             =head1 SUPPORT
167              
168             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-AttributeHelpers>
169             (or L<bug-MooseX-AttributeHelpers@rt.cpan.org|mailto:bug-MooseX-AttributeHelpers@rt.cpan.org>).
170              
171             There is also a mailing list available for users of this distribution, at
172             L<http://lists.perl.org/list/moose.html>.
173              
174             There is also an irc channel available for users of this distribution, at
175             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
176              
177             =head1 AUTHOR
178              
179             Stevan Little <stevan@iinteractive.com>
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             This software is copyright (c) 2007 by Stevan Little and Infinity Interactive, Inc.
184              
185             This is free software; you can redistribute it and/or modify it under
186             the same terms as the Perl 5 programming language system itself.
187              
188             =cut