summaryrefslogtreecommitdiffstats
path: root/lib/MooseX/MethodTraits.pm
blob: 91155aff1a1078915c7fd2e8e3680aec8e3992e7 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package MooseX::MethodTraits;
use Moose::Exporter;
use Scalar::Util qw(blessed reftype);

=head1 NAME

MooseX::MethodTraits -

=head1 SYNOPSIS


=head1 DESCRIPTION


=cut

sub _generate_method_creators {
    my ($package, $with_traits) = @_;

    my $package_meta = Class::MOP::Package->initialize($package);
    for my $sub (keys %$with_traits) {
        my $spec = $with_traits->{$sub};
        my $traits = $spec->{traits} || [];
        my $munge = $spec->{munge} || sub {
            my $meta = shift;
            my $name = shift;
            my $method = reftype($_[0]) && reftype($_[0]) eq 'CODE'
                ? shift : $meta->find_method_by_name($name);
            return $method, {@_};
        };

        my $code = sub {
            my ($method, $args) = $munge->(@_);
            my $meta = shift;
            my $name = shift;

            my $superclass = blessed($method) || $meta->method_metaclass;
            my $method_metaclass = Moose::Meta::Class->create_anon_class(
                superclasses => [$superclass],
                roles        => $traits,
                cache        => 1,
            );

            my $method_meta = $method_metaclass->name->wrap(
                $method,
                name         => $name,
                package_name => $meta->name,
            );

            $meta->add_method($name => $method_meta);

            return unless $args;

            for my $attr_name (map { $_->meta->get_attribute_list } @$traits) {
                next unless exists $args->{$attr_name};
                my $attr = $method_meta->meta->find_attribute_by_name($attr_name);
                $attr->set_value($method_meta, $args->{$attr_name});
            }
        };
        $package_meta->add_package_symbol('&' . $sub => $code);
    }
    return keys %$with_traits;
}

# XXX: factor some of this stuff out
sub build_import_methods {
    my $class   = shift;
    my %options = @_;
    $options{exporting_package} ||= caller;
    if (exists $options{with_traits}) {
        my $with_traits = delete $options{with_traits};
        my @extra_with_meta = _generate_method_creators(
            $options{exporting_package}, $with_traits
        );
        $options{with_meta} = [
            @{ $options{with_meta} || [] },
            @extra_with_meta,
        ];
    }
    return Moose::Exporter->build_import_methods(%options)
}

sub setup_import_methods {
    my $class   = shift;
    my %options = @_;
    $options{exporting_package} ||= caller;
    if (exists $options{with_traits}) {
        my $with_traits = delete $options{with_traits};
        my @extra_with_meta = _generate_method_creators(
            $options{exporting_package}, $with_traits
        );
        $options{with_meta} = [
            @{ $options{with_meta} || [] },
            @extra_with_meta,
        ];
    }
    return Moose::Exporter->setup_import_methods(%options)
}

sub import {
    strict->import;
    warnings->import;
}

=head1 BUGS

No known bugs.

Please report any bugs through RT: email
C<bug-moosex-methodtraits at rt.cpan.org>, or browse to
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-MethodTraits>.

=head1 SEE ALSO


=head1 SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc MooseX::MethodTraits

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/MooseX-MethodTraits>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/MooseX-MethodTraits>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-MethodTraits>

=item * Search CPAN

L<http://search.cpan.org/dist/MooseX-MethodTraits>

=back

=head1 AUTHOR

  Jesse Luehrs <doy at tozt dot net>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2009 by Jesse Luehrs.

This is free software; you can redistribute it and/or modify it under
the same terms as perl itself.

=cut

1;