File Coverage

blib/lib/MooX/Traits.pm
Criterion Covered Total %
statement 29 30 96.6
branch 9 10 90.0
condition 4 5 80.0
subroutine 8 8 100.0
pod 2 2 100.0
total 52 55 94.5


line stmt bran cond sub pod time code
1 8     8   108503 use 5.006;
  8         32  
  8         343  
2 8     8   47 use strict;
  8         14  
  8         297  
3 8     8   58 use warnings;
  8         17  
  8         519  
4              
5 8 50   8   517 BEGIN { if ($] < 5.010000) { require UNIVERSAL::DOES } };
  0         0  
6              
7             package MooX::Traits;
8              
9             our $AUTHORITY = 'cpan:TOBYINK';
10             our $VERSION = '0.004';
11              
12 8     8   1101 use Role::Tiny;
  8         9972  
  8         93  
13              
14             sub _trait_namespace
15             {
16 17     17   2319 ();
17             }
18              
19             sub with_traits
20             {
21 34     34 1 14572 my $class = shift;
22 34 100       122 return $class unless @_;
23            
24 26         17359 require MooX::Traits::Util;
25 26         110 MooX::Traits::Util::new_class_with_traits($class, @_);
26             }
27              
28             sub new_with_traits
29             {
30 23     23 1 96923 my $class = shift;
31 23         590 my (%args, $pass_as_ref);
32 23 100 66     122 if (@_==1 and ref($_[0]) eq 'HASH')
33             {
34 1         2 %args = %{$_[0]};
  1         7  
35 1         4 $pass_as_ref = !!1;
36             }
37             else
38             {
39 22         75 %args = @_;
40 22         42 $pass_as_ref = !!0;
41             }
42            
43 23   100     147 my $traits_arr = delete($args{traits}) || [];
44 23 100       82 my @traits = ref($traits_arr) ? @$traits_arr : $traits_arr;
45 23 100       76 $class->with_traits(@traits)->new( $pass_as_ref ? \%args : %args );
46             }
47              
48             1;
49              
50             __END__
51              
52             =pod
53              
54             =encoding utf-8
55              
56             =for stopwords MooseX MouseX prepend
57              
58             =head1 NAME
59              
60             MooX::Traits - automatically apply roles at object creation time
61              
62             =head1 SYNOPSIS
63              
64             Given some roles:
65              
66             package Role;
67             use Moo::Role;
68             has foo => ( is => 'ro', required => 1 );
69              
70             And a class:
71              
72             package Class;
73             use Moo;
74             with 'MooX::Traits';
75              
76             Apply the roles to the class:
77              
78             my $class = Class->with_traits('Role');
79              
80             Then use your customized class:
81              
82             my $object = $class->new( foo => 42 );
83             $object->isa('Class'); # true
84             $object->does('Role'); # true
85             $object->foo; # 42
86              
87             =head1 DESCRIPTION
88              
89             Was any of the SYNOPSIS unexpected? Basically, this module is the same
90             thing as L<MooseX::Traits> and L<MouseX::Traits>, only for L<Moo>.
91             I<Quelle surprise>, right?
92              
93             =head2 Methods
94              
95             =over
96              
97             =item C<< $class->with_traits( @traits ) >>
98              
99             Return a new class name with the traits applied.
100              
101             =item C<< $class->new_with_traits(%args, traits => \@traits) >>
102              
103             C<new_with_traits> can also take a hashref, e.g.:
104              
105             my $instance = $class->new_with_traits({ traits => \@traits, foo => 'bar' });
106              
107             This method exists for compatibility with the MooseX and MouseX
108             equivalents of this module, but generally speaking you should prefer
109             to use C<with_traits>.
110              
111             =item C<< $class->_trait_namespace >>
112              
113             This returns undef, but you can override it in your class to
114             automatically prepend a namespace to supplied traits.
115              
116             This differs slightly from the MooseX and MouseX versions of this
117             module which have C<_trait_namespace> as an attribute instead of
118             a method.
119              
120             =back
121              
122             =head2 Compatibility
123              
124             Although called MooX::Traits, this module actually uses L<Role::Tiny>,
125             so doesn't really require L<Moo>. If you use it in a non-Moo class,
126             you should be able to safely consume any Role::Tiny-based traits.
127              
128             If you use it in a Moo class, you should also be able to consume
129             Moo::Role-based traits and Moose::Role-based traits.
130              
131             L<Package::Variant> and L<MooseX::Role::Parameterized> roles should be
132             usable; just provide a hashref of arguments:
133              
134             $class->with_traits( $param_role => \%args )->new( ... )
135              
136             =head1 BUGS
137              
138             Please report any bugs to
139             L<http://rt.cpan.org/Dist/Display.html?Queue=MooX-Traits>.
140              
141             =head1 SEE ALSO
142              
143             L<MooX::Traits::Util>.
144              
145             L<Moo::Role>,
146             L<Role::Tiny>.
147              
148             L<MooseX::Traits>,
149             L<MouseX::Traits>.
150              
151             =head1 AUTHOR
152              
153             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
154              
155             =head1 COPYRIGHT AND LICENCE
156              
157             This software is copyright (c) 2014 by Toby Inkster.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =head1 DISCLAIMER OF WARRANTIES
163              
164             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
165             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
166             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
167