File Coverage

blib/lib/MooseX/OneArgNew.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package MooseX::OneArgNew;
2             $MooseX::OneArgNew::VERSION = '0.005';
3 2     2   7701 use MooseX::Role::Parameterized 1.01;
  2         180866  
  2         14  
4             # ABSTRACT: teach ->new to accept single, non-hashref arguments
5              
6             #pod =head1 SYNOPSIS
7             #pod
8             #pod In our class definition:
9             #pod
10             #pod package Delivery;
11             #pod use Moose;
12             #pod with('MooseX::OneArgNew' => {
13             #pod type => 'Existing::Message::Type',
14             #pod init_arg => 'message',
15             #pod });
16             #pod
17             #pod has message => (isa => 'Existing::Message::Type', required => 1);
18             #pod
19             #pod has to => (
20             #pod is => 'ro',
21             #pod isa => 'Str',
22             #pod lazy => 1,
23             #pod default => sub {
24             #pod my ($self) = @_;
25             #pod $self->message->get('To');
26             #pod },
27             #pod );
28             #pod
29             #pod When making a message:
30             #pod
31             #pod # The traditional way:
32             #pod
33             #pod my $delivery = Delivery->new({ message => $message });
34             #pod # or
35             #pod my $delivery = Delivery->new({ message => $message, to => $to });
36             #pod
37             #pod # With one-arg new:
38             #pod
39             #pod my $delivery = Delivery->new($message);
40             #pod
41             #pod =head1 DESCRIPTION
42             #pod
43             #pod MooseX::OneArgNew lets your constructor take a single argument, which will be
44             #pod translated into the value for a one-entry hashref. It is a L<parameterized
45             #pod role|MooseX::Role::Parameterized> with three parameters:
46             #pod
47             #pod =begin :list
48             #pod
49             #pod = type
50             #pod
51             #pod The Moose type that the single argument must be for the one-arg form to work.
52             #pod This should be an existing type, and may be either a string type or a
53             #pod MooseX::Type.
54             #pod
55             #pod = init_arg
56             #pod
57             #pod This is the string that will be used as the key for the hashref constructed
58             #pod from the one-arg call to new.
59             #pod
60             #pod = coerce
61             #pod
62             #pod If true, a single argument to new will be coerced into the expected type if
63             #pod possible. Keep in mind that if there are no coercions for the type, this will
64             #pod be an error, and that if a coercion from HashRef exists, you might be getting
65             #pod yourself into a weird situation.
66             #pod
67             #pod =end :list
68             #pod
69             #pod =head2 WARNINGS
70             #pod
71             #pod You can apply MooseX::OneArgNew more than once, but if more than one
72             #pod application's type matches a single argument to C<new>, the behavior is
73             #pod undefined and likely to cause bugs.
74             #pod
75             #pod It would be a B<very bad idea> to supply a type that could accept a normal
76             #pod hashref of arguments to C<new>.
77             #pod
78             #pod =cut
79              
80 2     2   68717 use Moose::Util::TypeConstraints;
  2         5  
  2         23  
81              
82 2     2   3895 use namespace::autoclean;
  2         4  
  2         14  
83              
84             subtype 'MooseX::OneArgNew::_Type',
85             as 'Moose::Meta::TypeConstraint';
86              
87             coerce 'MooseX::OneArgNew::_Type',
88             from 'Str',
89             via { Moose::Util::TypeConstraints::find_type_constraint($_) };
90              
91             parameter type => (
92             isa => 'MooseX::OneArgNew::_Type',
93             coerce => 1,
94             required => 1,
95             );
96              
97             parameter coerce => (
98             isa => 'Bool',
99             default => 0,
100             );
101              
102             parameter init_arg => (
103             isa => 'Str',
104             required => 1,
105             );
106              
107             role {
108             my $p = shift;
109              
110             around BUILDARGS => sub {
111             my $orig = shift;
112             my $self = shift;
113             return $self->$orig(@_) unless @_ == 1;
114              
115             my $value = $p->coerce ? $p->type->coerce($_[0]) : $_[0];
116             return $self->$orig(@_) unless $p->type->check($value);
117              
118             return { $p->init_arg => $value }
119             };
120             };
121              
122             1;
123              
124             __END__
125              
126             =pod
127              
128             =encoding UTF-8
129              
130             =head1 NAME
131              
132             MooseX::OneArgNew - teach ->new to accept single, non-hashref arguments
133              
134             =head1 VERSION
135              
136             version 0.005
137              
138             =head1 SYNOPSIS
139              
140             In our class definition:
141              
142             package Delivery;
143             use Moose;
144             with('MooseX::OneArgNew' => {
145             type => 'Existing::Message::Type',
146             init_arg => 'message',
147             });
148              
149             has message => (isa => 'Existing::Message::Type', required => 1);
150              
151             has to => (
152             is => 'ro',
153             isa => 'Str',
154             lazy => 1,
155             default => sub {
156             my ($self) = @_;
157             $self->message->get('To');
158             },
159             );
160              
161             When making a message:
162              
163             # The traditional way:
164              
165             my $delivery = Delivery->new({ message => $message });
166             # or
167             my $delivery = Delivery->new({ message => $message, to => $to });
168              
169             # With one-arg new:
170              
171             my $delivery = Delivery->new($message);
172              
173             =head1 DESCRIPTION
174              
175             MooseX::OneArgNew lets your constructor take a single argument, which will be
176             translated into the value for a one-entry hashref. It is a L<parameterized
177             role|MooseX::Role::Parameterized> with three parameters:
178              
179             =over 4
180              
181             =item type
182              
183             The Moose type that the single argument must be for the one-arg form to work.
184             This should be an existing type, and may be either a string type or a
185             MooseX::Type.
186              
187             =item init_arg
188              
189             This is the string that will be used as the key for the hashref constructed
190             from the one-arg call to new.
191              
192             =item coerce
193              
194             If true, a single argument to new will be coerced into the expected type if
195             possible. Keep in mind that if there are no coercions for the type, this will
196             be an error, and that if a coercion from HashRef exists, you might be getting
197             yourself into a weird situation.
198              
199             =back
200              
201             =head2 WARNINGS
202              
203             You can apply MooseX::OneArgNew more than once, but if more than one
204             application's type matches a single argument to C<new>, the behavior is
205             undefined and likely to cause bugs.
206              
207             It would be a B<very bad idea> to supply a type that could accept a normal
208             hashref of arguments to C<new>.
209              
210             =head1 AUTHOR
211              
212             Ricardo Signes <rjbs@cpan.org>
213              
214             =head1 CONTRIBUTORS
215              
216             =for stopwords George Hartzell William Orr
217              
218             =over 4
219              
220             =item *
221              
222             George Hartzell <hartzell@alerce.com>
223              
224             =item *
225              
226             William Orr <will@worrbase.com>
227              
228             =back
229              
230             =head1 COPYRIGHT AND LICENSE
231              
232             This software is copyright (c) 2015 by Ricardo Signes.
233              
234             This is free software; you can redistribute it and/or modify it under
235             the same terms as the Perl 5 programming language system itself.
236              
237             =cut