File Coverage

blib/lib/Siebel/Srvrmgr/Daemon/ActionStash.pm
Criterion Covered Total %
statement 20 28 71.4
branch 1 2 50.0
condition n/a
subroutine 5 7 71.4
pod 4 4 100.0
total 30 41 73.1


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::Daemon::ActionStash;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Siebel::Srvrmgr::Daemon::ActionStash - singleton to stash data returned by Siebel::Srvrmgr::Daemon::Action subclasses
8              
9             =head1 SYNOPSIS
10              
11             package MyAction;
12             use Moose;
13             use namespace::autoclean;
14              
15             extends 'Siebel::Srvrmgr::Daemon::Action';
16              
17             my $stash = Siebel::Srvrmgr::Daemon::ActionStash->instance();
18             $stash->set_stash([{foobar => foobar}, [qw(one two three)]]);
19              
20             package main;
21              
22             my $stash = Siebel::Srvrmgr::Daemon::ActionStash->instance();
23             my $something = $stash->shift_stash()
24             # do something with $something
25              
26             =head1 DESCRIPTION
27              
28             This class was created to enable the possibility to retrieve data from an L<Siebel::Srvrmgr::Daemon::Action> subclass invoked by L<Siebel::Srvrmg::Daemon> without
29             the need to return data from within the objects.
30              
31             Since Siebel::Srvrmgr::Daemon::ActionStash is a singleton, a reference of the already instantied object will always be returned when calling C<instance>. So, before
32             calling the method C<run> from a L<Siebel::Srvrmgr::Daemon> class instance, it is just a matter to call C<instance> and inside the L<Siebel::Srvrmgr::Daemon::Action> subclass,
33             call the C<initialize> method with the data that should be returned as parameter.
34              
35             The drawnback from this technique is that two Action objects cannot used the same Stash at the same time or data will be replace/lost: a ActionStash instance should be used
36             exclusively by a single Action subclass. If you have need to returned data from several L<Siebel::Srvrmgr::Daemon::Action> subclasses you must use a different method.
37              
38             Considering this situation, the interface of this class should be considered experimental and may be changed in the future releases.
39              
40             =cut
41              
42 4     4   3705 use warnings;
  4         9  
  4         159  
43 4     4   25 use strict;
  4         9  
  4         114  
44 4     4   1087 use MooseX::Singleton 0.29;
  4         118766  
  4         27  
45              
46             our $VERSION = '0.29'; # VERSION
47              
48             =pod
49              
50             =head1 ATTRIBUTES
51              
52             =head2 stash
53              
54             This attribute is a array reference of references. This means that it will accept B<any> reference to some data structure that you think it will be
55             useful (including objects).
56              
57             If undefined, this attribute will returned an empty array reference.
58              
59             =cut
60              
61             has stash => (
62             is => 'rw',
63             isa => 'ArrayRef[Ref]',
64             required => 0,
65             reader => 'get_stash',
66             writer => 'set_stash',
67             default => sub { return [] }
68             );
69              
70             =pod
71              
72             =head1 METHODS
73              
74             =head2 get_stash
75              
76             Returns the C<stash> attribute array reference.
77              
78             =head2 set_stash
79              
80             Sets the C<stash> attribute. Expects an array reference as parameter.
81              
82             Beware that such call will complete remove all other data stored in the stash. To add single items, see C<push_stash> method.
83              
84             =head2 push_stash
85              
86             Expects as parameter a reference.
87              
88             C<push>es a new reference into the C<stash> attribute.
89              
90             If there is no member in the C<stash> attribute, the method C<set_stash> will be invoked to set the attribute.
91              
92             =cut
93              
94             sub push_stash {
95 1     1 1 360 my ( $self, $ref ) = @_;
96 1         5 $DB::single = 1;
97 1         41 my $array_ref = $self->get_stash;
98              
99 1 50       26 if ( scalar( @{$array_ref} ) > 0 ) {
  1         5  
100 1         3 push( @{$array_ref}, $ref );
  1         3  
101             }
102             else {
103 0         0 $self->set_stash( [$ref] );
104             }
105              
106 1         5 return 1;
107              
108             }
109              
110             =head2 shift_stash
111              
112             C<shift>s the C<stash> attribute, removing the first item in the attribute and returning it.
113              
114             If there is not other member to be shift, it will return undef.
115              
116             =cut
117              
118             sub shift_stash {
119 16     16 1 1138 my $self = shift;
120 16         44 return shift( @{ $self->get_stash() } );
  16         1332  
121             }
122              
123             =head2 shift_all
124              
125             Retrieves all content from the C<stash> attribute, clean it up and returns the content.
126              
127             It is basically calling C<get_stash> and C<set_stash> with an empty array reference as parameter.
128              
129             =cut
130              
131             sub shift_all {
132 0     0 1   my $self = shift;
133 0           my $ref = $self->get_stash();
134 0           $self->set_stash( [] );
135 0           return $ref;
136             }
137              
138             =head2 pop_stash
139              
140             C<pop>s the C<stash> attribute, returning the last element.
141              
142             =cut
143              
144             sub pop_stash {
145 0     0 1   my $self = shift;
146 0           return pop( @{ $self->get_stash } );
  0            
147             }
148              
149             =head1 SEE ALSO
150              
151             =over
152              
153             =item *
154              
155             L<Siebel::Srvrmgr::Daemon::Action>
156              
157             =item *
158              
159             L<MooseX::Singleton>
160              
161             =back
162              
163             =head1 AUTHOR
164              
165             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
166              
167             =head1 COPYRIGHT AND LICENSE
168              
169             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
170              
171             This file is part of Siebel Monitoring Tools.
172              
173             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
174             it under the terms of the GNU General Public License as published by
175             the Free Software Foundation, either version 3 of the License, or
176             (at your option) any later version.
177              
178             Siebel Monitoring Tools is distributed in the hope that it will be useful,
179             but WITHOUT ANY WARRANTY; without even the implied warranty of
180             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
181             GNU General Public License for more details.
182              
183             You should have received a copy of the GNU General Public License
184             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
185              
186             =cut
187              
188             1;