File Coverage

blib/lib/MOP/Method/Attribute.pm
Criterion Covered Total %
statement 38 39 97.4
branch 3 6 50.0
condition 3 7 42.8
subroutine 13 13 100.0
pod 6 6 100.0
total 63 71 88.7


line stmt bran cond sub pod time code
1             package MOP::Method::Attribute;
2             # ABSTRACT: The Method Attribute object
3              
4 33     33   174 use strict;
  33         59  
  33         830  
5 33     33   131 use warnings;
  33         44  
  33         604  
6              
7 33     33   129 use Carp ();
  33         43  
  33         1059  
8              
9             our $VERSION = '0.14';
10             our $AUTHORITY = 'cpan:STEVAN';
11              
12 33     33   11267 use parent 'UNIVERSAL::Object::Immutable';
  33         7965  
  33         135  
13              
14             our %HAS; BEGIN {
15             %HAS = (
16 0         0 original => sub { die '`original` is required' },
17             )
18 33     33   73032 }
19              
20             # NOTE:
21             # we are not terribly sophisticated, but
22             # we accept `foo` calls (no-parens) and
23             # we accept `foo(1, 2, 3)` calls (parens
24             # with comma seperated args).
25              
26             sub BUILDARGS {
27 2     2 1 90 my $class = shift;
28 2 50 33     19 Carp::confess('You must pass only a simple string')
29             unless scalar(@_) == 1 && not ref $_[0];
30 2         8 return +{ original => $_[0] };
31             }
32              
33 2     2 1 4 sub REPR { \(my $x) }
34              
35             sub CREATE {
36 2     2 1 19 my ($class, $proto) = @_;
37 2         4 my $self = $class->REPR;
38 2         4 $$self = $proto->{original};
39 2         4 $self;
40             }
41              
42 1     1 1 2 sub original { ${ $_[0] } }
  1         29  
43              
44             sub name {
45 2     2 1 36 my ($self) = @_;
46 2         7 my ($name) = ($$self =~ m/^([a-zA-Z_]*)/);
47 2         8 return $name;
48             }
49              
50             sub args {
51 1     1 1 3 my ($self, $arg_splitter, $arg_processor) = @_;
52 1         7 my ($args) = ($$self =~ m/^[a-zA-Z_]*\(\s*(.*)\)/ms);
53 1 50       3 return unless $args;
54              
55             # NOTE:
56             # These parses arguments badly,
57             # but they are just the defaults.
58             # it makes no attempt to enforce
59             # anything, just splits on the
60             # comma, both skinny and fat,
61             # then strips away any quotes
62             # and treats everything as a
63             # simple string.
64 1   50 1   8 $arg_splitter ||= sub { split /\s*(?:\,|\=\>)\s*/ => $_[0] };
  1         6  
65             $arg_processor ||= sub {
66             # NOTE:
67             # None of the args are eval-ed and they are
68             # basically just a list of strings, with the
69             # one exception of the string "undef", which
70             # will be turned into undef
71 2     2   3 my $arg = $_[0];
72 2         8 $arg =~ s/\s*$//;
73 2         3 $arg =~ s/^['"]//;
74 2         3 $arg =~ s/['"]$//;
75 2 50       12 $arg eq 'undef' ? undef : $arg;
76 1   50     6 };
77              
78 1         3 return [ map $arg_processor->( $_ ), $arg_splitter->( $args ) ];
79             }
80              
81             1;
82              
83             __END__