MooseX-OmniTrigger

 view release on metacpan or  search on metacpan

t/001-basic.t  view on Meta::CPAN

use strict; use warnings; use warnings (FATAL => qw(misc numeric uninitialized)); # use autodie;

use Test::Fatal;
use Test::More;

use Moose::Util::TypeConstraints;

subtype 'Exciting',
    as 'Str',
        where { /!$/ };

coerce 'Exciting',
    from 'Str',
        via { "$_!" };

no Moose::Util::TypeConstraints;

my $stuff = <<'END';
    has poo => (is => 'rw', isa => 'Str', omnitrigger => sub {

        my ($self_aka_instance, $attr_name, $new_val, $old_val) = (shift, @_);

        ::isa_ok($self_aka_instance, $class, '1st arg to omnitrigger');

        ::is($attr_name, 'poo', '2nd arg to omnitrigger is the attribute name');

        ::isa_ok($new_val, 'ARRAY', '3rd arg to omnitrigger');
        ::isa_ok($old_val, 'ARRAY', '4th arg to omnitrigger');
    });

    has foo => (is => 'rw', isa => 'Exciting', coerce => 1,                          clearer => '_clear_foo', omnitrigger => \&_capture_changes);
    has bar => (is => 'rw', isa => 'Exciting', coerce => 1, default => 'DEFAULT'   ,                          omnitrigger => \&_capture_changes);
    has baz => (is => 'rw', isa => 'Exciting', coerce => 1, builder => '_build_bar',                          omnitrigger => \&_capture_changes);

    has blo => (is => 'ro', isa => 'Exciting', coerce => 1, default => 'DEFAULT', omnitrigger => \&_capture_changes);

    has goo => (is => 'rw', isa => 'Exciting', coerce => 1, default => 'DEFAULT'   , lazy => 1, clearer => '_clear_goo', omnitrigger => \&_capture_changes);
    has moo => (is => 'rw', isa => 'Exciting', coerce => 1, builder => '_build_bar', lazy => 1, clearer => '_clear_moo', omnitrigger => \&_capture_changes);

    has changes => (is => 'ro', isa => 'HashRef', default => sub { {} });

    has biz => (is => 'rw', isa => 'ArrayRef', weak_ref => 1,                                              omnitrigger => \&_capture_weaklings);
    has buz => (is => 'rw', isa => 'ArrayRef', weak_ref => 1, default => sub { $arrayref_buz }, lazy => 1, omnitrigger => \&_capture_weaklings);
    has bez => (is => 'rw', isa => 'ArrayRef', weak_ref => 1, default => sub { $arrayref_bez }, lazy => 1, omnitrigger => \&_capture_weaklings);

    has weaklings => (is => 'ro', isa => 'HashRef', default => sub { {} });

    has ziz => (is => 'rw', isa => 'Exciting', coerce => 1,

        initializer => sub {

            my ($self, $value, $set, $attr) = @_;

            $set->("$value-INITIALIZER");
        },

        omnitrigger => sub {

            my ($self_aka_instance, $attr_name, $new_val, $old_val) = (shift, @_);

            $self_aka_instance->meta->get_attribute('ziz')->set_raw_value($self_aka_instance, "$new_val->[0]-OMNITRIG");
        },
    );

    sub _capture_changes {

        my ($self, $attr_name, $new, $old) = (shift, @_);

        push(@{$self->changes->{$attr_name}}, sprintf('%s=>%s',

            @$old ? defined($old->[0]) ? $old->[0] : 'UNDEF' : 'NOVAL',
            @$new ? defined($new->[0]) ? $new->[0] : 'UNDEF' : 'NOVAL',
        ));
    }

    sub _capture_weaklings { $_[0]->weaklings->{$_[1]} = Scalar::Util::isweak($_[0]{$_[1]}) }

    sub _build_bar { 'BUILDER' }
END

my $class;

my $arrayref_buz;
my $arrayref_biz;
my $arrayref_bez;

eval(qq[
    { package MyRoleA; use Moose::Role;                 use MooseX::OmniTrigger; $stuff; }
    { package MyRoleB; use Moose::Role; with 'MyRoleA';                                  }
    { package MyRoleC; use Moose::Role; with 'MyRoleB';                                  }
    { package MyRoleZ; use Moose::Role;                                                  }

    { package MyClassA; use Moose; use MooseX::OmniTrigger; $stuff; }

    { package MyClassB; use Moose; with 'MyRoleA'; }
    { package MyClassC; use Moose; with 'MyRoleC'; }

    { package MyClassD; use Moose; with 'MyRoleA'; with 'MyRoleZ'; }
    { package MyClassE; use Moose; with 'MyRoleZ'; with 'MyRoleA'; }

    { package MyClassF; use Moose; with qw(MyRoleA MyRoleZ); }
]);

{
    my $x = $@;



( run in 2.471 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )