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