File Coverage

blib/lib/MooseX/LazyRequire.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package MooseX::LazyRequire;
2             # git description: v0.10-7-gf996968
3             $MooseX::LazyRequire::VERSION = '0.11';
4             # ABSTRACT: Required attributes which fail only when trying to use them
5             # KEYWORDS: moose extension attribute required lazy defer populate method
6              
7 3     3   1519370 use Moose 0.94 ();
  3         544902  
  3         92  
8 3     3   29 use Moose::Exporter;
  3         6  
  3         37  
9 3     3   3765 use aliased 0.30 'MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire';
  3         2670  
  3         18  
10 3     3   335 use namespace::autoclean;
  3         6  
  3         13  
11              
12             #pod =head1 SYNOPSIS
13             #pod
14             #pod package Foo;
15             #pod
16             #pod use Moose;
17             #pod use MooseX::LazyRequire;
18             #pod
19             #pod has foo => (
20             #pod is => 'ro',
21             #pod lazy_required => 1,
22             #pod );
23             #pod
24             #pod has bar => (
25             #pod is => 'ro',
26             #pod builder => '_build_bar',
27             #pod );
28             #pod
29             #pod sub _build_bar { shift->foo }
30             #pod
31             #pod
32             #pod Foo->new(foo => 42); # succeeds, foo and bar will be 42
33             #pod Foo->new(bar => 42); # succeeds, bar will be 42
34             #pod Foo->new; # fails, neither foo nor bare were given
35             #pod
36             #pod =head1 DESCRIPTION
37             #pod
38             #pod This module adds a C<lazy_required> option to Moose attribute declarations.
39             #pod
40             #pod The reader methods for all attributes with that option will throw an exception
41             #pod unless a value for the attributes was provided earlier by a constructor
42             #pod parameter or through a writer method.
43             #pod
44             #pod =head1 CAVEATS
45             #pod
46             #pod Prior to Moose 1.9900, roles didn't have an attribute metaclass, so this module can't
47             #pod easily apply its magic to attributes defined in roles. If you want to use
48             #pod C<lazy_required> in role attributes, you'll have to apply the attribute trait
49             #pod yourself:
50             #pod
51             #pod has foo => (
52             #pod traits => ['LazyRequire'],
53             #pod is => 'ro',
54             #pod lazy_required => 1,
55             #pod );
56             #pod
57             #pod With Moose 1.9900, you can use this module in roles just the same way you can
58             #pod in classes.
59             #pod
60             #pod =cut
61              
62             my %metaroles = (
63             class_metaroles => {
64             attribute => [LazyRequire],
65             },
66             );
67              
68             $metaroles{role_metaroles} = {
69             applied_attribute => [LazyRequire],
70             }
71             if $Moose::VERSION >= 1.9900;
72              
73             Moose::Exporter->setup_import_methods(%metaroles);
74              
75             1;
76              
77             __END__
78              
79             =pod
80              
81             =encoding UTF-8
82              
83             =head1 NAME
84              
85             MooseX::LazyRequire - Required attributes which fail only when trying to use them
86              
87             =head1 VERSION
88              
89             version 0.11
90              
91             =head1 SYNOPSIS
92              
93             package Foo;
94              
95             use Moose;
96             use MooseX::LazyRequire;
97              
98             has foo => (
99             is => 'ro',
100             lazy_required => 1,
101             );
102              
103             has bar => (
104             is => 'ro',
105             builder => '_build_bar',
106             );
107              
108             sub _build_bar { shift->foo }
109              
110              
111             Foo->new(foo => 42); # succeeds, foo and bar will be 42
112             Foo->new(bar => 42); # succeeds, bar will be 42
113             Foo->new; # fails, neither foo nor bare were given
114              
115             =head1 DESCRIPTION
116              
117             This module adds a C<lazy_required> option to Moose attribute declarations.
118              
119             The reader methods for all attributes with that option will throw an exception
120             unless a value for the attributes was provided earlier by a constructor
121             parameter or through a writer method.
122              
123             =head1 CAVEATS
124              
125             Prior to Moose 1.9900, roles didn't have an attribute metaclass, so this module can't
126             easily apply its magic to attributes defined in roles. If you want to use
127             C<lazy_required> in role attributes, you'll have to apply the attribute trait
128             yourself:
129              
130             has foo => (
131             traits => ['LazyRequire'],
132             is => 'ro',
133             lazy_required => 1,
134             );
135              
136             With Moose 1.9900, you can use this module in roles just the same way you can
137             in classes.
138              
139             =for Pod::Coverage init_meta
140              
141             =head1 AUTHORS
142              
143             =over 4
144              
145             =item *
146              
147             Florian Ragwitz <rafl@debian.org>
148              
149             =item *
150              
151             Dave Rolsky <autarch@urth.org>
152              
153             =back
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2009 by Florian Ragwitz.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =head1 CONTRIBUTORS
163              
164             =for stopwords Karen Etheridge David Precious Jesse Luehrs
165              
166             =over 4
167              
168             =item *
169              
170             Karen Etheridge <ether@cpan.org>
171              
172             =item *
173              
174             David Precious <davidp@preshweb.co.uk>
175              
176             =item *
177              
178             Jesse Luehrs <doy@tozt.net>
179              
180             =back
181              
182             =cut