File Coverage

blib/lib/Venus/Role/Stashable.pm
Criterion Covered Total %
statement 20 20 100.0
branch 6 6 100.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 1 3 33.3
total 35 38 92.1


line stmt bran cond sub pod time code
1             package Venus::Role::Stashable;
2              
3 34     34   659 use 5.018;
  34         134  
4              
5 34     34   196 use strict;
  34         102  
  34         782  
6 34     34   177 use warnings;
  34         80  
  34         1053  
7              
8 34     34   206 use Venus::Role 'with';
  34         118  
  34         342  
9              
10             # BUILDERS
11              
12             sub BUILD {
13 486     486 0 1470 my ($self, $data) = @_;
14              
15 486 100 50     8713 $self->{'$stash'} = delete $data->{'$stash'} || {} if !$self->{'$stash'};
16              
17 486         1302 return $self;
18             };
19              
20             # METHODS
21              
22             sub stash {
23 2836     2836 1 31159 my ($self, $key, $value) = @_;
24              
25 2836 100       10984 return $self->{'$stash'} if !exists $_[1];
26              
27 440 100       1447 return $self->{'$stash'}->{$key} if !exists $_[2];
28              
29 297         904 $self->{'$stash'}->{$key} = $value;
30              
31 297         919 return $value;
32             }
33              
34             # EXPORTS
35              
36             sub EXPORT {
37 64     64 0 253 ['stash']
38             }
39              
40             1;
41              
42              
43              
44             =head1 NAME
45              
46             Venus::Role::Stashable - Stashable Role
47              
48             =cut
49              
50             =head1 ABSTRACT
51              
52             Stashable Role for Perl 5
53              
54             =cut
55              
56             =head1 SYNOPSIS
57              
58             package Example;
59              
60             use Venus::Class;
61              
62             with 'Venus::Role::Stashable';
63              
64             attr 'test';
65              
66             package main;
67              
68             my $example = Example->new(test => time);
69              
70             # $example->stash;
71              
72             =cut
73              
74             =head1 DESCRIPTION
75              
76             This package modifies the consuming package and provides methods for stashing
77             data within the object.
78              
79             =cut
80              
81             =head1 METHODS
82              
83             This package provides the following methods:
84              
85             =cut
86              
87             =head2 stash
88              
89             stash(Any $key, Any $value) (Any)
90              
91             The stash method is used to fetch and stash named values associated with the
92             object. Calling this method without arguments returns all values.
93              
94             I>
95              
96             =over 4
97              
98             =item stash example 1
99              
100             package main;
101              
102             my $example = Example->new(test => time);
103              
104             my $stash = $example->stash;
105              
106             # {}
107              
108             =back
109              
110             =over 4
111              
112             =item stash example 2
113              
114             package main;
115              
116             my $example = Example->new(test => time);
117              
118             my $stash = $example->stash('test', {1..4});
119              
120             # { 1 => 2, 3 => 4 }
121              
122             =back
123              
124             =over 4
125              
126             =item stash example 3
127              
128             package main;
129              
130             my $example = Example->new(test => time);
131              
132             my $stash = $example->stash('test');
133              
134             # undef
135              
136             =back
137              
138             =cut
139              
140             =head1 AUTHORS
141              
142             Awncorp, C
143              
144             =cut
145              
146             =head1 LICENSE
147              
148             Copyright (C) 2000, Al Newkirk.
149              
150             This program is free software, you can redistribute it and/or modify it under
151             the terms of the Apache license version 2.0.
152              
153             =cut