File Coverage

blib/lib/MooseX/Singleton.pm
Criterion Covered Total %
statement 2 4 50.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 4 6 66.6


line stmt bran cond sub pod time code
1             package MooseX::Singleton;
2             BEGIN {
3 6     6   226743   $MooseX::Singleton::AUTHORITY = 'cpan:SARTAK';
4             }
5             {
6               $MooseX::Singleton::VERSION = '0.29';
7             }
8              
9 6     6   17161 use Moose 1.10 ();
  0            
  0            
10             use Moose::Exporter;
11             use MooseX::Singleton::Role::Object;
12             use MooseX::Singleton::Role::Meta::Class;
13             use MooseX::Singleton::Role::Meta::Instance;
14              
15              
16             Moose::Exporter->setup_import_methods( also => 'Moose' );
17              
18             sub init_meta {
19                 shift;
20                 my %p = @_;
21              
22                 Moose->init_meta(%p);
23              
24                 my $caller = $p{for_class};
25              
26                 Moose::Util::MetaRole::apply_metaroles(
27                     for => $caller,
28                     class_metaroles => {
29                         class => ['MooseX::Singleton::Role::Meta::Class'],
30                         instance =>
31                             ['MooseX::Singleton::Role::Meta::Instance'],
32                         constructor =>
33                             ['MooseX::Singleton::Role::Meta::Method::Constructor'],
34                     },
35                 );
36              
37                 Moose::Util::MetaRole::apply_base_class_roles(
38                     for_class => $caller,
39                     roles =>
40                         ['MooseX::Singleton::Role::Object'],
41                 );
42              
43                 return $caller->meta();
44             }
45              
46              
47             1;
48              
49             # ABSTRACT: turn your Moose class into a singleton
50              
51              
52              
53             =pod
54            
55             =head1 NAME
56            
57             MooseX::Singleton - turn your Moose class into a singleton
58            
59             =head1 VERSION
60            
61             version 0.29
62            
63             =head1 SYNOPSIS
64            
65             package MyApp;
66             use MooseX::Singleton;
67            
68             has env => (
69             is => 'rw',
70             isa => 'HashRef[Str]',
71             default => sub { \%ENV },
72             );
73            
74             package main;
75            
76             delete MyApp->env->{PATH};
77             my $instance = MyApp->instance;
78             my $same = MyApp->instance;
79            
80             =head1 DESCRIPTION
81            
82             A singleton is a class that has only one instance in an application.
83             C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
84             L<Moose> class to a singleton.
85            
86             All you should need to do to transform your class is to change C<use Moose> to
87             C<use MooseX::Singleton>. This module uses metaclass roles to do its magic, so
88             it should cooperate with most other C<MooseX> modules.
89            
90             =head1 METHODS
91            
92             A singleton class will have the following additional methods:
93            
94             =head2 Singleton->instance
95            
96             This returns the singleton instance for the given package. This method does
97             I<not> accept any arguments. If the instance does not yet exist, it is created
98             with its defaults values. This means that if your singleton requires
99             arguments, calling C<instance> will die if the object has not already been
100             initialized.
101            
102             =head2 Singleton->initialize(%args)
103            
104             This method can be called I<only once per class>. It explicitly initializes
105             the singleton object with the given arguments.
106            
107             =head2 Singleton->_clear_instance
108            
109             This clears the existing singleton instance for the class. Obviously, this is
110             meant for use only inside the class itself.
111            
112             =head2 Singleton->new
113            
114             This method currently works like a hybrid of C<initialize> and
115             C<instance>. However, calling C<new> directly will probably be deprecated in a
116             future release. Instead, call C<initialize> or C<instance> as appropriate.
117            
118             =head1 BUGS
119            
120             Please report any bugs or feature requests to
121             C<bug-moosex-singleton@rt.cpan.org>, or through the web interface at
122             L<http://rt.cpan.org>. We will be notified, and then you'll automatically be
123             notified of progress on your bug as we make changes.
124            
125             =head1 SOME CODE STOLEN FROM
126            
127             Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
128            
129             =head1 AND PATCHES FROM
130            
131             Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
132            
133             =head1 AUTHOR
134            
135             Shawn M Moore <sartak@gmail.com>
136            
137             =head1 COPYRIGHT AND LICENSE
138            
139             This software is copyright (c) 2011 by Shawn M Moore.
140            
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143            
144             =cut
145              
146              
147             __END__
148            
149            
150