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