diff options
Diffstat (limited to 'lib/Moose/Meta/Attribute/Native/Trait')
-rw-r--r-- | lib/Moose/Meta/Attribute/Native/Trait/Array.pm | 384 | ||||
-rw-r--r-- | lib/Moose/Meta/Attribute/Native/Trait/Bool.pm | 146 | ||||
-rw-r--r-- | lib/Moose/Meta/Attribute/Native/Trait/Code.pm | 129 | ||||
-rw-r--r-- | lib/Moose/Meta/Attribute/Native/Trait/Counter.pm | 157 | ||||
-rw-r--r-- | lib/Moose/Meta/Attribute/Native/Trait/Hash.pm | 226 | ||||
-rw-r--r-- | lib/Moose/Meta/Attribute/Native/Trait/Number.pm | 155 | ||||
-rw-r--r-- | lib/Moose/Meta/Attribute/Native/Trait/String.pm | 187 |
7 files changed, 1384 insertions, 0 deletions
diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm new file mode 100644 index 0000000..3d33b08 --- /dev/null +++ b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm @@ -0,0 +1,384 @@ +package Moose::Meta::Attribute::Native::Trait::Array; +our $VERSION = '2.1405'; + +use Moose::Role; +with 'Moose::Meta::Attribute::Native::Trait'; + +sub _helper_type { 'ArrayRef' } + +no Moose::Role; + +1; + +# ABSTRACT: Helper trait for ArrayRef attributes + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes + +=head1 VERSION + +version 2.1405 + +=head1 SYNOPSIS + + package Stuff; + use Moose; + + has 'options' => ( + traits => ['Array'], + is => 'ro', + isa => 'ArrayRef[Str]', + default => sub { [] }, + handles => { + all_options => 'elements', + add_option => 'push', + map_options => 'map', + filter_options => 'grep', + find_option => 'first', + get_option => 'get', + join_options => 'join', + count_options => 'count', + has_options => 'count', + has_no_options => 'is_empty', + sorted_options => 'sort', + }, + ); + + no Moose; + 1; + +=head1 DESCRIPTION + +This trait provides native delegation methods for array references. + +=head1 DEFAULT TYPE + +If you don't provide an C<isa> value for your attribute, it will default to +C<ArrayRef>. + +=head1 PROVIDED METHODS + +=over 4 + +=item * B<count> + +Returns the number of elements in the array. + + $stuff = Stuff->new; + $stuff->options( [ "foo", "bar", "baz", "boo" ] ); + + print $stuff->count_options; # prints 4 + +This method does not accept any arguments. + +=item * B<is_empty> + +Returns a boolean value that is true when the array has no elements. + + $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n"; + +This method does not accept any arguments. + +=item * B<elements> + +Returns all of the elements of the array as an array (not an array reference). + + my @option = $stuff->all_options; + print "@options\n"; # prints "foo bar baz boo" + +This method does not accept any arguments. + +=item * B<get($index)> + +Returns an element of the array by its index. You can also use negative index +numbers, just as with Perl's core array handling. + + my $option = $stuff->get_option(1); + print "$option\n"; # prints "bar" + +If the specified element does not exist, this will return C<undef>. + +This method accepts just one argument. + +=item * B<pop> + +Just like Perl's builtin C<pop>. + +This method does not accept any arguments. + +=item * B<push($value1, $value2, value3 ...)> + +Just like Perl's builtin C<push>. Returns the number of elements in the new +array. + +This method accepts any number of arguments. + +=item * B<shift> + +Just like Perl's builtin C<shift>. + +This method does not accept any arguments. + +=item * B<unshift($value1, $value2, value3 ...)> + +Just like Perl's builtin C<unshift>. Returns the number of elements in the new +array. + +This method accepts any number of arguments. + +=item * B<splice($offset, $length, @values)> + +Just like Perl's builtin C<splice>. In scalar context, this returns the last +element removed, or C<undef> if no elements were removed. In list context, +this returns all the elements removed from the array. + +This method requires at least one argument. + +=item * B<first( sub { ... } )> + +This method returns the first matching item in the array, just like +L<List::Util>'s C<first> function. The matching is done with a subroutine +reference you pass to this method. The subroutine will be called against each +element in the array until one matches or all elements have been checked. +Each list element will be available to the sub in C<$_>. + + my $found = $stuff->find_option( sub {/^b/} ); + print "$found\n"; # prints "bar" + +This method requires a single argument. + +=item * B<first_index( sub { ... } )> + +This method returns the index of the first matching item in the array, just +like L<List::MoreUtils>'s C<first_index> function. The matching is done with a +subroutine reference you pass to this method. The subroutine will be called +against each element in the array until one matches or all elements have been +checked. Each list element will be available to the sub in C<$_>. + +This method requires a single argument. + +=item * B<grep( sub { ... } )> + +This method returns every element matching a given criteria, just like Perl's +core C<grep> function. This method requires a subroutine which implements the +matching logic; each list element will be available to the sub in C<$_>. + + my @found = $stuff->filter_options( sub {/^b/} ); + print "@found\n"; # prints "bar baz boo" + +This method requires a single argument. + +=item * B<map( sub { ... } )> + +This method transforms every element in the array and returns a new array, +just like Perl's core C<map> function. This method requires a subroutine which +implements the transformation; each list element will be available to the sub +in C<$_>. + + my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); + print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" + +This method requires a single argument. + +=item * B<reduce( sub { ... } )> + +This method turns an array into a single value, by passing a function the +value so far and the next value in the array, just like L<List::Util>'s +C<reduce> function. The reducing is done with a subroutine reference you pass +to this method; each list element will be available to the sub in C<$_>. + + my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } ); + print "$found\n"; # prints "foobarbazboo" + +This method requires a single argument. + +=item * B<sort> + +=item * B<sort( sub { ... } )> + +Returns the elements of the array (not an array reference) in sorted order, +or, like C<elements>, returns the number of elements in the array in scalar context. + +You can provide an optional subroutine reference to sort with (as you can with +Perl's core C<sort> function). However, instead of using C<$a> and C<$b> in +this subroutine, you will need to use C<$_[0]> and C<$_[1]>. + + # ascending ASCIIbetical + my @sorted = $stuff->sort_options(); + + # Descending alphabetical order + my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } ); + print "@sorted_options\n"; # prints "foo boo baz bar" + +This method accepts a single argument. + +=item * B<sort_in_place> + +=item * B<sort_in_place( sub { ... } )> + +Sorts the array I<in place>, modifying the value of the attribute. + +You can provide an optional subroutine reference to sort with (as you can with +Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you +will need to use C<$_[0]> and C<$_[1]> instead. + +This method does not define a return value. + +This method accepts a single argument. + +=item * B<shuffle> + +Returns the elements of the array in random order, like C<shuffle> from +L<List::Util>. + +This method does not accept any arguments. + +=item * B<uniq> + +Returns the array with all duplicate elements removed, like C<uniq> from +L<List::MoreUtils>. + +This method does not accept any arguments. + +=item * B<join($str)> + +Joins every element of the array using the separator given as argument, just +like Perl's core C<join> function. + + my $joined = $stuff->join_options(':'); + print "$joined\n"; # prints "foo:bar:baz:boo" + +This method requires a single argument. + +=item * B<set($index, $value)> + +Given an index and a value, sets the specified array element's value. + +This method returns the value at C<$index> after the set. + +This method requires two arguments. + +=item * B<delete($index)> + +Removes the element at the given index from the array. + +This method returns the deleted value. Note that if no value exists, it will +return C<undef>. + +This method requires one argument. + +=item * B<insert($index, $value)> + +Inserts a new element into the array at the given index. + +This method returns the new value at C<$index>. + +This method requires two arguments. + +=item * B<clear> + +Empties the entire array, like C<@array = ()>. + +This method does not define a return value. + +This method does not accept any arguments. + +=item * B<accessor($index)> + +=item * B<accessor($index, $value)> + +This method provides a get/set accessor for the array, based on array indexes. +If passed one argument, it returns the value at the specified index. If +passed two arguments, it sets the value of the specified index. + +When called as a setter, this method returns the new value at C<$index>. + +This method accepts one or two arguments. + +=item * B<natatime($n)> + +=item * B<natatime($n, $code)> + +This method returns an iterator which, on each call, returns C<$n> more items +from the array, in order, like C<natatime> from L<List::MoreUtils>. + +If you pass a coderef as the second argument, then this code ref will be +called on each group of C<$n> elements in the array until the array is +exhausted. + +This method accepts one or two arguments. + +=item * B<shallow_clone> + +This method returns a shallow clone of the array reference. The return value +is a reference to a new array with the same elements. It is I<shallow> +because any elements that were references in the original will be the I<same> +references in the clone. + +=back + +=head1 BUGS + +See L<Moose/BUGS> for details on reporting bugs. + +=head1 AUTHORS + +=over 4 + +=item * + +Stevan Little <stevan.little@iinteractive.com> + +=item * + +Dave Rolsky <autarch@urth.org> + +=item * + +Jesse Luehrs <doy@tozt.net> + +=item * + +Shawn M Moore <code@sartak.org> + +=item * + +יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> + +=item * + +Karen Etheridge <ether@cpan.org> + +=item * + +Florian Ragwitz <rafl@debian.org> + +=item * + +Hans Dieter Pearcey <hdp@weftsoar.net> + +=item * + +Chris Prather <chris@prather.org> + +=item * + +Matt S Trout <mst@shadowcat.co.uk> + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2006 by Infinity Interactive, Inc.. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm b/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm new file mode 100644 index 0000000..d228d0b --- /dev/null +++ b/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm @@ -0,0 +1,146 @@ +package Moose::Meta::Attribute::Native::Trait::Bool; +our $VERSION = '2.1405'; + +use Moose::Role; +with 'Moose::Meta::Attribute::Native::Trait'; + +sub _helper_type { 'Bool' } + +no Moose::Role; + +1; + +# ABSTRACT: Helper trait for Bool attributes + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Moose::Meta::Attribute::Native::Trait::Bool - Helper trait for Bool attributes + +=head1 VERSION + +version 2.1405 + +=head1 SYNOPSIS + + package Room; + use Moose; + + has 'is_lit' => ( + traits => ['Bool'], + is => 'rw', + isa => 'Bool', + default => 0, + handles => { + illuminate => 'set', + darken => 'unset', + flip_switch => 'toggle', + is_dark => 'not', + }, + ); + + my $room = Room->new(); + $room->illuminate; # same as $room->is_lit(1); + $room->darken; # same as $room->is_lit(0); + $room->flip_switch; # same as $room->is_lit(not $room->is_lit); + return $room->is_dark; # same as !$room->is_lit + +=head1 DESCRIPTION + +This trait provides native delegation methods for boolean values. A boolean is +a scalar which can be C<1>, C<0>, C<"">, or C<undef>. + +=head1 DEFAULT TYPE + +If you don't provide an C<isa> value for your attribute, it will default to +C<Bool>. + +=head1 PROVIDED METHODS + +None of these methods accept arguments. + +=over 4 + +=item * B<set> + +Sets the value to C<1> and returns C<1>. + +=item * B<unset> + +Set the value to C<0> and returns C<0>. + +=item * B<toggle> + +Toggles the value. If it's true, set to false, and vice versa. + +Returns the new value. + +=item * B<not> + +Equivalent of 'not C<$value>'. + +=back + +=head1 BUGS + +See L<Moose/BUGS> for details on reporting bugs. + +=head1 AUTHORS + +=over 4 + +=item * + +Stevan Little <stevan.little@iinteractive.com> + +=item * + +Dave Rolsky <autarch@urth.org> + +=item * + +Jesse Luehrs <doy@tozt.net> + +=item * + +Shawn M Moore <code@sartak.org> + +=item * + +יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> + +=item * + +Karen Etheridge <ether@cpan.org> + +=item * + +Florian Ragwitz <rafl@debian.org> + +=item * + +Hans Dieter Pearcey <hdp@weftsoar.net> + +=item * + +Chris Prather <chris@prather.org> + +=item * + +Matt S Trout <mst@shadowcat.co.uk> + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2006 by Infinity Interactive, Inc.. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Code.pm b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm new file mode 100644 index 0000000..a0b90b3 --- /dev/null +++ b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm @@ -0,0 +1,129 @@ +package Moose::Meta::Attribute::Native::Trait::Code; +our $VERSION = '2.1405'; + +use Moose::Role; +with 'Moose::Meta::Attribute::Native::Trait'; + +sub _helper_type { 'CodeRef' } + +no Moose::Role; + +1; + +# ABSTRACT: Helper trait for CodeRef attributes + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Moose::Meta::Attribute::Native::Trait::Code - Helper trait for CodeRef attributes + +=head1 VERSION + +version 2.1405 + +=head1 SYNOPSIS + + package Foo; + use Moose; + + has 'callback' => ( + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', + default => sub { + sub { print "called" } + }, + handles => { + call => 'execute', + }, + ); + + my $foo = Foo->new; + $foo->call; # prints "called" + +=head1 DESCRIPTION + +This trait provides native delegation methods for code references. + +=head1 DEFAULT TYPE + +If you don't provide an C<isa> value for your attribute, it will default to +C<CodeRef>. + +=head1 PROVIDED METHODS + +=over 4 + +=item * B<execute(@args)> + +Calls the coderef with the given args. + +=item * B<execute_method(@args)> + +Calls the coderef with the instance as invocant and given args. + +=back + +=head1 BUGS + +See L<Moose/BUGS> for details on reporting bugs. + +=head1 AUTHORS + +=over 4 + +=item * + +Stevan Little <stevan.little@iinteractive.com> + +=item * + +Dave Rolsky <autarch@urth.org> + +=item * + +Jesse Luehrs <doy@tozt.net> + +=item * + +Shawn M Moore <code@sartak.org> + +=item * + +יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> + +=item * + +Karen Etheridge <ether@cpan.org> + +=item * + +Florian Ragwitz <rafl@debian.org> + +=item * + +Hans Dieter Pearcey <hdp@weftsoar.net> + +=item * + +Chris Prather <chris@prather.org> + +=item * + +Matt S Trout <mst@shadowcat.co.uk> + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2006 by Infinity Interactive, Inc.. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm b/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm new file mode 100644 index 0000000..2677f88 --- /dev/null +++ b/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm @@ -0,0 +1,157 @@ +package Moose::Meta::Attribute::Native::Trait::Counter; +our $VERSION = '2.1405'; + +use Moose::Role; +with 'Moose::Meta::Attribute::Native::Trait'; + +sub _helper_type { 'Num' } +sub _root_types { 'Num', 'Int' } + +no Moose::Role; + +1; + +# ABSTRACT: Helper trait for Int attributes which represent counters + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Moose::Meta::Attribute::Native::Trait::Counter - Helper trait for Int attributes which represent counters + +=head1 VERSION + +version 2.1405 + +=head1 SYNOPSIS + + package MyHomePage; + use Moose; + + has 'counter' => ( + traits => ['Counter'], + is => 'ro', + isa => 'Num', + default => 0, + handles => { + inc_counter => 'inc', + dec_counter => 'dec', + reset_counter => 'reset', + }, + ); + + my $page = MyHomePage->new(); + $page->inc_counter; # same as $page->counter( $page->counter + 1 ); + $page->dec_counter; # same as $page->counter( $page->counter - 1 ); + + my $count_by_twos = 2; + $page->inc_counter($count_by_twos); + +=head1 DESCRIPTION + +This trait provides native delegation methods for counters. A counter can be +any sort of number (integer or not). The delegation methods allow you to +increment, decrement, or reset the value. + +=head1 DEFAULT TYPE + +If you don't provide an C<isa> value for your attribute, it will default to +C<Num>. + +=head1 PROVIDED METHODS + +=over 4 + +=item * B<set($value)> + +Sets the counter to the specified value and returns the new value. + +This method requires a single argument. + +=item * B<inc> + +=item * B<inc($arg)> + +Increases the attribute value by the amount of the argument, or by 1 if no +argument is given. This method returns the new value. + +This method accepts a single argument. + +=item * B<dec> + +=item * B<dec($arg)> + +Decreases the attribute value by the amount of the argument, or by 1 if no +argument is given. This method returns the new value. + +This method accepts a single argument. + +=item * B<reset> + +Resets the value stored in this slot to its default value, and returns the new +value. + +=back + +=head1 BUGS + +See L<Moose/BUGS> for details on reporting bugs. + +=head1 AUTHORS + +=over 4 + +=item * + +Stevan Little <stevan.little@iinteractive.com> + +=item * + +Dave Rolsky <autarch@urth.org> + +=item * + +Jesse Luehrs <doy@tozt.net> + +=item * + +Shawn M Moore <code@sartak.org> + +=item * + +יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> + +=item * + +Karen Etheridge <ether@cpan.org> + +=item * + +Florian Ragwitz <rafl@debian.org> + +=item * + +Hans Dieter Pearcey <hdp@weftsoar.net> + +=item * + +Chris Prather <chris@prather.org> + +=item * + +Matt S Trout <mst@shadowcat.co.uk> + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2006 by Infinity Interactive, Inc.. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Hash.pm b/lib/Moose/Meta/Attribute/Native/Trait/Hash.pm new file mode 100644 index 0000000..25fbc6b --- /dev/null +++ b/lib/Moose/Meta/Attribute/Native/Trait/Hash.pm @@ -0,0 +1,226 @@ +package Moose::Meta::Attribute::Native::Trait::Hash; +our $VERSION = '2.1405'; + +use Moose::Role; +with 'Moose::Meta::Attribute::Native::Trait'; + +sub _helper_type { 'HashRef' } + +no Moose::Role; + +1; + +# ABSTRACT: Helper trait for HashRef attributes + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Moose::Meta::Attribute::Native::Trait::Hash - Helper trait for HashRef attributes + +=head1 VERSION + +version 2.1405 + +=head1 SYNOPSIS + + package Stuff; + use Moose; + + has 'options' => ( + traits => ['Hash'], + is => 'ro', + isa => 'HashRef[Str]', + default => sub { {} }, + handles => { + set_option => 'set', + get_option => 'get', + has_no_options => 'is_empty', + num_options => 'count', + delete_option => 'delete', + option_pairs => 'kv', + }, + ); + +=head1 DESCRIPTION + +This trait provides native delegation methods for hash references. + +=head1 PROVIDED METHODS + +=over 4 + +=item B<get($key, $key2, $key3...)> + +Returns values from the hash. + +In list context it returns a list of values in the hash for the given keys. In +scalar context it returns the value for the last key specified. + +This method requires at least one argument. + +=item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)> + +Sets the elements in the hash to the given values. It returns the new values +set for each key, in the same order as the keys passed to the method. + +This method requires at least two arguments, and expects an even number of +arguments. + +=item B<delete($key, $key2, $key3...)> + +Removes the elements with the given keys. + +In list context it returns a list of values in the hash for the deleted +keys. In scalar context it returns the value for the last key specified. + +=item B<keys> + +Returns the list of keys in the hash. + +This method does not accept any arguments. + +=item B<exists($key)> + +Returns true if the given key is present in the hash. + +This method requires a single argument. + +=item B<defined($key)> + +Returns true if the value of a given key is defined. + +This method requires a single argument. + +=item B<values> + +Returns the list of values in the hash. + +This method does not accept any arguments. + +=item B<kv> + +Returns the key/value pairs in the hash as an array of array references. + + for my $pair ( $object->option_pairs ) { + print "$pair->[0] = $pair->[1]\n"; + } + +This method does not accept any arguments. + +=item B<elements> + +Returns the key/value pairs in the hash as a flattened list.. + +This method does not accept any arguments. + +=item B<clear> + +Resets the hash to an empty value, like C<%hash = ()>. + +This method does not accept any arguments. + +=item B<count> + +Returns the number of elements in the hash. Also useful to check for a nonempty hash, because C<count> returns a true (nonzero) value if there is something in the hash: +C<< has_options => 'count' >>. + +This method does not accept any arguments. + +=item B<is_empty> + +If the hash is populated, returns false. Otherwise, returns true. + +This method does not accept any arguments. + +=item B<accessor($key)> + +=item B<accessor($key, $value)> + +If passed one argument, returns the value of the specified key. If passed two +arguments, sets the value of the specified key. + +When called as a setter, this method returns the value that was set. + +=item B<shallow_clone> + +This method returns a shallow clone of the hash reference. The return value +is a reference to a new hash with the same keys and values. It is I<shallow> +because any values that were references in the original will be the I<same> +references in the clone. + +=back + +Note that C<each> is deliberately omitted, due to its stateful interaction +with the hash iterator. C<keys> or C<kv> are much safer. + +=head1 METHODS + +=over 4 + +=item B<meta> + +=back + +=head1 BUGS + +See L<Moose/BUGS> for details on reporting bugs. + +=head1 AUTHORS + +=over 4 + +=item * + +Stevan Little <stevan.little@iinteractive.com> + +=item * + +Dave Rolsky <autarch@urth.org> + +=item * + +Jesse Luehrs <doy@tozt.net> + +=item * + +Shawn M Moore <code@sartak.org> + +=item * + +יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> + +=item * + +Karen Etheridge <ether@cpan.org> + +=item * + +Florian Ragwitz <rafl@debian.org> + +=item * + +Hans Dieter Pearcey <hdp@weftsoar.net> + +=item * + +Chris Prather <chris@prather.org> + +=item * + +Matt S Trout <mst@shadowcat.co.uk> + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2006 by Infinity Interactive, Inc.. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Number.pm b/lib/Moose/Meta/Attribute/Native/Trait/Number.pm new file mode 100644 index 0000000..4851246 --- /dev/null +++ b/lib/Moose/Meta/Attribute/Native/Trait/Number.pm @@ -0,0 +1,155 @@ +package Moose::Meta::Attribute::Native::Trait::Number; +our $VERSION = '2.1405'; + +use Moose::Role; +with 'Moose::Meta::Attribute::Native::Trait'; + +sub _helper_type { 'Num' } + +no Moose::Role; + +1; + +# ABSTRACT: Helper trait for Num attributes + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Moose::Meta::Attribute::Native::Trait::Number - Helper trait for Num attributes + +=head1 VERSION + +version 2.1405 + +=head1 SYNOPSIS + + package Real; + use Moose; + + has 'integer' => ( + traits => ['Number'], + is => 'ro', + isa => 'Num', + default => 5, + handles => { + set => 'set', + add => 'add', + sub => 'sub', + mul => 'mul', + div => 'div', + mod => 'mod', + abs => 'abs', + }, + ); + + my $real = Real->new(); + $real->add(5); # same as $real->integer($real->integer + 5); + $real->sub(2); # same as $real->integer($real->integer - 2); + +=head1 DESCRIPTION + +This trait provides native delegation methods for numbers. All of the +operations correspond to arithmetic operations like addition or +multiplication. + +=head1 DEFAULT TYPE + +If you don't provide an C<isa> value for your attribute, it will default to +C<Num>. + +=head1 PROVIDED METHODS + +All of these methods modify the attribute's value in place. All methods return +the new value. + +=over 4 + +=item * B<add($value)> + +Adds the current value of the attribute to C<$value>. + +=item * B<sub($value)> + +Subtracts C<$value> from the current value of the attribute. + +=item * B<mul($value)> + +Multiplies the current value of the attribute by C<$value>. + +=item * B<div($value)> + +Divides the current value of the attribute by C<$value>. + +=item * B<mod($value)> + +Returns the current value of the attribute modulo C<$value>. + +=item * B<abs> + +Sets the current value of the attribute to its absolute value. + +=back + +=head1 BUGS + +See L<Moose/BUGS> for details on reporting bugs. + +=head1 AUTHORS + +=over 4 + +=item * + +Stevan Little <stevan.little@iinteractive.com> + +=item * + +Dave Rolsky <autarch@urth.org> + +=item * + +Jesse Luehrs <doy@tozt.net> + +=item * + +Shawn M Moore <code@sartak.org> + +=item * + +יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> + +=item * + +Karen Etheridge <ether@cpan.org> + +=item * + +Florian Ragwitz <rafl@debian.org> + +=item * + +Hans Dieter Pearcey <hdp@weftsoar.net> + +=item * + +Chris Prather <chris@prather.org> + +=item * + +Matt S Trout <mst@shadowcat.co.uk> + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2006 by Infinity Interactive, Inc.. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/Moose/Meta/Attribute/Native/Trait/String.pm b/lib/Moose/Meta/Attribute/Native/Trait/String.pm new file mode 100644 index 0000000..c919f3a --- /dev/null +++ b/lib/Moose/Meta/Attribute/Native/Trait/String.pm @@ -0,0 +1,187 @@ +package Moose::Meta::Attribute::Native::Trait::String; +our $VERSION = '2.1405'; + +use Moose::Role; +with 'Moose::Meta::Attribute::Native::Trait'; + +sub _helper_type { 'Str' } + +no Moose::Role; + +1; + +# ABSTRACT: Helper trait for Str attributes + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Moose::Meta::Attribute::Native::Trait::String - Helper trait for Str attributes + +=head1 VERSION + +version 2.1405 + +=head1 SYNOPSIS + + package MyHomePage; + use Moose; + + has 'text' => ( + traits => ['String'], + is => 'rw', + isa => 'Str', + default => q{}, + handles => { + add_text => 'append', + replace_text => 'replace', + }, + ); + + my $page = MyHomePage->new(); + $page->add_text("foo"); # same as $page->text($page->text . "foo"); + +=head1 DESCRIPTION + +This trait provides native delegation methods for strings. + +=head1 DEFAULT TYPE + +If you don't provide an C<isa> value for your attribute, it will default to +C<Str>. + +=head1 PROVIDED METHODS + +=over 4 + +=item * B<inc> + +Increments the value stored in this slot using the magical string autoincrement +operator. Note that Perl doesn't provide analogous behavior in C<-->, so +C<dec> is not available. This method returns the new value. + +This method does not accept any arguments. + +=item * B<append($string)> + +Appends to the string, like C<.=>, and returns the new value. + +This method requires a single argument. + +=item * B<prepend($string)> + +Prepends to the string and returns the new value. + +This method requires a single argument. + +=item * B<replace($pattern, $replacement)> + +Performs a regexp substitution (L<perlop/s>). There is no way to provide the +C<g> flag, but code references will be accepted for the replacement, causing +the regex to be modified with a single C<e>. C</smxi> can be applied using the +C<qr> operator. This method returns the new value. + +This method requires two arguments. + +=item * B<match($pattern)> + +Runs the regex against the string and returns the matching value(s). + +This method requires a single argument. + +=item * B<chop> + +Just like L<perlfunc/chop>. This method returns the chopped character. + +This method does not accept any arguments. + +=item * B<chomp> + +Just like L<perlfunc/chomp>. This method returns the number of characters +removed. + +This method does not accept any arguments. + +=item * B<clear> + +Sets the string to the empty string (not the value passed to C<default>). + +This method does not have a defined return value. + +This method does not accept any arguments. + +=item * B<length> + +Just like L<perlfunc/length>, returns the length of the string. + +=item * B<substr> + +This acts just like L<perlfunc/substr>. When called as a writer, it returns +the substring that was replaced, just like the Perl builtin. + +This method requires at least one argument, and accepts no more than three. + +=back + +=head1 BUGS + +See L<Moose/BUGS> for details on reporting bugs. + +=head1 AUTHORS + +=over 4 + +=item * + +Stevan Little <stevan.little@iinteractive.com> + +=item * + +Dave Rolsky <autarch@urth.org> + +=item * + +Jesse Luehrs <doy@tozt.net> + +=item * + +Shawn M Moore <code@sartak.org> + +=item * + +יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> + +=item * + +Karen Etheridge <ether@cpan.org> + +=item * + +Florian Ragwitz <rafl@debian.org> + +=item * + +Hans Dieter Pearcey <hdp@weftsoar.net> + +=item * + +Chris Prather <chris@prather.org> + +=item * + +Matt S Trout <mst@shadowcat.co.uk> + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2006 by Infinity Interactive, Inc.. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut |