2 package Moose::AttributeHelpers::Trait::String;
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use Moose::AttributeHelpers::MethodProvider::String;
11 with 'Moose::AttributeHelpers::Trait::Base';
13 has 'method_provider' => (
16 predicate => 'has_method_provider',
17 default => 'Moose::AttributeHelpers::MethodProvider::String',
20 sub helper_type { 'Str' }
22 before 'process_options_for_handles' => 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_handles_values' => sub {
36 my $handles = $self->handles;
38 unless (scalar keys %$handles) {
39 my $method_constructors = $self->method_constructors;
40 my $attr_name = $self->name;
42 foreach my $method (keys %$method_constructors) {
43 $handles->{$method} = ($method . '_' . $attr_name);
50 # register the alias ...
51 package # hide me from search.cpan.org
52 Moose::Meta::Attribute::Custom::Trait::String;
53 sub register_implementation { 'Moose::AttributeHelpers::Trait::String' }
63 Moose::AttributeHelpers::String
69 use Moose::AttributeHelpers;
72 metaclass => 'String',
75 default => sub { '' },
78 replace_text => 'replace',
82 my $page = MyHomePage->new();
83 $page->add_text("foo"); # same as $page->text($page->text . "foo");
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
92 If your attribute definition does not include any of I<is>, I<isa>,
93 I<default> or I<handles> 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:
97 has 'foo' => (metaclass => 'String');
106 =item B<method_provider>
108 =item B<has_method_provider>
112 =item B<process_options_for_handles>
114 Run before its superclass method.
116 =item B<check_handles_values>
118 Run after its superclass method.
122 =head1 PROVIDED METHODS
124 It is important to note that all those methods do in place
125 modification of the value stored in the attribute.
131 Increments the value stored in this slot using the magical string autoincrement
132 operator. Note that Perl doesn't provide analogous behavior in C<-->, so
133 C<dec> is not available.
135 =item I<append> C<$string>
137 Append a string, like C<.=>.
139 =item I<prepend> C<$string>
143 =item I<replace> C<$pattern> C<$replacement>
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
150 =item I<match> C<$pattern>
152 Like I<replace> but without the replacement. Provided mostly for completeness.
164 Sets the string to the empty string (not the value passed to C<default>).
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
176 Stevan Little E<lt>stevan@iinteractive.comE<gt>
178 =head1 COPYRIGHT AND LICENSE
180 Copyright 2007-2009 by Infinity Interactive, Inc.
182 L<http://www.iinteractive.com>
184 This library is free software; you can redistribute it and/or modify
185 it under the same terms as Perl itself.