File Coverage

blib/lib/Reindeer/Builder.pm
Criterion Covered Total %
statement 44 44 100.0
branch 6 10 60.0
condition 2 4 50.0
subroutine 11 11 100.0
pod n/a
total 63 69 91.3


line stmt bran cond sub pod time code
1             #
2             # This file is part of Reindeer
3             #
4             # This software is Copyright (c) 2011 by Chris Weyl.
5             #
6             # This is free software, licensed under:
7             #
8             # The GNU Lesser General Public License, Version 2.1, February 1999
9             #
10             package Reindeer::Builder;
11             our $AUTHORITY = 'cpan:RSRCHBOY';
12             $Reindeer::Builder::VERSION = '0.018';
13             # ABSTRACT: Easily build a new 'Reindeer' style class
14              
15 1     1   112981 use strict;
  1         1  
  1         25  
16 1     1   3 use warnings;
  1         1  
  1         19  
17              
18 1     1   4 use Carp;
  1         2  
  1         55  
19 1     1   473 use Moose::Exporter;
  1         5089  
  1         5  
20 1     1   476 use Moose::Autobox;
  1         361748  
  1         5  
21 1     1   566 use Sub::Install;
  1         2  
  1         8  
22              
23 1     1   582 use Reindeer ();
  1         3  
  1         24  
24 1     1   5 use Reindeer::Util;
  1         1  
  1         5  
25              
26             # Look at our args, figure out what needs to be added/filtered
27              
28             sub import {
29 1     1   9 my ($class, %config) = @_;
30              
31             # figure out if we're supposed to be for a role
32 1         2 my $target = caller(0); # let's start out simple
33 1 50       4 my $for_role = ($target =~ /::Role$/) ? 1 : 0;
34              
35 1         1 my $also = [];
36 1 50       2 if (exists $config{also}) {
37              
38 1         2 my $also_config = $config{also};
39              
40 1         2 $also = [ Reindeer::Util::also_list() ];
41 1   50     3 my $exclude = $config{also}->{exclude} || [];
42 1   50     6 my $add = $config{also}->{add} || [];
43              
44 8     8   169 $also = $also->grep(sub { !$exclude->any($_) })
45 1 50       12 if @$exclude > 0;
46              
47 1         12 $also->push(@$add);
48             }
49              
50 1 50       7 $also->unshift($for_role ? 'Moose::Role' : 'Moose');
51              
52             # create our methods...
53             #my ($import, $unimport, $init_meta) = Moose::Exporter->build_import_methods(
54 1         4 my %methods;
55 1         4 @methods{qw/ import unimport init_meta /} =
56             Moose::Exporter->build_import_methods(
57             also => $also,
58             );
59              
60             my $_install = sub {
61 2     2   29 Sub::Install::reinstall_sub({
62             code => $methods{$_[0]},
63             into => $target,
64             as => $_[0],
65             });
66 1         595 };
67              
68 3 100       105 do { $_install->($_) if defined $methods{$_} }
69 1         3 for keys %methods;
70              
71 1         59 return;
72             }
73              
74             !!42;
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =for :stopwords Chris Weyl metaclass
83              
84             =for :stopwords Wishlist flattr flattr'ed gittip gittip'ed
85              
86             =head1 NAME
87              
88             Reindeer::Builder - Easily build a new 'Reindeer' style class
89              
90             =head1 VERSION
91              
92             This document describes version 0.018 of Reindeer::Builder - released March 28, 2015 as part of Reindeer.
93              
94             =head1 SYNOPSIS
95              
96             package My::Reindeer;
97             use Reindeer::Builder
98             also => {
99             exclude => [ ... ], # packages from also
100             add => [ ... ],
101             };
102              
103             package My::Class;
104             use My::Reindeer;
105              
106             # profit!
107             has foo => (...);
108              
109             =head1 DESCRIPTION
110              
111             Sometimes you need more than what L<Reindeer> provides... And sometime less.
112             Or there's a conflict with a default extension (e.g. a Catalyst controller
113             with a config that will end up with unrecognized arguments passed to the
114             constructor will blow up if L<MooseX::StrictConstructor> is used).
115              
116             Reindeer::Builder provides a simple interface to add additional extensions --
117             or filter the list of default extensions. It's intended to be used in a
118             package of its own that can then be used in the same way L<Moose> or
119             L<Reindeer> is in a package, to set up the metaclass and sugar.
120              
121             =head1 ROLE OR CLASS?
122              
123             If the package you're using Reindeer::Builder in ends with '::Role', we set up
124             role metaclass and sugar.
125              
126             =head1 ARGUMENTS
127              
128             We take a set of name / hashref pairs. Right now we only support 'also' for
129             names.
130              
131             It is legal and supported to add and exclude at the same time.
132              
133             =head2 also / exclude
134              
135             If given, we expect exclude to be an arrayref of package names to be excluded
136             from the list of extensions. (e.g. this filters what is passed to
137             L<Moose::Exporter>'s 'also' argument.
138              
139             e.g.
140              
141             use Reindeer::Builder also => { exclude => 'MooseX::Something' };
142              
143             =head2 also / add
144              
145             If given, we expect add to be an arrayref of package names to be added
146             to the list of extensions. (e.g. this augments what is passed to
147             L<Moose::Exporter>'s 'also' argument.
148              
149             e.g.
150              
151             use Reindeer::Builder also => { add => 'MooseX::SomethingElse' };
152              
153             =head1 SEE ALSO
154              
155             Please see those modules/websites for more information related to this module.
156              
157             =over 4
158              
159             =item *
160              
161             L<Reindeer|Reindeer>
162              
163             =item *
164              
165             L<L<Reindeer>, L<Moose::Exporter>.|L<Reindeer>, L<Moose::Exporter>.>
166              
167             =back
168              
169             =head1 SOURCE
170              
171             The development version is on github at L<http://https://github.com/RsrchBoy/reindeer>
172             and may be cloned from L<git://https://github.com/RsrchBoy/reindeer.git>
173              
174             =head1 BUGS
175              
176             Please report any bugs or feature requests on the bugtracker website
177             https://github.com/RsrchBoy/reindeer/issues
178              
179             When submitting a bug or request, please include a test-file or a
180             patch to an existing test-file that illustrates the bug or desired
181             feature.
182              
183             =head1 AUTHOR
184              
185             Chris Weyl <cweyl@alumni.drew.edu>
186              
187             =head2 I'm a material boy in a material world
188              
189             =begin html
190              
191             <a href="https://www.gittip.com/RsrchBoy/"><img src="https://raw.githubusercontent.com/gittip/www.gittip.com/master/www/assets/%25version/logo.png" /></a>
192             <a href="http://bit.ly/rsrchboys-wishlist"><img src="http://wps.io/wp-content/uploads/2014/05/amazon_wishlist.resized.png" /></a>
193             <a href="https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Freindeer&title=RsrchBoy's%20CPAN%20Reindeer&tags=%22RsrchBoy's%20Reindeer%20in%20the%20CPAN%22"><img src="http://api.flattr.com/button/flattr-badge-large.png" /></a>
194              
195             =end html
196              
197             Please note B<I do not expect to be gittip'ed or flattr'ed for this work>,
198             rather B<it is simply a very pleasant surprise>. I largely create and release
199             works like this because I need them or I find it enjoyable; however, don't let
200             that stop you if you feel like it ;)
201              
202             L<Flattr this|https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Freindeer&title=RsrchBoy's%20CPAN%20Reindeer&tags=%22RsrchBoy's%20Reindeer%20in%20the%20CPAN%22>,
203             L<gittip me|https://www.gittip.com/RsrchBoy/>, or indulge my
204             L<Amazon Wishlist|http://bit.ly/rsrchboys-wishlist>... If you so desire.
205              
206             =head1 COPYRIGHT AND LICENSE
207              
208             This software is Copyright (c) 2011 by Chris Weyl.
209              
210             This is free software, licensed under:
211              
212             The GNU Lesser General Public License, Version 2.1, February 1999
213              
214             =cut