File Coverage

blib/lib/MooX/AliasedAttributes.pm
Criterion Covered Total %
statement 25 25 100.0
branch 3 4 75.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 34 35 97.1


line stmt bran cond sub pod time code
1             package MooX::AliasedAttributes;
2             $MooX::AliasedAttributes::VERSION = '0.03';
3 1     1   61007 use strictures 1;
  1         7  
  1         30  
4              
5             =head1 NAME
6              
7             MooX::AliasedAttributes - Make aliases to your attributes, and methods.
8              
9             =head1 SYNOPSIS
10              
11             package Foo;
12             use Moo;
13             use MooX::AliasedAttributes;
14            
15             has color => (
16             is => 'rw',
17             alias => 'colour',
18             );
19              
20             alias bar => 'color';
21            
22             my $foo = Foo->new( color=>'red' );
23            
24             print $foo->colour(); # red
25             $foo->colour('green');
26             print $foo->color(); # green
27            
28             print $foo->bar(); # green
29              
30             =head1 DESCRIPTION
31              
32             Aliases a method to an attribute's C. Note that if you set either
33             C or C that this has no affect onthe alias, the alias
34             will still point at the attribute name.
35              
36             This module came to life to help port L code using L
37             to L. In order to port you existing code from L to L you
38             should just be able to replace C with
39             C.
40              
41             =cut
42              
43 1     1   1015 use Class::Method::Modifiers qw( install_modifier );
  1         1507  
  1         276  
44              
45             sub import {
46 1     1   9 my $target = caller();
47              
48 1         10 my $around = $target->can('around');
49 1     3   4 my $fresh = sub{ install_modifier( $target, 'fresh', @_ ) };
  3         15  
50              
51             $fresh->(
52             alias => sub{
53 2     2   36 my ($aliases, $method) = @_;
54 2 50       8 $aliases = [ $aliases ] if !ref $aliases;
55              
56 2         5 foreach my $alias ($aliases) {
57 2         15 $fresh->( $alias => sub{ shift()->$method(@_) } );
  2         1330  
58             }
59             },
60 1         7 );
61              
62 1         166 my $alias = $target->can('alias');
63              
64             $around->(
65             has => sub{
66 2     2   148 my ($orig, $name, %attributes) = @_;
67              
68 2         7 my $aliases = delete $attributes{alias};
69 2         14 $orig->( $name, %attributes );
70 2 100       19645 return if !$aliases;
71              
72 1         34 $alias->( $aliases, $name );
73              
74 1         221 return;
75             },
76 1         6 );
77              
78 1         4138 return;
79             }
80              
81             1;
82             __END__