File Coverage

blib/lib/MooseX/Singleton.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 28 29 96.5


line stmt bran cond sub pod time code
1             package MooseX::Singleton; # git description: 0.25-37-g820a8b5
2              
3 7     7   664605 use Moose 1.10 ();
  7         1762336  
  7         231  
4 7     7   45 use Moose::Exporter;
  7         8  
  7         42  
5 7     7   4218 use MooseX::Singleton::Role::Object;
  7         14  
  7         179  
6 7     7   2720 use MooseX::Singleton::Role::Meta::Class;
  7         14  
  7         208  
7 7     7   34 use MooseX::Singleton::Role::Meta::Instance;
  7         7  
  7         768  
8              
9             our $VERSION = '0.30';
10              
11             Moose::Exporter->setup_import_methods( also => 'Moose' );
12              
13             sub init_meta {
14 9     9 0 41941 shift;
15 9         32 my %p = @_;
16              
17 9         81 Moose->init_meta(%p);
18              
19 9         30194 my $caller = $p{for_class};
20              
21 9         69 Moose::Util::MetaRole::apply_metaroles(
22             for => $caller,
23             class_metaroles => {
24             class => ['MooseX::Singleton::Role::Meta::Class'],
25             instance =>
26             ['MooseX::Singleton::Role::Meta::Instance'],
27             constructor =>
28             ['MooseX::Singleton::Role::Meta::Method::Constructor'],
29             },
30             );
31              
32 9         84188 Moose::Util::MetaRole::apply_base_class_roles(
33             for_class => $caller,
34             roles =>
35             ['MooseX::Singleton::Role::Object'],
36             );
37              
38 9         27742 return $caller->meta();
39             }
40              
41              
42             1;
43              
44             # ABSTRACT: Turn your Moose class into a singleton
45              
46             __END__
47              
48             =pod
49              
50             =encoding UTF-8
51              
52             =head1 NAME
53              
54             MooseX::Singleton - Turn your Moose class into a singleton
55              
56             =head1 VERSION
57              
58             version 0.30
59              
60             =head1 SYNOPSIS
61              
62             package MyApp;
63             use MooseX::Singleton;
64              
65             has env => (
66             is => 'rw',
67             isa => 'HashRef[Str]',
68             default => sub { \%ENV },
69             );
70              
71             package main;
72              
73             delete MyApp->env->{PATH};
74             my $instance = MyApp->instance;
75             my $same = MyApp->instance;
76              
77             =head1 DESCRIPTION
78              
79             A singleton is a class that has only one instance in an application.
80             C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
81             L<Moose> class to a singleton.
82              
83             All you should need to do to transform your class is to change C<use Moose> to
84             C<use MooseX::Singleton>. This module uses metaclass roles to do its magic, so
85             it should cooperate with most other C<MooseX> modules.
86              
87             =head1 METHODS
88              
89             A singleton class will have the following additional methods:
90              
91             =head2 Singleton->instance
92              
93             This returns the singleton instance for the given package. This method does
94             I<not> accept any arguments. If the instance does not yet exist, it is created
95             with its defaults values. This means that if your singleton requires
96             arguments, calling C<instance> will die if the object has not already been
97             initialized.
98              
99             =head2 Singleton->initialize(%args)
100              
101             This method can be called I<only once per class>. It explicitly initializes
102             the singleton object with the given arguments.
103              
104             =head2 Singleton->_clear_instance
105              
106             This clears the existing singleton instance for the class. Obviously, this is
107             meant for use only inside the class itself.
108              
109             =head2 Singleton->new
110              
111             This method currently works like a hybrid of C<initialize> and
112             C<instance>. However, calling C<new> directly will probably be deprecated in a
113             future release. Instead, call C<initialize> or C<instance> as appropriate.
114              
115             =for Pod::Coverage init_meta
116              
117             =head1 SOME CODE STOLEN FROM
118              
119             =for stopwords Anders
120              
121             Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
122              
123             =head1 AND PATCHES FROM
124              
125             Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
126              
127             =head1 SUPPORT
128              
129             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Singleton>
130             (or L<bug-MooseX-Singleton@rt.cpan.org|mailto:bug-MooseX-Singleton@rt.cpan.org>).
131              
132             There is also a mailing list available for users of this distribution, at
133             L<http://lists.perl.org/list/moose.html>.
134              
135             There is also an irc channel available for users of this distribution, at
136             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
137              
138             =head1 AUTHOR
139              
140             Shawn M Moore <code@sartak.org>
141              
142             =head1 CONTRIBUTORS
143              
144             =for stopwords Dave Rolsky Karen Etheridge Ricardo SIGNES Kaare Rasmussen Anders Nor Berle Jonathan Rockway Hans Dieter Pearcey
145              
146             =over 4
147              
148             =item *
149              
150             Dave Rolsky <autarch@urth.org>
151              
152             =item *
153              
154             Karen Etheridge <ether@cpan.org>
155              
156             =item *
157              
158             Ricardo SIGNES <rjbs@cpan.org>
159              
160             =item *
161              
162             Kaare Rasmussen <kaare@jasonic.dk>
163              
164             =item *
165              
166             Anders Nor Berle <berle@cpan.org>
167              
168             =item *
169              
170             Jonathan Rockway <jon@jrock.us>
171              
172             =item *
173              
174             Hans Dieter Pearcey <hdp@weftsoar.net>
175              
176             =back
177              
178             =head1 COPYRIGHT AND LICENSE
179              
180             This software is copyright (c) 2007 by Shawn M Moore.
181              
182             This is free software; you can redistribute it and/or modify it under
183             the same terms as the Perl 5 programming language system itself.
184              
185             =cut