# Copyright 2013-2014 Stefan Goebel.
#
# This file is part of Newcomen.
#
# Newcomen is free software: you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Newcomen is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with Newcomen. If not, see
# .
package Newcomen::Util::Traits;
our $VERSION = 2014052501;
use strict;
use warnings;
use Exporter ();
our @ISA = qw( Exporter );
our @EXPORT_OK = qw( std_array_handles std_hash_handles );
sub std_array_handles {{
'clear' => 'clear',
'count' => 'count',
'empty' => 'is_empty',
'find' => 'first',
'first' => 'first_index',
'grep' => 'grep',
'insert' => 'insert',
'item' => 'get',
'items' => 'elements',
'map' => 'map',
'ordered' => 'sort',
'paginate' => 'natatime',
'push' => 'push',
'remove' => 'delete',
'sort' => 'sort_in_place',
'splice' => 'splice',
'unshift' => 'unshift',
}}
sub std_hash_handles {{
'defined' => 'defined',
'delete' => 'delete',
'elements' => 'kv',
'exists' => 'exists',
'get' => 'get',
'keys' => 'keys',
'set' => 'set',
'total' => 'count',
'values' => 'values',
'void' => 'is_empty',
'wipe' => 'clear',
}}
1;
__END__
####################################################################################################
=head1 NAME
Newcomen::Util::Traits - Moose related utility functions.
=head1 SYNOPSIS
package MyPackage;
use namespace::autoclean;
use Moose;
use Newcomen::Util::Traits;
has 'array' => (
'is' => 'ro',
'isa' => 'ArrayRef',
'default' => sub {[]},
'traits' => ['Array'],
'handles' => Newcomen::Util::Traits::std_array_handles (),
);
has 'hash' => (
'is' => 'ro',
'isa' => 'HashRef',
'default' => sub {{}},
'traits' => ['Hash'],
'handles' => Newcomen::Util::Traits::std_hash_handles (),
);
__PACKAGE__ -> meta () -> make_immutable ();
1;
Using that package:
use MyPackage;
my $instance = MyPackage -> new ();
# Add a value to the array attribute:
$instance -> push ($value);
# Add an entry to the hash attribute:
$instance -> set ('key', $value);
=head1 DESCRIPTION
This package provides utility functions for L classes. It is not a L class itself.
Note that all functions may be imported into the package using I:
use Newcomen::Util::Traits qw( std_array_handles std_hash_handles );
No function is exported by default.
If you followed a link from another module's documentation to this page and are looking for the
array and/or hash methods, see L below. In that case you may ignore the section
L. Keep in mind that these instance methods are directly available as instance methods
of the class that led you here (any exceptions are mentioned in the class' documentation).
=head1 FUNCTIONS
=head2 std_array_handles, std_hash_handles
has 'array' => (
'is' => 'ro',
'isa' => 'ArrayRef',
'default' => sub {[]},
'traits' => ['Array'],
'handles' => std_array_handles (),
);
has 'hash' => (
'is' => 'ro',
'isa' => 'HashRef',
'default' => sub {{}},
'traits' => ['Hash'],
'handles' => std_hash_handles (),
);
The two functions I and I may be used to add a default set
of handles to a L attribute (either an arrayref attribute or a hashref attribute,
respectively). Both functions return a hashref containing the mapping of method names to L's
native traits (see L for array traits and
L for hash traits). The handles are named in a way that
allows a class to include one arrayref attribute using the array handles and one hashref attribute
using the hash handles at the same time without clashing method names. See below for the list of
methods that will be available to the classes.
=head1 INSTANCE METHODS
The following methods will be available in the classes using one of I'
functions, i.e. these are B I' methods!
=head2 Array Methods
=head3 clear
$instance -> clear ();
Handle for the I native trait.
Removes all elements from the array. This method does not accept any parameters, the return value is
not defined.
=head3 count
my $number = $instance -> count ();
Handle for the I native trait.
Returns the number of elements in the array. This method does not accept any parameters.
=head3 empty
if ($instance -> empty ()) {
# do something...
}
Handle for the I native trait.
Returns a true value if the array does not contain any elements, a false value if there is at least
one element. This method does not accept any parameters.
=head3 find
my $element = $instance -> find (sub { ... });
Handle for the I native trait.
Returns the first matching element of the array. The parameter has to be a subroutine reference. It
will be called against each array element until one matches or all elements have been checked. If no
matching element can be found, the method will return C.
=head3 first
my $index = $instance -> first (sub { ... });
Handle for the I native trait.
Like L (see above), but instead of returning the matching element itself this method
returns the matching element's index in the array, or C<-1> if there is no match.
=head3 grep
my @matching = $instance -> grep (sub { ... });
Handle for the I native trait.
Like Perl's I function, this method returns a list of all matching elements of the array.
The parameter has to be a subroutine reference that will be called against each array element.
=head3 insert
$instance -> insert ($index, $value);
Handle for the I native trait.
Inserts a new element (second parameter) at the specified index (first parameter) into the array.
Returns the new value at the specified index.
=head3 item
my $item = $instance -> item ($index);
Handle for the I native trait.
Returns the element of the array at the specified index (the only parameter). A negative index may
be used, like in Perl's array handling. If the specified element does not exist, the method will
return C.
=head3 items
my @items = $instance -> items ();
Handle for the I native trait.
Returns all the items in the array as a list. This method does not accept any parameters.
=head3 map
my @stuff = $instance -> map (sub { ... });
Handle for the I