2 package MooseX::AttributeHelpers::Trait::String;
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use MooseX::AttributeHelpers::MethodProvider::String;
11 with 'MooseX::AttributeHelpers::Trait::Base';
13 has 'method_provider' => (
16 predicate => 'has_method_provider',
17 default => 'MooseX::AttributeHelpers::MethodProvider::String',
20 sub helper_type { 'Str' }
22 before 'process_options_for_provides' => sub {
23 my ($self, $options, $name) = @_;
25 # Set some default attribute options here unless already defined
26 if ((my $type = $self->helper_type) && !exists $options->{isa}){
27 $options->{isa} = $type;
30 $options->{is} = 'rw' unless exists $options->{is};
31 $options->{default} = '' unless exists $options->{default};
34 after 'check_provides_values' => sub {
36 my $provides = $self->provides;
38 unless (scalar keys %$provides) {
39 my $method_constructors = $self->method_constructors;
40 my $attr_name = $self->name;
42 foreach my $method (keys %$method_constructors) {
43 $provides->{$method} = ($method . '_' . $attr_name);
58 MooseX::AttributeHelpers::String
64 use MooseX::AttributeHelpers;
67 metaclass => 'String',
70 default => sub { '' },
73 replace => "replace_text",
77 my $page = MyHomePage->new();
78 $page->add_text("foo"); # same as $page->text($page->text . "foo");
82 This module provides a simple string attribute, to which mutating string
83 operations can be applied more easily (no need to make an lvalue attribute
84 metaclass or use temporary variables). Additional methods are provided for
87 If your attribute definition does not include any of I<is>, I<isa>,
88 I<default> or I<provides> but does use the C<String> metaclass,
89 then this module applies defaults as in the L</SYNOPSIS>
90 above. This allows for a very basic counter definition:
92 has 'foo' => (metaclass => 'String');
101 =item B<method_provider>
103 =item B<has_method_provider>
107 =item B<process_options_for_provides>
109 Run before its superclass method.
111 =item B<check_provides_values>
113 Run after its superclass method.
117 =head1 PROVIDED METHODS
119 It is important to note that all those methods do in place
120 modification of the value stored in the attribute.
126 Increments the value stored in this slot using the magical string autoincrement
127 operator. Note that Perl doesn't provide analogeous behavior in C<-->, so
128 C<dec> is not available.
130 =item I<append> C<$string>
132 Append a string, like C<.=>.
134 =item I<prepend> C<$string>
138 =item I<replace> C<$pattern> C<$replacement>
140 Performs a regexp substitution (L<perlop/s>). There is no way to provide the
141 C<g> flag, but code references will be accepted for the replacement, causing
142 the regex to be modified with a single C<e>. C</smxi> can be applied using the
145 =item I<match> C<$pattern>
147 Like I<replace> but without the replacement. Provided mostly for completeness.
159 Sets the string to the empty string (not the value passed to C<default>).
165 All complex software has bugs lurking in it, and this module is no
166 exception. If you find a bug please either email me, or add the bug
171 Stevan Little E<lt>stevan@iinteractive.comE<gt>
173 =head1 COPYRIGHT AND LICENSE
175 Copyright 2007-2009 by Infinity Interactive, Inc.
177 L<http://www.iinteractive.com>
179 This library is free software; you can redistribute it and/or modify
180 it under the same terms as Perl itself.