2 package MooseX::AttributeHelpers::Trait::String;
6 our $AUTHORITY = 'cpan:STEVAN';
8 use MooseX::AttributeHelpers::MethodProvider::String;
10 with 'MooseX::AttributeHelpers::Trait::Base';
12 has 'method_provider' => (
15 predicate => 'has_method_provider',
16 default => 'MooseX::AttributeHelpers::MethodProvider::String',
19 sub helper_type { 'Str' }
21 before 'process_options_for_provides' => sub {
22 my ($self, $options, $name) = @_;
24 # Set some default attribute options here unless already defined
25 if ((my $type = $self->helper_type) && !exists $options->{isa}){
26 $options->{isa} = $type;
29 $options->{is} = 'rw' unless exists $options->{is};
30 $options->{default} = '' unless exists $options->{default};
33 after 'check_provides_values' => sub {
35 my $provides = $self->provides;
37 unless (scalar keys %$provides) {
38 my $method_constructors = $self->method_constructors;
39 my $attr_name = $self->name;
41 foreach my $method (keys %$method_constructors) {
42 $provides->{$method} = ($method . '_' . $attr_name);
49 # register the alias ...
50 package # hide me from search.cpan.org
51 Moose::Meta::Attribute::Custom::Trait::String;
52 sub register_implementation { 'MooseX::AttributeHelpers::Trait::String' }
62 MooseX::AttributeHelpers::String
68 use MooseX::AttributeHelpers;
71 metaclass => 'String',
74 default => sub { '' },
77 replace => "replace_text",
81 my $page = MyHomePage->new();
82 $page->add_text("foo"); # same as $page->text($page->text . "foo");
86 This module provides a simple string attribute, to which mutating string
87 operations can be applied more easily (no need to make an lvalue attribute
88 metaclass or use temporary variables). Additional methods are provided for
91 If your attribute definition does not include any of I<is>, I<isa>,
92 I<default> or I<provides> but does use the C<String> metaclass,
93 then this module applies defaults as in the L</SYNOPSIS>
94 above. This allows for a very basic counter definition:
96 has 'foo' => (metaclass => 'String');
105 =item B<method_provider>
107 =item B<has_method_provider>
111 =item B<process_options_for_provides>
113 Run before its superclass method.
115 =item B<check_provides_values>
117 Run after its superclass method.
121 =head1 PROVIDED METHODS
123 It is important to note that all those methods do in place
124 modification of the value stored in the attribute.
130 Increments the value stored in this slot using the magical string autoincrement
131 operator. Note that Perl doesn't provide analogeous behavior in C<-->, so
132 C<dec> is not available.
134 =item I<append> C<$string>
136 Append a string, like C<.=>.
138 =item I<prepend> C<$string>
142 =item I<replace> C<$pattern> C<$replacement>
144 Performs a regexp substitution (L<perlop/s>). There is no way to provide the
145 C<g> flag, but code references will be accepted for the replacement, causing
146 the regex to be modified with a single C<e>. C</smxi> can be applied using the
149 =item I<match> C<$pattern>
151 Like I<replace> but without the replacement. Provided mostly for completeness.
163 Sets the string to the empty string (not the value passed to C<default>).
169 All complex software has bugs lurking in it, and this module is no
170 exception. If you find a bug please either email me, or add the bug
175 Stevan Little E<lt>stevan@iinteractive.comE<gt>
177 =head1 COPYRIGHT AND LICENSE
179 Copyright 2007-2008 by Infinity Interactive, Inc.
181 L<http://www.iinteractive.com>
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.