MouseX::AttributeHelpers::Collection::ImmutableHash - MouseX::AttributeHelpers::Collection::ImmutableHash documentation


MouseX-AttributeHelpers documentation Contained in the MouseX-AttributeHelpers distribution.

Index


Code Index:

NAME

Top

MouseX::AttributeHelpers::Collection::ImmutableHash

SYNOPSIS

Top

    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',
        },
    );

DESCRIPTION

Top

This module provides a immutable HashRef attribute which provides a number of hash-line operations.

PROVIDERS

Top

count

empty

exists

get

keys

values

kv

METHODS

Top

method_constructors

helper_type

AUTHOR

Top

NAKAGAWA Masaki <masaki@cpan.org>

LICENSE

Top

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

MouseX::AttributeHelpers, MouseX::AttributeHelpers::Base


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__