File Coverage

blib/lib/Siebel/Srvrmgr/Daemon/ActionStash.pm
Criterion Covered Total %
statement 20 21 95.2
branch 1 2 50.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 28 30 93.3


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              
24             # do something with the get_stash method
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   2086 use warnings;
  4         8  
  4         140  
43 4     4   23 use strict;
  4         9  
  4         89  
44 4     4   2820 use MooseX::Singleton;
  4         124216  
  4         66  
45              
46             =pod
47              
48             =head1 ATTRIBUTES
49              
50             =head2 stash
51              
52             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
53             useful (including objects).
54              
55             If undefined, this attribute will returned an empty array reference.
56              
57             =cut
58              
59             has stash => (
60             is => 'rw',
61             isa => 'ArrayRef[Ref]',
62             required => 0,
63             reader => 'get_stash',
64             writer => 'set_stash',
65             default => sub { return [] }
66             );
67              
68             =pod
69              
70             =head1 METHODS
71              
72             =head2 get_stash
73              
74             Returns the C<stash> attribute array reference.
75              
76             =head2 set_stash
77              
78             Sets the C<stash> attribute. Expects an array reference as parameter.
79              
80             Beware that such call will complete remove all other data stored in the stash. To add single items, see C<push_stash> method.
81              
82             =head2 push_stash
83              
84             Expects as parameter a reference.
85              
86             Pushes a new reference into the C<stash> attribute.
87              
88             If there is no member in the C<stash> attribute, the method C<set_stash> will be invoked to set the attribute.
89              
90             =cut
91              
92             sub push_stash {
93              
94 1     1 1 366 my $self = shift;
95 1         2 my $ref = shift;
96              
97 1         49 my $array_ref = $self->get_stash;
98              
99 1 50       24 if ( scalar( @{$array_ref} ) > 0 ) {
  1         6  
100              
101 1         2 push( @{$array_ref}, $ref );
  1         3  
102              
103             }
104             else {
105              
106 0         0 $self->set_stash( [$ref] );
107              
108             }
109              
110 1         5 return 1;
111              
112             }
113              
114             =head2 shift_stash
115              
116             Shifts the C<stash> attribute, removing the first item in the attribute and returning it.
117              
118             If there is not other member to be shift, it will return undef.
119              
120             =cut
121              
122             sub shift_stash {
123              
124 16     16 1 2036 my $self = shift;
125              
126 16         36 return shift( @{ $self->get_stash() } );
  16         803  
127              
128             }
129              
130             =head1 SEE ALSO
131              
132             =over
133              
134             =item *
135              
136             L<Siebel::Srvrmgr::Daemon::Action>
137              
138             =item *
139              
140             L<MooseX::Singleton>
141              
142             =back
143              
144             =head1 AUTHOR
145              
146             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
151              
152             This file is part of Siebel Monitoring Tools.
153              
154             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
155             it under the terms of the GNU General Public License as published by
156             the Free Software Foundation, either version 3 of the License, or
157             (at your option) any later version.
158              
159             Siebel Monitoring Tools is distributed in the hope that it will be useful,
160             but WITHOUT ANY WARRANTY; without even the implied warranty of
161             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
162             GNU General Public License for more details.
163              
164             You should have received a copy of the GNU General Public License
165             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
166              
167             =cut
168              
169             1;