File Coverage

blib/lib/Config/Model/Role/WarpMaster.pm
Criterion Covered Total %
statement 47 47 100.0
branch 10 12 83.3
condition 5 5 100.0
subroutine 10 10 100.0
pod 4 4 100.0
total 76 78 97.4


line stmt bran cond sub pod time code
1             #
2             # This file is part of Config-Model
3             #
4             # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5             #
6             # This is free software, licensed under:
7             #
8             # The GNU Lesser General Public License, Version 2.1, February 1999
9             #
10             package Config::Model::Role::WarpMaster 2.153; # TRIAL
11              
12             # ABSTRACT: register and trigger a warped element
13              
14 59     59   32854 use Mouse::Role;
  59         165  
  59         532  
15 59     59   22195 use strict;
  59         207  
  59         1391  
16 59     59   393 use warnings;
  59         210  
  59         2191  
17              
18 59     59   442 use Mouse::Util;
  59         186  
  59         458  
19 59     59   5060 use Log::Log4perl qw(get_logger :levels);
  59         205  
  59         525  
20 59     59   8335 use Scalar::Util qw/weaken/;
  59         190  
  59         35937  
21              
22             my $logger = get_logger("Warper");
23              
24             has 'warp_these_objects' => (
25             traits => ['Array'],
26             is => 'ro',
27             isa => 'ArrayRef',
28             default => sub { [] },
29             handles => {
30             _slave_info => 'elements',
31             _add_slave_info => 'push',
32             _delete_slave => 'delete',
33             has_warped_slaves => 'count',
34             # find_slave_idx => 'first_index', not available in Mouse
35             },
36             );
37              
38             sub register {
39 482     482 1 1249 my ( $self, $warped, $warper_name ) = @_;
40              
41 482         1066 my $w_name = $warped->name;
42 482 100       1449 $logger->debug( $self->get_type . ": " . $self->name, " registered $w_name ($warper_name)" )
43             if $logger->is_debug;
44              
45             # weaken only applies to the passed reference, and there's no way
46             # to duplicate a weak ref. Only a strong ref is created. See
47             # qw(weaken) module for weaken()
48 482         3672 my @tmp = ( $warped, $w_name, $warper_name );
49 482         2158 weaken( $tmp[0] );
50 482         1871 $self->_add_slave_info( \@tmp );
51              
52 482 100       8157 return defined $self->{compute} ? 'computed' : 'regular';
53             }
54              
55             sub unregister {
56 4     4 1 10 my ( $self, $w_name ) = @_;
57 4 50       10 $logger->debug( $self->get_type .": " . $self->name, " unregister $w_name" )
58             if $logger->is_debug;
59              
60 4         25 my $idx = 0;
61 4         10 foreach my $info ($self->_slave_info) {
62 7 50       77 last if $info->[0] eq $w_name ;
63 7         16 $idx++;
64             }
65              
66 4         12 $self->_delete_slave($idx);
67 4         50 return;
68             }
69              
70             # And I'm going to warp them ...
71             sub trigger_warp {
72 123     123 1 277 my $self = shift;
73 123         214 my $value = shift;
74 123   100     507 my $str_val = shift // $value // 'undefined';
      100        
75              
76 123         378 foreach my $ref ( $self->_slave_info ) {
77 375         1863 my ( $warped, $w_name, $warp_index ) = @$ref;
78 375 100       860 next unless defined $warped; # $warped is a weak ref and may vanish
79              
80             # pure warp of object
81 340 100       927 if ($logger->is_debug) {
82 3         26 $logger->debug("trigger_warp: ".$self->get_type." ", $self->name,
83             " warps '$w_name' with value <$str_val> ");
84             }
85 340         2580 $warped->trigger( $value, $warp_index );
86             }
87 122         371 return;
88             }
89              
90             sub get_warped_slaves {
91 1350     1350 1 2478 my $self = shift;
92              
93             # grep is used to clean up weak ref to object that were destroyed
94 1350         3742 return grep { defined $_ } map { $_->[0] } $self->_slave_info;
  331         763  
  331         1565  
95             }
96              
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             Config::Model::Role::WarpMaster - register and trigger a warped element
108              
109             =head1 VERSION
110              
111             version 2.153
112              
113             =head1 SYNOPSIS
114              
115             package Config::Model::Stuff;
116             use Mouse;
117             with Config::Model::Role::WarpMaster
118              
119             =head1 DESCRIPTION
120              
121             This role enable a configuration element to become a warp maser, i.e. a parameter
122             whose value can change the features of the configuration tree (by controlling a
123             warped_node) or the feature of various elements like leaf, hash ...
124              
125             =head1 METHODS
126              
127             =head2 register
128              
129             Parameters: C<< ( $warped_object, warper_name ) >>
130              
131             Register a new warped object. Called by an element which has a C<warp> parameter.
132             This method is calling on the object pointed by C<follow> value.
133              
134             =head2 unregister
135              
136             Parameters: C<< ( warper_name ) >>
137              
138             Remove a warped object from the object controlled by this warp master.
139              
140             =head2 trigger_warp
141              
142             Parameters: C<< ( value, stringified_value ) >>
143              
144             Called by the object using this role when the value held by this object is changed (i.e.
145             something like store was called). The passed value can be a plain scalar (from a value
146             object) or a hash (from a check_list object). The stringified_value is a string shown
147             in debug log.
148              
149             -head2 has_warped_slaves
150              
151             Return the number of object controlled by this master.
152              
153             =head2 get_warped_slaves
154              
155             Return a list of object controlled by this master.
156              
157             =head1 AUTHOR
158              
159             Dominique Dumont
160              
161             =head1 COPYRIGHT AND LICENSE
162              
163             This software is Copyright (c) 2005-2022 by Dominique Dumont.
164              
165             This is free software, licensed under:
166              
167             The GNU Lesser General Public License, Version 2.1, February 1999
168              
169             =cut