File Coverage

blib/lib/MooX/ShortHas.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod n/a
total 32 32 100.0


line stmt bran cond sub pod time code
1             package MooX::ShortHas;
2              
3 2     4   147857 use strictures 2;
  2         31  
  2         75  
4              
5             our $VERSION = '1.202040'; # VERSION
6              
7             # ABSTRACT: shortcuts for common Moo has attribute configurations
8              
9             #
10             # This file is part of MooX-ShortHas
11             #
12             #
13             # Christian Walde has dedicated the work to the Commons by waiving all of his
14             # or her rights to the work worldwide under copyright law and all related or
15             # neighboring legal rights he or she had in the work, to the extent allowable by
16             # law.
17             #
18             # Works under CC0 do not require attribution. When citing the work, you should
19             # not imply endorsement by the author.
20             #
21              
22              
23 2     2   843 use Moo 2.003006 ();
  2         2584  
  2         41  
24 2     2   10 use Moo::_Utils qw(_install_tracked);
  2         2  
  2         409  
25              
26             sub _modified_has {
27 8     8   21 my ( $has, $mod, $name, @args ) = @_;
28 8         12 $has->( $name, @{$mod}, @args );
  8         25  
29             }
30              
31             sub import {
32 4     4   8426 my $caller = caller;
33 4 100       50 my $has = $caller->can( "has" ) or die "Moo not loaded in caller: $caller";
34             my %mods = (
35             lazy => [qw( is lazy builder )],
36 3         9 map { $_ => [ is => $_ => required => 1 ] } qw( ro rwp rw )
  9         26  
37             );
38 3         19 for my $mod ( keys %mods ) {
39             _install_tracked $caller => $mod => sub {
40 8     8   34798 _modified_has $has, $mods{$mod}, @_;
        8      
        8      
        10      
        10      
        2      
41 12         240 };
42             }
43             }
44              
45             1;
46              
47             __END__
48              
49             =pod
50              
51             =head1 NAME
52              
53             MooX::ShortHas - shortcuts for common Moo has attribute configurations
54              
55             =head1 VERSION
56              
57             version 1.202040
58              
59             =head1 SYNOPSIS
60              
61             Instead of:
62              
63             use Moo;
64            
65             has hro => is => ro => required => 1;
66             has hlazy => is => lazy => builder => sub { 2 };
67             has hrwp => is => rwp => required => 1;
68             has hrw => is => rw => required => 1;
69              
70             You can now write:
71              
72             use Moo;
73             use MooX::ShortHas;
74            
75             ro "hro";
76             lazy hlazy => sub { 2 };
77             rwp "hrwp";
78             rw "hrw";
79              
80             And options can be added or overriden by appending them:
81              
82             ro hro_opt => required => 0;
83              
84             =head1 DESCRIPTION
85              
86             L<Moo>'s C<has> asks developers to repeat themselves a lot to set up attributes,
87             and since its inceptions the most common configurations of attributes have
88             crystallized through long usage.
89              
90             This module provides sugar shortcuts that wrap around has under the appropriate
91             names to reduce the effort of setting up an attribute to naming it with a
92             shortcut.
93              
94             =head1 EXPORTS
95              
96             =head2 ro, rwp, rw
97              
98             These three work the same, they convert a call like this:
99              
100             ro $name => @extra_args;
101              
102             To this corresponding has call:
103              
104             has $name => is => ro => required => 1 => @extra_args;
105              
106             The appending of extra args makes it easy to override the required if
107             necessary.
108              
109             =head2 lazy
110              
111             This one is slightly different than the others, as lazy arguments don't require
112             a constructor value, but almost always want a builder of some kind:
113              
114             lazy $name => @extra_args;
115              
116             Corresponds to:
117              
118             has $name => is => lazy => builder => @extra_args;
119              
120             The first extra argument is thus expected to be any of the values appropriate
121             for the builder option.
122              
123             =head1 SEE ALSO
124              
125             =over
126              
127             =item *
128              
129             L<Mu> - automatically wraps this module into Moo
130              
131             =item *
132              
133             L<Mu::Role> - automatically wraps this module into Moo::Role
134              
135             =item *
136              
137             L<Mus> - Mu but with slightly more typing and strict constructors
138              
139             =back
140              
141             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
142              
143             =head1 SUPPORT
144              
145             =head2 Bugs / Feature Requests
146              
147             Please report any bugs or feature requests through the issue tracker
148             at L<https://github.com/wchristian/MooX-ShortHas/issues>.
149             You will be notified automatically of any progress on your issue.
150              
151             =head2 Source Code
152              
153             This is open source software. The code repository is available for
154             public review and contribution under the terms of the license.
155              
156             L<https://github.com/wchristian/MooX-ShortHas>
157              
158             git clone https://github.com/wchristian/MooX-ShortHas.git
159              
160             =head1 AUTHOR
161              
162             Christian Walde <walde.christian@gmail.com>
163              
164             =head1 CONTRIBUTORS
165              
166             =for stopwords Graham Knop Zakariyya Mughal mst - Matt S. Trout (cpan:MSTROUT)
167              
168             =over 4
169              
170             =item *
171              
172             Graham Knop <haarg@haarg.org>
173              
174             =item *
175              
176             Zakariyya Mughal <zaki.mughal@gmail.com>
177              
178             =item *
179              
180             mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
181              
182             =back
183              
184             =head1 COPYRIGHT AND LICENSE
185              
186              
187             Christian Walde has dedicated the work to the Commons by waiving all of his
188             or her rights to the work worldwide under copyright law and all related or
189             neighboring legal rights he or she had in the work, to the extent allowable by
190             law.
191              
192             Works under CC0 do not require attribution. When citing the work, you should
193             not imply endorsement by the author.
194              
195             =cut