Re: MooseX::OmniTrigger

[email protected] ("Todd Lorenz")
Newsgroups perl.moose
Message-ID <[email protected]>
A possibly more readable example attached :)

-----Original Message----- 
From: Jason Galea
Sent: Sunday, December 18, 2011 9:36 PM
To: Todd Lorenz
Cc: [email protected]
Subject: Re: MooseX::OmniTrigger

On Mon, Dec 19, 2011 at 2:02 PM, Todd Lorenz <[email protected]> wrote:

>
> So far as IÂ’ve been able to find, there currently arenÂ’t any other modules
> like this one, so IÂ’d like to make it available on the CPAN. But first IÂ’d
> like your feedback, if youÂ’ve got the time to give it.
>
> https://github.com/trlorenz/MooseX-OmniTrigger
>

Hi Todd,

have you seen these?

https://metacpan.org/search?q=moose+observable

MooseX::Observer::Role::Observable
MooseX::Role::Listenable

Observable looks like it might be close, it's brand new too..

are they similar? or what are the differences? (alway good to explain in
your docs)

cheers,

J



-- 

Jason Galea
Web Developer

Ph 07 40556926
Mob 04 12345 534
www.eightdegrees.com.au
example.pl (application/octet-stream, 2.6 KB)
require 5.10.0; use feature ':5.10'; use strict; use warnings; use warnings (FATAL => qw(misc numeric uninitialized)); # use autodie;

#===================================================================================================

{ package OmniTrigClass;

    use Moose;
    use MooseX::OmniTrigger;

    has foo => (is => 'rw', isa => 'Str', default => 'FOO',            omnitrigger => \&_callback);
    has bar => (is => 'rw', isa => 'Str', default => 'BAR', lazy => 1, omnitrigger => \&_callback);

    has baz => (is => 'rw', isa => 'Str', accessor => 'access_baz', reader => 'read_baz', writer => 'write_baz', clearer => 'clear_baz', omnitrigger => sub {

        my ($self) = shift;

        $self->_callback(@_);

        $self->write_baz('RECURSE, DAMN YOU');
    });

    sub _callback { say("OMNITRIGGERED FOR $_[1]") }
}

my $omnitriggered = OmniTrigClass->new;

# OMNITRIGGERED FOR foo

$omnitriggered->bar;

# OMNITRIGGERED FOR bar

$omnitriggered->meta->get_attribute('foo')->set_value($omnitriggered, 'FOO!!!');

# OMNITRIGGERED FOR foo

$omnitriggered->write_baz('BAZ???');

# OMNITRIGGERED FOR baz

say $omnitriggered->read_baz;

# RECURSE, DAMN YOU

#===================================================================================================

{ package ObservableClass;

    use Moose;

    has foo => (is => 'rw', isa => 'Str', default => 'FOO'           );
    has bar => (is => 'rw', isa => 'Str', default => 'BAR', lazy => 1);

    has baz => (is => 'rw', isa => 'Str', accessor => 'access_baz', reader => 'read_baz', writer => 'write_baz', clearer => 'clear_baz');

    # WE HAVE TO BE EXPLICIT ABOUT WHAT WE'RE OBSERVING. THIS LIST COULD GET BIG.
    with 'MooseX::Observer::Role::Observable' => {notify_after => [qw(

        foo
        bar
        access_baz
        read_baz
        write_baz
        clear_baz
    )]};
}

{ package ObserverClass;

    use Moose;
        with 'MooseX::Observer::Role::Observer';

    sub update {

        say("OBSERVED $_[3]");

        # WE HAVE TO MAINTAIN OUR OWN "DISPATCHER" HERE.
        $_[1]->write_baz('RECURSE, DAMN YOU') if $_[3] =~ /^(?:access_baz|read_baz|write_baz|clear_baz)$/;
    }
}

my $observable = ObservableClass->new;

$observable->add_observer(ObserverClass->new); # <NOTHING>

$observable->bar; # <NOTHING>

$observable->meta->get_attribute('foo')->set_value($observable, 'FOO!!!'); # <NOTHING>

$observable->write_baz('BAZ???');

# OBSERVED write_baz
# OBSERVED write_baz
# OBSERVED write_baz
# ...INIFINITE RECURSION

#===================================================================================================
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.