X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FAttributeHelpers%2FMethodProvider%2FImmutableHash.pm;h=3bb6a3abdc87922d074ec6364db7838f9d9c0229;hb=2e74144c8801485e5c33fdcdcbee9f7d882c1a3a;hp=1cf5123c0ec1bdef12dcc871e095354cc9e338eb;hpb=99c62fb8cb5b5da1d5832062364ce48e1dd15800;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm index 1cf5123..3bb6a3a 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm @@ -1,16 +1,22 @@ package MooseX::AttributeHelpers::MethodProvider::ImmutableHash; use Moose::Role; -our $VERSION = '0.03'; +our $VERSION = '0.23'; +$VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; sub exists : method { - my ($attr, $reader, $writer) = @_; + my ($attr, $reader, $writer) = @_; return sub { CORE::exists $reader->($_[0])->{$_[1]} ? 1 : 0 }; -} +} + +sub defined : method { + my ($attr, $reader, $writer) = @_; + return sub { CORE::defined $reader->($_[0])->{$_[1]} ? 1 : 0 }; +} sub get : method { - my ($attr, $reader, $writer) = @_; + my ($attr, $reader, $writer) = @_; return sub { if ( @_ == 2 ) { $reader->($_[0])->{$_[1]} @@ -23,32 +29,42 @@ sub get : method { sub keys : method { my ($attr, $reader, $writer) = @_; - return sub { CORE::keys %{$reader->($_[0])} }; + return sub { CORE::keys %{$reader->($_[0])} }; } - + sub values : method { my ($attr, $reader, $writer) = @_; - return sub { CORE::values %{$reader->($_[0])} }; -} + return sub { CORE::values %{$reader->($_[0])} }; +} sub kv : method { my ($attr, $reader, $writer) = @_; - return sub { + return sub { my $h = $reader->($_[0]); map { [ $_, $h->{$_} ] - } CORE::keys %{$h} - }; + } CORE::keys %{$h} + }; +} + +sub elements : method { + my ($attr, $reader, $writer) = @_; + return sub { + my $h = $reader->($_[0]); + map { + $_, $h->{$_} + } CORE::keys %{$h} + }; } - + sub count : method { my ($attr, $reader, $writer) = @_; - return sub { scalar CORE::keys %{$reader->($_[0])} }; + return sub { scalar CORE::keys %{$reader->($_[0])} }; } sub empty : method { my ($attr, $reader, $writer) = @_; - return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 }; + return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 }; } 1; @@ -60,10 +76,10 @@ __END__ =head1 NAME MooseX::AttributeHelpers::MethodProvider::ImmutableHash - + =head1 DESCRIPTION -This is a role which provides the method generators for +This is a role which provides the method generators for L. =head1 METHODS @@ -80,23 +96,45 @@ L. =item B +Returns the number of elements in the list. + =item B +If the list is populated, returns true. Otherwise, returns false. + =item B +Returns true if the given key is present in the hash + +=item B + +Returns true if the value of a given key is defined + =item B +Returns an element of the hash by its key. + =item B +Returns the list of keys in the hash. + =item B +Returns the list of values in the hash. + =item B +Returns the key, value pairs in the hash as array references + +=item B + +Returns the key, value pairs in the hash as a flattened list + =back =head1 BUGS -All complex software has bugs lurking in it, and this module is no +All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. @@ -106,7 +144,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2007-2008 by Infinity Interactive, Inc. +Copyright 2007-2009 by Infinity Interactive, Inc. L