File Coverage

blib/lib/MooseX/SlurpyConstructor.pm
Criterion Covered Total %
statement 25 25 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 34 34 100.0


line stmt bran cond sub pod time code
1             package MooseX::SlurpyConstructor;
2             BEGIN {
3 7     7   4280130 $MooseX::SlurpyConstructor::VERSION = '1.2';
4             }
5              
6 7     7   69 use strict;
  7         16  
  7         248  
7 7     7   41 use warnings;
  7         13  
  7         241  
8              
9 7     7   952 use Moose 0.94 ();
  7         517623  
  7         157  
10 7     7   46 use Moose::Exporter;
  7         17  
  7         90  
11 7     7   326 use Moose::Util::MetaRole;
  7         21  
  7         185  
12 7     7   4717 use MooseX::SlurpyConstructor::Role::Object;
  7         26  
  7         342  
13 7     7   5324 use MooseX::SlurpyConstructor::Trait::Class;
  7         27  
  7         815  
14 7     7   6081 use MooseX::SlurpyConstructor::Trait::Attribute;
  7         24  
  7         1874  
15              
16             {
17             my %meta_stuff = (
18             base_class_roles => ['MooseX::SlurpyConstructor::Role::Object'],
19             class_metaroles => {
20             class => ['MooseX::SlurpyConstructor::Trait::Class'],
21             attribute => ['MooseX::SlurpyConstructor::Trait::Attribute'],
22             },
23             );
24              
25             if ( Moose->VERSION < 1.9900 ) {
26             require MooseX::SlurpyConstructor::Trait::Method::Constructor;
27             push @{$meta_stuff{class_metaroles}{constructor}}, 'MooseX::SlurpyConstructor::Trait::Method::Constructor';
28             }
29             else {
30             push @{$meta_stuff{class_metaroles}{class}},
31             'MooseX::SlurpyConstructor::Trait::Class';
32             push @{$meta_stuff{role_metaroles}{role}},
33             'MooseX::SlurpyConstructor::Trait::Role';
34             push @{$meta_stuff{role_metaroles}{application_to_class}},
35             'MooseX::SlurpyConstructor::Trait::ApplicationToClass';
36             push @{$meta_stuff{role_metaroles}{application_to_role}},
37             'MooseX::SlurpyConstructor::Trait::ApplicationToRole';
38             push @{$meta_stuff{role_metaroles}{applied_attribute}},
39             'MooseX::SlurpyConstructor::Trait::Attribute';
40             }
41              
42             Moose::Exporter->setup_import_methods(
43             %meta_stuff,
44             );
45             }
46              
47             1;
48              
49             # ABSTRACT: Make your object constructor collect all unknown attributes
50              
51              
52              
53             =pod
54              
55             =head1 NAME
56              
57             MooseX::SlurpyConstructor - Make your object constructor collect all unknown attributes
58              
59             =head1 VERSION
60              
61             version 1.2
62              
63             =head1 SYNOPSIS
64              
65             package My::Class;
66              
67             use Moose;
68             use MooseX::SlurpyConstructor;
69              
70             has fixed => (
71             is => 'ro',
72             );
73              
74             has slurpy => (
75             is => 'ro',
76             slurpy => 1,
77             );
78              
79             package main;
80              
81             ASDF->new({
82             fixed => 100, unknown1 => "a", unknown2 => [ 1..3 ]
83             })->dump;
84              
85             # returns:
86             # $VAR1 = bless( {
87             # 'slurpy' => {
88             # 'unknown2' => [
89             # 1,
90             # 2,
91             # 3
92             # ],
93             # 'unknown1' => 'a'
94             # },
95             # 'fixed' => 100
96             # }, 'ASDF' );
97              
98             =head1 DESCRIPTION
99              
100             Including this module within Moose-based classes, and declaring an
101             attribute as 'slurpy' will allow capturing of all unknown constructor
102             arguments in the given attribute.
103              
104             As of Moose 1.9900, this module can also be used in a role, in which case the
105             constructor of the consuming class will become slurpy.
106              
107             =head1 OPTIONAL RESTRICTIONS
108              
109             No additional options are added to your 'slurpy' attribute, so if you want to
110             make it read-only, or restrict its type constraint to a HashRef of specific
111             types, you should state that yourself. Typical usage may include any or all of
112             the options below:
113              
114             has slurpy => (
115             is => 'ro',
116             isa => 'HashRef',
117             init_arg => undef,
118             lazy => 1,
119             default => sub { {} },
120             traits => ['Hash'],
121             handles => {
122             slurpy_values => 'elements',
123             },
124             );
125              
126             For more information on these options, see L<Moose::Manual::Attributes> and
127             L<Moose::Meta::Attribute::Native::Trait::Hash>.
128              
129             =head1 SEE ALSO
130              
131             =over 4
132              
133             =item MooseX::StrictConstructor
134              
135             The opposite of this module, making constructors die on unknown arguments.
136             If both of these are used together, StrictConstructor will always
137             take precedence.
138              
139             This module can also be used in migrating code from vanilla Moose to
140             using L<MooseX::StrictConstructor>. That was one of my original motivations
141             for writing it; to allow staged migration.
142              
143             =back
144              
145             =head1 BUGS
146              
147             Please report any bugs or feature requests to
148             C<bug-moosex-slurpyconstructor@rt.cpan.org>, or through the web
149             interface at L<http://rt.cpan.org>. The module maintainers will be notified,
150             and then you'll automatically be notified of progress on your bug as changes
151             are made.
152              
153             You can also use the normal Moose support channels - see L<Moose#GETTING_HELP>.
154              
155             =head1 HISTORY
156              
157             This module was originally written by Mark Morgan C<< <makk384@gmail.com> >>,
158             with some bugfix patches by Christian Walde.
159              
160             It was completely rewritten for Moose 2.0 by Karen Etheridge C<<
161             <ether@cpan.org> >>, drawing heavily on MooseX::StrictConstructor.
162              
163             =head1 ACKNOWLEDGEMENTS
164              
165             Thanks to the folks from moose mailing list and IRC channels for
166             helping me find my way around some of the Moose bits I didn't
167             know of before writing this module.
168              
169             =head1 AUTHORS
170              
171             =over 4
172              
173             =item *
174              
175             Mark Morgan <makk384@gmail.com>
176              
177             =item *
178              
179             Karen Etheridge <ether@cpan.org>
180              
181             =back
182              
183             =head1 COPYRIGHT AND LICENSE
184              
185             This software is copyright (c) 2011 by Karen Etheridge.
186              
187             This is free software; you can redistribute it and/or modify it under
188             the same terms as the Perl 5 programming language system itself.
189              
190             =cut
191              
192              
193             __END__
194