line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MoobX::Trait::Observer; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
3
|
|
|
|
|
|
|
# ABSTRACT: turn a Moose attribute into a MoobX observer |
4
|
|
|
|
|
|
|
$MoobX::Trait::Observer::VERSION = '0.1.0'; |
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
6119
|
use Moose::Role; |
|
8
|
|
|
|
|
25457
|
|
|
8
|
|
|
|
|
26
|
|
7
|
8
|
|
|
8
|
|
30923
|
use MoobX::Observer; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
207
|
|
8
|
|
|
|
|
|
|
|
9
|
8
|
|
|
8
|
|
27
|
use experimental 'signatures'; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
54
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Moose::Util::meta_attribute_alias('Observer'); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
before _process_options => sub { |
14
|
|
|
|
|
|
|
my( $self, $name, $args) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $gen = $args->{default}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$args->{lazy} //= 1; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
$args->{default} = sub { |
21
|
|
|
|
|
|
|
my @args = @_; |
22
|
|
|
|
|
|
|
MoobX::Observer->new( |
23
|
|
|
|
|
|
|
generator => sub { $gen->(@args) }, |
24
|
|
|
|
|
|
|
autorun => !$args->{lazy}, |
25
|
|
|
|
|
|
|
) |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
1; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
__END__ |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=pod |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=encoding UTF-8 |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 NAME |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
MoobX::Trait::Observer - turn a Moose attribute into a MoobX observer |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 VERSION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
version 0.1.0 |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 SYNOPSIS |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
package Person; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
use MoobX; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
our $OPENING :Observable = 'Dear'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has name => ( |
55
|
|
|
|
|
|
|
traits => [ 'Observable' ], |
56
|
|
|
|
|
|
|
is => 'rw', |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has address => ( |
60
|
|
|
|
|
|
|
is => 'ro', |
61
|
|
|
|
|
|
|
traits => [ 'Observer' ], |
62
|
|
|
|
|
|
|
default => sub { |
63
|
|
|
|
|
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
join ' ', $Person::OPENING, $self->name |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $person = Person->new( name => 'Wilfred' ); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
print $person->address; # Dear Wilfred |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
$Person::OPENING = 'My very dear'; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
print $person->address; # My very dear Wilfred |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 DESCRIPTION |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Turns an object attribute into an observer. The C<default> argument |
79
|
|
|
|
|
|
|
is used as the value-generating function. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
By default the attribute will be considered to be lazy. |
82
|
|
|
|
|
|
|
If the C<lazy> attribute is explicitly |
83
|
|
|
|
|
|
|
set to C<false>, then the observer will be of the C<autorun> |
84
|
|
|
|
|
|
|
variety. Be careful, though, as it'll probably not do what you want if you |
85
|
|
|
|
|
|
|
observe other attributes. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
package MyThing; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use MoobX; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
has foo => ( |
92
|
|
|
|
|
|
|
is => [ 'Observable' ], |
93
|
|
|
|
|
|
|
); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
has bar => ( |
96
|
|
|
|
|
|
|
is => [ 'Observer' ], |
97
|
|
|
|
|
|
|
lazy => 0, |
98
|
|
|
|
|
|
|
default => sub { |
99
|
|
|
|
|
|
|
my $self = shift; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# OOPS! If 'bar' is processed before 'foo' |
102
|
|
|
|
|
|
|
# at the object init stage, `$self->foo` |
103
|
|
|
|
|
|
|
# will not be an observable yet, so `bar` |
104
|
|
|
|
|
|
|
# will be set to be `1` and never react to anything |
105
|
|
|
|
|
|
|
$self->foo + 1; |
106
|
|
|
|
|
|
|
}, |
107
|
|
|
|
|
|
|
); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 AUTHOR |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Yanick Champoux <yanick@cpan.org> |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Yanick Champoux. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
118
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |