2 package MooseX::AttributeHelpers::String;
5 our $VERSION = '0.12_01';
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use MooseX::AttributeHelpers::MethodProvider::String;
11 extends 'MooseX::AttributeHelpers::Base';
13 has '+method_provider' => (
14 default => 'MooseX::AttributeHelpers::MethodProvider::String'
17 sub helper_type { 'Str' }
19 before 'process_options_for_provides' => sub {
20 my ($self, $options, $name) = @_;
22 # Set some default attribute options here unless already defined
23 if ((my $type = $self->helper_type) && !exists $options->{isa}){
24 $options->{isa} = $type;
27 $options->{is} = 'rw' unless exists $options->{is};
28 $options->{default} = '' unless exists $options->{default};
31 after 'check_provides_values' => sub {
33 my $provides = $self->provides;
35 unless (scalar keys %$provides) {
36 my $method_constructors = $self->method_constructors;
37 my $attr_name = $self->name;
39 foreach my $method (keys %$method_constructors) {
40 $provides->{$method} = ($method . '_' . $attr_name);
47 # register the alias ...
48 package # hide me from search.cpan.org
49 Moose::Meta::Attribute::Custom::String;
50 sub register_implementation { 'MooseX::AttributeHelpers::String' }
60 MooseX::AttributeHelpers::String
66 use MooseX::AttributeHelpers;
69 metaclass => 'String',
72 default => sub { '' },
75 replace => "replace_text",
79 my $page = MyHomePage->new();
80 $page->add_text("foo"); # same as $page->text($page->text . "foo");
84 This module provides a simple string attribute, to which mutating string
85 operations can be applied more easily (no need to make an lvalue attribute
86 metaclass or use temporary variables). Additional methods are provided for
89 If your attribute definition does not include any of I<is>, I<isa>,
90 I<default> or I<provides> but does use the C<String> metaclass,
91 then this module applies defaults as in the L</SYNOPSIS>
92 above. This allows for a very basic counter definition:
94 has 'foo' => (metaclass => 'String');
103 =item B<method_provider>
105 =item B<has_method_provider>
109 =item B<process_options_for_provides>
111 Run before its superclass method.
113 =item B<check_provides_values>
115 Run after its superclass method.
119 =head1 PROVIDED METHODS
121 It is important to note that all those methods do in place
122 modification of the value stored in the attribute.
128 Increments the value stored in this slot using the magical string autoincrement
129 operator. Note that Perl doesn't provide analogeous behavior in C<-->, so
130 C<dec> is not available.
132 =item I<append> C<$string>
134 Append a string, like C<.=>.
136 =item I<prepend> C<$string>
140 =item I<replace> C<$pattern> C<$replacement>
142 Performs a regexp substitution (L<perlop/s>). There is no way to provide the
143 C<g> flag, but code references will be accepted for the replacement, causing
144 the regex to be modified with a single C<e>. C</smxi> can be applied using the
147 =item I<match> C<$pattern>
149 Like I<replace> but without the replacement. Provided mostly for completeness.
161 Sets the string to the empty string (not the value passed to C<default>).
167 All complex software has bugs lurking in it, and this module is no
168 exception. If you find a bug please either email me, or add the bug
173 Stevan Little E<lt>stevan@iinteractive.comE<gt>
175 =head1 COPYRIGHT AND LICENSE
177 Copyright 2007-2008 by Infinity Interactive, Inc.
179 L<http://www.iinteractive.com>
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself.