File Coverage

blib/lib/Data/Object/State.pm
Criterion Covered Total %
statement 61 61 100.0
branch 3 4 75.0
condition 2 6 33.3
subroutine 16 16 100.0
pod 2 3 66.6
total 84 90 93.3


line stmt bran cond sub pod time code
1             package Data::Object::State;
2              
3 1     1   32734 use 5.014;
  1         5  
4              
5 1     1   6 use strict;
  1         1  
  1         22  
6 1     1   5 use warnings;
  1         2  
  1         23  
7              
8 1     1   5 use Moo;
  1         1  
  1         5  
9              
10 1     1   820 use parent 'Moo';
  1         340  
  1         5  
11              
12 1     1   64 no warnings 'redefine';
  1         2  
  1         92  
13              
14             our $VERSION = '2.00'; # VERSION
15              
16             # BUILD
17              
18             my %seen;
19              
20             sub import {
21 3     3   51645 my ($class) = @_;
22              
23 3         9 my $target = caller;
24              
25 3 50       12 return if $seen{$target}++;
26              
27 1     1   7 eval "package $target; use Moo; 1;";
  1     1   7  
  1     1   13  
  1         7  
  1         2  
  1         5  
  1         7  
  1         3  
  1         6  
  3         251  
28              
29 1     1   5 no strict 'refs';
  1         2  
  1         183  
30              
31 3         20 *{"${target}::renew"} = $class->can('renew');
  3         28  
32 3         29 *{"${target}::singleton"} = $class->can('singleton');
  3         13  
33 3         10 *{"${target}::BUILD"} = $class->can('BUILD');
  3         12  
34              
35 3         244 return;
36             }
37              
38             sub BUILD {
39 6     6 1 5450 my ($self, $args) = @_;
40              
41 6         32 $_[0] = $self->singleton($args);
42              
43 6         109 return $self;
44             }
45              
46             # METHODS
47              
48             sub renew {
49 1     1 1 7 my ($self, @args) = @_;
50              
51 1   33     5 my $class = ref($self) || $self;
52              
53 1     1   8 no strict 'refs';
  1         1  
  1         97  
54              
55 1         3 undef ${"${class}::data"};
  1         4  
56              
57 1         19 return $class->new(@args);
58             }
59              
60             sub singleton {
61 6     6 0 13 my ($self, $args) = @_;
62              
63 6   33     32 my $class = ref($self) || $self;
64              
65 1     1   7 no strict 'refs';
  1         9  
  1         122  
66              
67 6 100       8 ${"${class}::data"} = {%$self, %$args} if !${"${class}::data"};
  4         11  
  6         33  
68              
69 6         10 return $_[0] = bless ${"${class}::data"}, $class;
  6         33  
70             }
71              
72             1;
73              
74             =encoding utf8
75              
76             =head1 NAME
77              
78             Data::Object::State
79              
80             =cut
81              
82             =head1 ABSTRACT
83              
84             Singleton Builder for Perl 5
85              
86             =cut
87              
88             =head1 SYNOPSIS
89              
90             package Example;
91              
92             use Data::Object::State;
93              
94             has data => (
95             is => 'ro'
96             );
97              
98             package main;
99              
100             my $example = Example->new;
101              
102             =cut
103              
104             =head1 DESCRIPTION
105              
106             This package provides an abstract base class for creating singleton classes.
107             This package is derived from L<Moo> and makes consumers Moo classes (with all
108             that that entails). This package also injects a C<BUILD> method which is
109             responsible for hooking into the build process and returning the appropriate
110             state.
111              
112             =cut
113              
114             =head1 METHODS
115              
116             This package implements the following methods:
117              
118             =cut
119              
120             =head2 new
121              
122             renew() : Object
123              
124             The new method sets the internal state and returns a new class instance.
125             Subsequent calls to C<new> will return the same instance as was previously
126             returned.
127              
128             =over 4
129              
130             =item new example #1
131              
132             package Example::New;
133              
134             use Data::Object::State;
135              
136             has data => (
137             is => 'ro'
138             );
139              
140             my $example1 = Example::New->new(data => 'a');
141             my $example2 = Example::New->new(data => 'b');
142              
143             [$example1, $example2]
144              
145             =back
146              
147             =cut
148              
149             =head2 renew
150              
151             renew() : Object
152              
153             The renew method resets the internal state and returns a new class instance.
154             Each call to C<renew> will discard the previous state, then reconstruct and
155             stash the new state as requested.
156              
157             =over 4
158              
159             =item renew example #1
160              
161             package Example::Renew;
162              
163             use Data::Object::State;
164              
165             has data => (
166             is => 'ro'
167             );
168              
169             my $example1 = Example::Renew->new(data => 'a');
170             my $example2 = $example1->renew(data => 'b');
171             my $example3 = Example::Renew->new(data => 'c');
172              
173             [$example1, $example2, $example3]
174              
175             =back
176              
177             =cut
178              
179             =head1 AUTHOR
180              
181             Al Newkirk, C<awncorp@cpan.org>
182              
183             =head1 LICENSE
184              
185             Copyright (C) 2011-2019, Al Newkirk, et al.
186              
187             This is free software; you can redistribute it and/or modify it under the terms
188             of the The Apache License, Version 2.0, as elucidated in the L<"license
189             file"|https://github.com/iamalnewkirk/data-object-state/blob/master/LICENSE>.
190              
191             =head1 PROJECT
192              
193             L<Wiki|https://github.com/iamalnewkirk/data-object-state/wiki>
194              
195             L<Project|https://github.com/iamalnewkirk/data-object-state>
196              
197             L<Initiatives|https://github.com/iamalnewkirk/data-object-state/projects>
198              
199             L<Milestones|https://github.com/iamalnewkirk/data-object-state/milestones>
200              
201             L<Contributing|https://github.com/iamalnewkirk/data-object-state/blob/master/CONTRIBUTE.md>
202              
203             L<Issues|https://github.com/iamalnewkirk/data-object-state/issues>
204              
205             =cut