File Coverage

blib/lib/MooseX/Declare/Context/WithOptions/Patch/Extensible.pm
Criterion Covered Total %
statement 18 20 90.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 25 27 92.5


line stmt bran cond sub pod time code
1             package MooseX::Declare::Context::WithOptions::Patch::Extensible;
2              
3 1     1   24403 use 5.010;
  1         4  
  1         39  
4 1     1   5 use strict;
  1         2  
  1         36  
5 1     1   5 use warnings;
  1         6  
  1         49  
6 1     1   1057 use utf8;
  1         12  
  1         7  
7              
8             BEGIN {
9 1     1   67 $MooseX::Declare::Context::WithOptions::Patch::Extensible::AUTHORITY = 'cpan:TOBYINK';
10 1         21 $MooseX::Declare::Context::WithOptions::Patch::Extensible::VERSION = '0.002';
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 1     1   5 use Carp;
  1         1  
  1         137  
18 1     1   487 use MooseX::Declare::Context::WithOptions 0.22;
  0            
  0            
19              
20             sub import
21             {
22             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             use Moose::Role;
31             use Carp qw(croak);
32            
33             no warnings 'redefine';
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             my ($self) = @_;
44             my %ret;
45            
46             # Make errors get reported from right place in source file
47             local $Carp::Internal{'MooseX::Declare'} = 1;
48             local $Carp::Internal{'Devel::Declare'} = 1;
49            
50             $self->skipspace;
51             my $linestr = $self->get_linestr;
52            
53             while (substr($linestr, $self->offset, 1) !~ /[{;]/) {
54             my $key = $self->strip_name;
55             if (!defined $key) {
56             croak 'expected option name'
57             if keys %ret;
58             return; # This is the case when { class => 'foo' } happens
59             }
60            
61             croak "unknown option name '$key'"
62             unless grep { $key eq $_ } @{ $self->allowed_option_names }; ##DIFF
63            
64             my $val = $self->strip_name;
65             if (!defined $val) {
66             if (defined($val = $self->strip_proto)) {
67             $val = [split /\s*,\s*/, $val];
68             }
69             else {
70             croak "expected option value after $key";
71             }
72             }
73            
74             $ret{$key} ||= [];
75             push @{ $ret{$key} }, ref $val ? @{ $val } : $val;
76             } continue {
77             $self->skipspace;
78             $linestr = $self->get_linestr();
79             }
80            
81             my $options = { map {
82             my $key = $_;
83             $key eq 'is'
84             ? ($key => { map { ($_ => 1) } @{ $ret{$key} } })
85             : ($key => $ret{$key})
86             } keys %ret };
87            
88             $self->options($options);
89            
90             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