File: Traits.pm

package info (click to toggle)
libmoosex-compiletime-traits-perl 0.092801-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116 kB
  • sloc: perl: 63; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{package MooseX::CompileTime::Traits;
our $VERSION = '0.092801';
}


#ABSTRACT: Allow compile time traits for classes/roles


use MooseX::Declare;

role MooseX::CompileTime::Traits
{
    use Moose::Util;


    method import (ClassName $class: ArrayRef :$traits?)
    {
        if(defined($traits))
        {
            my $meta = $class->meta;
            if($meta->isa('Moose::Meta::Class') && $meta->is_immutable)
            {
                $meta->make_mutable();
                Moose::Util::apply_all_roles($meta, @$traits);
                $meta->make_immutable();
            }
            else
            {
                Moose::Util::apply_all_roles($meta, @$traits);
            }
        }
    }
}

1;




=pod

=head1 NAME

MooseX::CompileTime::Traits - Allow compile time traits for classes/roles

=head1 VERSION

version 0.092801

=head1 SYNOPSIS

    role Bar(Int :$bar) { method bar { $bar + 2 } }
    role Baz(Int :$baz) { method baz { $baz + 4 } }

    class Foo with MooseX::CompileTime::Traits { }
    class Flarg with MooseX::CompileTime::Traits { }

    ...

    use Foo traits => [ Bar => { bar => 2 } ];
    use Flarg traits => [ Bar => { bar => 1 }, Baz => { baz => 1} ];

    Foo->new()->bar(); # 4
    my $flarg = Flarg->new();
    $flarg->bar(); # 3
    $flarg->baz(); # 5




=head1 DESCRIPTION

MooseX::CompileTime::Traits allows role application at compile time via use 
statements. What this class does is provide an import method that will apply
each of the roles (along with any arguments for parameterized roles).

Roles and their arguments should be provided as an ArrayRef of tuples.

Simply 'with' the role to gain the functionality

=head1 METHODS

=head2 import (ClassName $class: ArrayRef :$traits?)

import is provided such that when your class or role is use'd it can take 
additional arguments that will be validatated and interpreted as roles or
traits that need to be applied.



=head1 AUTHOR

  Nicholas Perez <nperez@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2009 by Infinity Interactive.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut 



__END__