File Coverage

blib/lib/Project/Environment.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Project::Environment;
2              
3             # ABSTRACT: Set and detect project environment via .environment file.
4              
5 1     1   2618 use Moose;
  0            
  0            
6             with 'Project::Environment::Role';
7             with 'MooseX::Role::Flyweight';
8              
9             use version; our $VERSION = version->new('v1.2.0');
10              
11             use overload '""' => sub { shift->project_environment };
12              
13              
14             has _caller => (
15             is => 'ro',
16             isa => 'ArrayRef',
17             default => sub {
18             for (0 .. 10) {
19             my @caller = caller($_);
20             return \@caller if $caller[3] eq 'Moose::Object::new';
21             }
22             },
23             );
24              
25             1; ## eof
26              
27             __END__
28              
29             =pod
30              
31             =encoding UTF-8
32              
33             =head1 NAME
34              
35             Project::Environment - Set and detect project environment via .environment file.
36              
37             =head1 VERSION
38              
39             version v1.2.0
40              
41             =head1 SYNOPSIS
42              
43             Add a .environment file into the root of your project:
44              
45             .
46             |-- .environment (<-- add this)
47             |-- .git
48             |-- lib
49             |-- MyApp
50             | |-- Environment.pm
51             |-- MyApp.pm
52              
53             Define a subclass for your application:
54              
55             package MyApp::Environment;
56              
57             use Moose;
58             extends 'Project::Environment';
59              
60             1;
61              
62             Now, somewhere inside your application code:
63              
64             my $env = MyApp::Environment->instance->project_environment; ## or ->env
65              
66             =head1 DESCRIPTION
67              
68             This module provides a way to determine the environment an application is
69             running in (e.g. development, production, testing, etc.).
70              
71             Mainly the environment is detected from C<.environment> file in the project
72             root.
73              
74             You can also set the environment via C<%ENV>.
75              
76             Most of the functionality defined and documented in
77             L<Project::Environment::Role>.
78              
79             This consumer class provides 2 things:
80              
81             =head2 singularity
82              
83             This isn't exactly a singleton. And all of the magic is provided by
84             L<MooseX::Role::Flyweight>.
85              
86             In short, all you have to do is call C<instance> constructor instead of C<new>
87             and you get only one instance of the object and the result of the figuring out
88             the environment is cached.
89              
90             =head2 stringification
91              
92             An instance of L<Project::Environment> will stringify into the
93             environment name properly. This is useful if you were to store the instance
94             of the L<Project::Environment> object in an attribute, rather than
95             the string name of the environment.
96              
97             has environment => (
98             is => 'ro',
99             default => sub { MyApp::Environment->instance },
100             );
101              
102             Somewhere else in the application code:
103              
104             if ($self->environment eq 'production') {
105             ## do not break
106             } else {
107             ## break everything
108             }
109              
110             =head1 AUTHOR
111              
112             Roman F. <romanf@cpan.org>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2013 by Roman F..
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut