Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / MooseX / LazyRequire.pm
1 package MooseX::LazyRequire;
2 our $VERSION = '0.04';
3
4 # ABSTRACT: Required attributes which fail only when trying to use them
5
6 use Moose::Exporter;
7 use aliased 'MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire';
8 use namespace::autoclean;
9
10
11 Moose::Exporter->setup_import_methods;
12
13 sub init_meta {
14     my ($class, %options) = @_;
15     return Moose::Util::MetaRole::apply_metaclass_roles(
16         for_class                 => $options{for_class},
17         attribute_metaclass_roles => [LazyRequire],
18     );
19 }
20
21 1;
22
23
24 __END__
25
26 =pod
27
28 =head1 NAME
29
30 MooseX::LazyRequire - Required attributes which fail only when trying to use them
31
32 =head1 VERSION
33
34 version 0.04
35
36 =head1 SYNOPSIS
37
38     package Foo;
39
40     use Moose;
41     use MooseX::LazyRequire;
42
43     has foo => (
44         is            => 'ro',
45         lazy_required => 1,
46     );
47
48     has bar => (
49         is      => 'ro',
50         builder => '_build_bar',
51     );
52
53     sub _build_bar { shift->foo }
54
55     Foo->new(foo => 42); # succeeds, foo and bar will be 42
56     Foo->new(bar => 42); # succeeds, bar will be 42
57     Foo->new;            # fails, neither foo nor bare were given
58
59 =head1 DESCRIPTION
60
61 This module adds a C<lazy_required> option to Moose attribute declarations.
62
63 The reader methods for all attributes with that option will throw an exception
64 unless a value for the attributes was provided earlier by a constructor
65 parameter or through a writer method.
66
67 =head1 CAVEATS
68
69 Apparently Moose roles don't have an attribute metaclass, so this module can't
70 easily apply its magic to attributes defined in roles. If you want to use
71 C<lazy_required> in role attributes, you'll have to apply the attribute trait
72 yourself:
73
74     has foo => (
75         traits        => ['LazyRequire'],
76         is            => 'ro',
77         lazy_required => 1,
78     );
79
80
81
82 =begin Pod::Coverage
83
84 init_meta
85
86 =end Pod::Coverage
87
88 =head1 AUTHOR
89
90   Florian Ragwitz <rafl@debian.org>
91
92 =head1 COPYRIGHT AND LICENSE
93
94 This software is copyright (c) 2009 by Florian Ragwitz.
95
96 This is free software; you can redistribute it and/or modify it under
97 the same terms as the Perl 5 programming language system itself.
98
99 =cut 
100
101