MoobX.pm

filter: Statementstmt 96.1% 75 / 78 Branchbran 68.7% 11 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 16 / 16 Podpod 50.0% 3 / 6 totaltotal 89.0% 106 / 119 CC 13 SCAR 12.0
CC
13
Cov
91%
observable_ref
6.5
_exporter_expand_sub
6.0
observable_modified
2.0
CRAP 3.3
« MoobX/Hash/Observable.pm ↑ Summary MoobX/Scalar.pm »
1 package MoobX;
2 11 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Reactive programming framework heavily inspired by JavaScript's MobX
4 11 $MoobX::VERSION = '0.1.2';
5  
6 19 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use MoobX::Observer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 our @DEPENDENCIES;
11 11 our $WATCHING = 0;
12  
13 11 ▶use Scalar::Util qw/ reftype refaddr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Moose::Util qw/ with_traits /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Graph::Directed;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 11 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 11 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 11 our @EXPORT = qw/ observer observable autorun :attributes :traits /;
23 11 our %EXPORT_TAGS = (
24 'attributes' => [ '__ATTRIBUTES__' ],
25 'traits' => [ '__TRAITS__' ],
26 );
27  
28 11 our $WARN_NO_DEPS = 1;
29  
30 85 ▶sub _exporter_expand_sub {
Subroutine _exporter_expand_sub: called CC=6 SCAR=18.0
31 85 my ( $class, $name, $args, $globals ) = ( shift, @_ );
32  
33 85 ▶ if ( $name eq '__ATTRIBUTES__' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '__ATTRIBUTES__') { } 17 68 85
elsif ($name eq '__TRAITS__') { } 17 51 68
34 17 my $target = $globals->{into};
35 17 ▶ return if ref $target;
Branch: 1 branch
BranchTrueFalseTotal
if ref $target 0 17 17
36 17 local $@;
37 17 ▶ eval qq{
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "\n package $target;\n use parent 'MoobX::Attributes';\n 1;\n " or die $@ 17 0 17
38 package $target;
39 use parent 'MoobX::Attributes';
40 1;
41 } or die $@;
42 17 return;
43 }
44 elsif ( $name eq '__TRAITS__' ) {
45 17 use_module( "MoobX::Trait::$_" ) for qw/ Observer Observable /;
46 17 return;
47 }
48  
49 51 return $class->SUPER::_exporter_expand_sub( $name, $args, $globals );
50 }
51  
52 11 our $graph = Graph::Directed->new;
53  
54 108 ▶sub observable_modified($obs) {
Subroutine observable_modified: called CC=2 SCAR=6.9
Pod undocumented
55  
56 108 my @preds = $graph->all_predecessors( refaddr $obs );
57  
58 108 for my $pred ( @preds ) {
59 32 my $info = $graph->get_vertex_attribute(
60 $pred, 'info'
61 );
62  
63 32 local @MoobX::DEPENDENCIES = ( @MoobX::DEPENDENCIES, $obs );
64 32 $info->clear_value;
65 }
66 }
67  
68 36 ▶sub dependencies_for($self,@deps) {
Subroutine dependencies_for: called CC=2 SCAR=6.9
Pod undocumented
69 $graph->delete_edges(
70 map {
71 36 refaddr $self => $_
72 } $graph->successors(refaddr $self)
73 );
74  
75 $graph->add_edges(
76 36 map { refaddr $self => refaddr $_ } @deps
77 );
78  
79 $graph->set_vertex_attribute(
80 refaddr $_, info => $_
81 36 ) for $self, @deps;
82 }
83  
84 11 ▶sub observable :prototype(\[$%@]) {
Subroutine observable: called CC=1 SCAR=0.0
Pod documented
85 11 observable_ref( @_ );
86 }
87  
88 31 ▶sub observable_ref {
Subroutine observable_ref: called CC=6 SCAR=18.7
Pod undocumented
89 31 my $ref = shift;
90  
91 31 my $type = reftype $ref;
92  
93 31 ▶ my $class = 'MoobX::'. ucfirst lc $type || 'SCALAR';
Condition50%1/2
'MoobX::' . ucfirst(lc $type) || 'SCALAR'
l!l
310
MC/DC0%0/1
'MoobX::' . ucfirst(lc $type) || 'SCALAR'
'MoobX::' . ucfirst(lc $type)
Truth table50%1/2
A
A= 'MoobX::' . ucfirst(lc $type)
Aresult
11
00
94  
95 $class = with_traits(
96 62 map { use_module($_) }
97 31 map { $_, $_ . '::Observable' } $class
98 );
99  
100 31 ▶ if( $type eq 'SCALAR' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR') { } 9 22 31
elsif ($type eq 'ARRAY') { } 17 5 22
elsif ($type eq 'HASH') { } 5 0 5
elsif (not $type) { } 0 0 0
101 9 my $value = $$ref;
102 9 tie $$ref, $class;
103 9 $$ref = $value;
104 }
105 elsif( $type eq 'ARRAY' ) {
106 17 my @values = @$ref;
107 17 tie @$ref, $class;
108 17 @$ref = @values;
109 }
110 elsif( $type eq 'HASH' ) {
111 5 my %values = %$ref;
112 5 tie %$ref, $class;
113 5 %$ref = %values;
114 }
115 elsif( not $type ) {
116 0 my $value = $ref;
117 0 tie $ref, $class;
118 0 $ref = $value;
119 }
120  
121  
122 31 return $ref;
123  
124 }
125  
126 6 ▶sub observer :prototype(&) { MoobX::Observer->new( generator => @_ ) }
Subroutine observer: called CC=1 SCAR=0.0
Pod documented
127 4 ▶sub autorun :prototype(&) { MoobX::Observer->new( autorun => 1, generator => @_ ) }
Subroutine autorun: called CC=1 SCAR=0.0
Pod documented
128  
129 11 1;
130  
131 __END__
« MoobX/Hash/Observable.pm ↑ Summary MoobX/Scalar.pm »