| MouseX-AttributeHelpers documentation | Contained in the MouseX-AttributeHelpers distribution. |
MouseX::AttributeHelpers::Collection::ImmutableHash
package MyClass;
use Mouse;
use MouseX::AttributeHelpers;
has 'options' => (
metaclass => 'Collection::ImmutableHash',
is => 'rw',
isa => 'HashRef',
default => sub { +{} },
provides => {
get => 'get_option',
empty => 'has_options',
keys => 'get_option_list',
},
);
This module provides a immutable HashRef attribute which provides a number of hash-line operations.
NAKAGAWA Masaki <masaki@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| MouseX-AttributeHelpers documentation | Contained in the MouseX-AttributeHelpers distribution. |
package MouseX::AttributeHelpers::Collection::ImmutableHash; use Mouse; extends 'MouseX::AttributeHelpers::Base'; has '+method_constructors' => ( default => sub { return +{ exists => sub { my ($attr, $name) = @_; return sub { exists $_[0]->$name()->{$_[1]} ? 1 : 0 }; }, get => sub { my ($attr, $name) = @_; return sub { @_ == 2 ? $_[0]->$name()->{$_[1]} : @{ shift->$name() }{@_} }; }, keys => sub { my ($attr, $name) = @_; return sub { keys %{ $_[0]->$name() } }; }, values => sub { my ($attr, $name) = @_; return sub { values %{ $_[0]->$name() } }; }, kv => sub { my ($attr, $name) = @_; return sub { my $h = $_[0]->$name(); map { [ $_, $h->{$_} ] } keys %$h; }; }, count => sub { my ($attr, $name) = @_; return sub { scalar keys %{ $_[0]->$name() } }; }, empty => sub { my ($attr, $name) = @_; return sub { scalar keys %{ $_[0]->$name() } ? 1 : 0 }; }, }; }, ); sub helper_type { 'HashRef' } no Mouse; __PACKAGE__->meta->make_immutable(inline_constructor => 0); __END__