MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire - Attribute trait to make getters fail on unset attributes


MooseX-LazyRequire documentation Contained in the MooseX-LazyRequire distribution.

Index


Code Index:

NAME

Top

MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire - Attribute trait to make getters fail on unset attributes

AUTHORS

Top

COPYRIGHT AND LICENSE

Top


MooseX-LazyRequire documentation Contained in the MooseX-LazyRequire distribution.

package MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire;
BEGIN {
  $MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire::AUTHORITY = 'cpan:FLORA';
}
BEGIN {
  $MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire::VERSION = '0.07';
}
# ABSTRACT: Attribute trait to make getters fail on unset attributes

use Moose::Role;
use Carp qw/cluck/;
use MooseX::Types::Moose qw/Bool/;
use namespace::autoclean;

has lazy_required => (
    is       => 'ro',
    isa      => Bool,
    required => 1,
    default  => 0,
);

after _process_options => sub {
    my ($class, $name, $options) = @_;

    if (exists $options->{lazy_require}) {
        cluck "deprecated option 'lazy_require' used. use 'lazy_required' instead.";
        $options->{lazy_required} = delete $options->{lazy_require};
    }

    return unless $options->{lazy_required};

    Moose->throw_error(
        "You may not use both a builder or a default and lazy_required for one attribute ($name)",
        data => $options,
    ) if $options->{builder};

    $options->{ lazy     } = 1;
    $options->{ required } = 1;
    $options->{ default  } = sub {
        confess "Attribute $name must be provided before calling reader"
    };
};

package # hide
    Moose::Meta::Attribute::Custom::Trait::LazyRequire;

sub register_implementation { 'MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire' }

1;

__END__