2 package MooseX::AttributeHelpers::String;
6 our $AUTHORITY = 'cpan:STEVAN';
8 use MooseX::AttributeHelpers::MethodProvider::String;
10 extends 'MooseX::AttributeHelpers::Base';
12 has '+method_provider' => (
13 default => 'MooseX::AttributeHelpers::MethodProvider::String'
16 sub helper_type { 'Str' }
18 before 'process_options_for_provides' => sub {
19 my ($self, $options, $name) = @_;
21 # Set some default attribute options here unless already defined
22 if ((my $type = $self->helper_type) && !exists $options->{isa}){
23 $options->{isa} = $type;
26 $options->{is} = 'rw' unless exists $options->{is};
27 $options->{default} = '' unless exists $options->{default};
30 after 'check_provides_values' => sub {
32 my $provides = $self->provides;
34 unless (scalar keys %$provides) {
35 my $method_constructors = $self->method_constructors;
36 my $attr_name = $self->name;
38 foreach my $method (keys %$method_constructors) {
39 $provides->{$method} = ($method . '_' . $attr_name);
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' }
59 MooseX::AttributeHelpers::String
65 use MooseX::AttributeHelpers;
68 metaclass => 'String',
71 default => sub { '' },
74 replace => "replace_text",
78 my $page = MyHomePage->new();
79 $page->add_text("foo"); # same as $page->text($page->text . "foo");
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
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:
93 has 'foo' => (metaclass => 'String');
102 =item B<method_provider>
104 =item B<has_method_provider>
108 =item B<process_options_for_provides>
110 Run before its superclass method.
112 =item B<check_provides_values>
114 Run after its superclass method.
118 =head1 PROVIDED METHODS
120 It is important to note that all those methods do in place
121 modification of the value stored in the attribute.
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.
131 =item I<append> C<$string>
133 Append a string, like C<.=>.
135 =item I<prepend> C<$string>
139 =item I<replace> C<$pattern> C<$replacement>
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
146 =item I<match> C<$pattern>
148 Like I<replace> but without the replacement. Provided mostly for completeness.
160 Sets the string to the empty string (not the value passed to C<default>).
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
172 Stevan Little E<lt>stevan@iinteractive.comE<gt>
174 =head1 COPYRIGHT AND LICENSE
176 Copyright 2007-2008 by Infinity Interactive, Inc.
178 L<http://www.iinteractive.com>
180 This library is free software; you can redistribute it and/or modify
181 it under the same terms as Perl itself.