File Coverage

blib/lib/MooseX/Declare/Context/WithOptions/Patch/Extensible.pm
Criterion Covered Total %
statement 54 62 87.1
branch 6 16 37.5
condition 2 2 100.0
subroutine 12 12 100.0
pod 0 1 0.0
total 74 93 79.5


line stmt bran cond sub pod time code
1             package MooseX::Declare::Context::WithOptions::Patch::Extensible;
2              
3 2     2   1959556 use 5.010;
  2         8  
  2         158  
4 2     2   13 use strict;
  2         4  
  2         71  
5 2     2   12 use warnings;
  2         9  
  2         83  
6 2     2   968 use utf8;
  2         4  
  2         16  
7              
8             BEGIN {
9 2     2   5 $MooseX::Declare::Context::WithOptions::Patch::Extensible::AUTHORITY = 'cpan:TOBYINK';
10 2         45 $MooseX::Declare::Context::WithOptions::Patch::Extensible::VERSION = '0.001';
11             }
12              
13             # I had hoped to do this with Module::Patch, but it seems that Module::Patch
14             # doesn't work especially well with Moose roles. Patching a sub in the role
15             # does not necessarily affect the classes that the role has been composed with.
16              
17 2     2   12 use Carp;
  2         5  
  2         200  
18 2     2   1183 use MooseX::Declare::Context::WithOptions 0.22;
  2         881987  
  2         195  
19              
20             sub import
21             {
22 2 50   2   22343 carp "MooseX::Declare::Context::WithOptions->VERSION gt '0.35'"
23             if MooseX::Declare::Context::WithOptions->VERSION gt '0.35';
24             }
25              
26             {
27             package # hide from CPAN indexer
28             MooseX::Declare::Context::WithOptions;
29            
30 2     2   28 use Moose::Role;
  2         4  
  2         22  
31 2     2   5268 use Carp qw(croak);
  2         5  
  2         131  
32            
33 2     2   12 no warnings 'redefine';
  2         11  
  2         1436  
34            
35             has allowed_option_names => (
36             is => 'rw',
37             isa => 'ArrayRef',
38             lazy => 1,
39             default => sub { [qw[ extends with is ]] },
40             );
41            
42             sub strip_options {
43 1     1 0 35188 my ($self) = @_;
44 1         3 my %ret;
45            
46             # Make errors get reported from right place in source file
47 1         5 local $Carp::Internal{'MooseX::Declare'} = 1;
48 1         4 local $Carp::Internal{'Devel::Declare'} = 1;
49            
50 1         4 $self->skipspace;
51 1         68 my $linestr = $self->get_linestr;
52            
53 1         63 while (substr($linestr, $self->offset, 1) !~ /[{;]/) {
54 2         174 my $key = $self->strip_name;
55 2 50       167 if (!defined $key) {
56 0 0       0 croak 'expected option name'
57             if keys %ret;
58 0         0 return; # This is the case when { class => 'foo' } happens
59             }
60            
61 9         32 croak "unknown option name '$key'"
62 2 50       4 unless grep { $key eq $_ } @{ $self->allowed_option_names }; ##DIFF
  2         31  
63            
64 2         8 my $val = $self->strip_name;
65 2 50       162 if (!defined $val) {
66 0 0       0 if (defined($val = $self->strip_proto)) {
67 0         0 $val = [split /\s*,\s*/, $val];
68             }
69             else {
70 0         0 croak "expected option value after $key";
71             }
72             }
73            
74 2   100     16 $ret{$key} ||= [];
75 2 50       2 push @{ $ret{$key} }, ref $val ? @{ $val } : $val;
  2         10  
  0         0  
76             } continue {
77 2         6 $self->skipspace;
78 2         123 $linestr = $self->get_linestr();
79             }
80            
81 1         3 my $options = { map {
82 1         110 my $key = $_;
83 0         0 $key eq 'is'
84 1 50       16 ? ($key => { map { ($_ => 1) } @{ $ret{$key} } })
  0         0  
85             : ($key => $ret{$key})
86             } keys %ret };
87            
88 1         55 $self->options($options);
89            
90 1         17 return $options;
91             }
92             }
93              
94             1;
95              
96             __END__
97              
98             =head1 NAME
99              
100             MooseX::Declare::Context::WithOptions::Patch::Extensible - patch MooseX::Declare for extensibility
101              
102             =head1 SYNOPSIS
103              
104             use MooseX::Declare::Context::WithOptions::Patch::Extensible;
105              
106             =head1 DESCRIPTION
107              
108             This module extends MooseX::Declare::Context::WithOptions to add a new
109             attribute C<allowed_option_names> containing an arrayref of option names
110             that it can parse. The default is the standard MooseX::Declare list of
111             'extends', 'with' and 'is'.
112              
113             It also patches the C<strip_options> method so that it pays attention to
114             that arrayref.
115              
116             If you don't understand why you'd need to do this, then you probably don't
117             need to do this.
118              
119             =head1 BUGS
120              
121             Please report any bugs to
122             L<http://rt.cpan.org/Dist/Display.html?Queue=MooseX-Declare-Context-WithOptions-Patch-Extensible>.
123              
124             =head1 SEE ALSO
125              
126             C<MooseX::Declare>.
127              
128             =head1 AUTHOR
129              
130             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
131              
132             =head1 COPYRIGHT AND LICENCE
133              
134             This software is copyright (c) 2012 by Toby Inkster.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =head1 DISCLAIMER OF WARRANTIES
140              
141             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
142             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
143             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
144