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