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) 2017, 2015, 2014, 2012, 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.019';
13             # ABSTRACT: Easily build a new 'Reindeer' style class
14              
15 1     1   123858 use strict;
  1         2  
  1         25  
16 1     1   4 use warnings;
  1         2  
  1         19  
17              
18 1     1   5 use Carp;
  1         2  
  1         47  
19 1     1   416 use Moose::Exporter;
  1         5522  
  1         6  
20 1     1   378 use Moose::Autobox;
  1         505435  
  1         6  
21 1     1   676 use Sub::Install;
  1         3  
  1         9  
22              
23 1     1   614 use Reindeer ();
  1         3  
  1         29  
24 1     1   7 use Reindeer::Util;
  1         2  
  1         6  
25              
26             # Look at our args, figure out what needs to be added/filtered
27              
28             sub import {
29 1     1   13 my ($class, %config) = @_;
30              
31             # figure out if we're supposed to be for a role
32 1         3 my $target = caller(0); # let's start out simple
33 1 50       5 my $for_role = ($target =~ /::Role$/) ? 1 : 0;
34              
35 1         2 my $also = [];
36 1 50       4 if (exists $config{also}) {
37              
38 1         2 my $also_config = $config{also};
39              
40 1         3 $also = [ Reindeer::Util::also_list() ];
41 1   50     4 my $exclude = $config{also}->{exclude} || [];
42 1   50     8 my $add = $config{also}->{add} || [];
43              
44 8     8   172 $also = $also->grep(sub { !$exclude->any($_) })
45 1 50       15 if @$exclude > 0;
46              
47 1         17 $also->push(@$add);
48             }
49              
50 1 50       12 $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         7 my %methods;
55 1         5 @methods{qw/ import unimport init_meta /} =
56             Moose::Exporter->build_import_methods(
57             also => $also,
58             );
59              
60             my $_install = sub {
61             Sub::Install::reinstall_sub({
62 2     2   8 code => $methods{$_[0]},
63             into => $target,
64             as => $_[0],
65             });
66 1         955 };
67              
68 3 100       101 do { $_install->($_) if defined $methods{$_} }
69 1         3 for keys %methods;
70              
71 1         43 return;
72             }
73              
74             !!42;
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =for :stopwords Chris Weyl Alex Balhatchet metaclass
83              
84             =head1 NAME
85              
86             Reindeer::Builder - Easily build a new 'Reindeer' style class
87              
88             =head1 VERSION
89              
90             This document describes version 0.019 of Reindeer::Builder - released June 09, 2017 as part of Reindeer.
91              
92             =head1 SYNOPSIS
93              
94             package My::Reindeer;
95             use Reindeer::Builder
96             also => {
97             exclude => [ ... ], # packages from also
98             add => [ ... ],
99             };
100              
101             package My::Class;
102             use My::Reindeer;
103              
104             # profit!
105             has foo => (...);
106              
107             =head1 DESCRIPTION
108              
109             Sometimes you need more than what L<Reindeer> provides... And sometime less.
110             Or there's a conflict with a default extension (e.g. a Catalyst controller
111             with a config that will end up with unrecognized arguments passed to the
112             constructor will blow up if L<MooseX::StrictConstructor> is used).
113              
114             Reindeer::Builder provides a simple interface to add additional extensions --
115             or filter the list of default extensions. It's intended to be used in a
116             package of its own that can then be used in the same way L<Moose> or
117             L<Reindeer> is in a package, to set up the metaclass and sugar.
118              
119             =head1 ROLE OR CLASS?
120              
121             If the package you're using Reindeer::Builder in ends with '::Role', we set up
122             role metaclass and sugar.
123              
124             =head1 ARGUMENTS
125              
126             We take a set of name / hashref pairs. Right now we only support 'also' for
127             names.
128              
129             It is legal and supported to add and exclude at the same time.
130              
131             =head2 also / exclude
132              
133             If given, we expect exclude to be an arrayref of package names to be excluded
134             from the list of extensions. (e.g. this filters what is passed to
135             L<Moose::Exporter>'s 'also' argument.
136              
137             e.g.
138              
139             use Reindeer::Builder also => { exclude => 'MooseX::Something' };
140              
141             =head2 also / add
142              
143             If given, we expect add to be an arrayref of package names to be added
144             to the list of extensions. (e.g. this augments what is passed to
145             L<Moose::Exporter>'s 'also' argument.
146              
147             e.g.
148              
149             use Reindeer::Builder also => { add => 'MooseX::SomethingElse' };
150              
151             =head1 SEE ALSO
152              
153             Please see those modules/websites for more information related to this module.
154              
155             =over 4
156              
157             =item *
158              
159             L<Reindeer|Reindeer>
160              
161             =item *
162              
163             L<L<Reindeer>, L<Moose::Exporter>.|L<Reindeer>, L<Moose::Exporter>.>
164              
165             =back
166              
167             =head1 BUGS
168              
169             Please report any bugs or feature requests on the bugtracker website
170             L<https://github.com/RsrchBoy/reindeer/issues>
171              
172             When submitting a bug or request, please include a test-file or a
173             patch to an existing test-file that illustrates the bug or desired
174             feature.
175              
176             =head1 AUTHOR
177              
178             Chris Weyl <cweyl@alumni.drew.edu>
179              
180             =head1 COPYRIGHT AND LICENSE
181              
182             This software is Copyright (c) 2017, 2015, 2014, 2012, 2011 by Chris Weyl.
183              
184             This is free software, licensed under:
185              
186             The GNU Lesser General Public License, Version 2.1, February 1999
187              
188             =cut