File: Traits.pm

package info (click to toggle)
libhtml-formhandler-perl 0.40057-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,320 kB
  • ctags: 685
  • sloc: perl: 8,849; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 3,314 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
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
package HTML::FormHandler::Traits;
# ABSTRACT: customized replacement for MooseX::Traits
use Moose::Role;
use Class::Load qw/ load_class /;
use namespace::autoclean;

has '_trait_namespace' => (
    init_arg => undef,
    isa      => 'Str',
    is       => 'bare',
);

my %COMPOSED_CLASS_INDEX;
# {
#    'HTML::FormHandler::Field::Text' => { 'Role|Another::Role' => 1 },
#    'HTML::FormHandler::Field::Select' => { 'My::Role' => 2,
#                                            'My::Role|Your::Role' => 3 },
# }
my %COMPOSED_META;
my $composed_index = 0;

sub resolve_traits {
    my ( $class, @traits ) = @_;

    return map {
        my $orig = $_;
        if ( !ref $orig ) {
            my $transformed = transform_trait( $class, $orig );
            load_class($transformed);
            $transformed;
        }
        else {
            $orig;
        }
    } @traits;
}

sub transform_trait {
    my ( $class, $name ) = @_;
    return $1 if $name =~ /^[+](.+)$/;

    my $namespace = $class->meta->find_attribute_by_name('_trait_namespace');
    my $base;
    if ( $namespace->has_default ) {
        $base = $namespace->default;
        if ( ref $base eq 'CODE' ) {
            $base = $base->();
        }
    }

    return $name unless $base;
    return join '::', $base, $name;
}

sub composed_class_name {
    my (%options) = @_;

    my $class     = $options{class};
    my $cache_key = _anon_cache_key( $options{roles} );

    my $index = $COMPOSED_CLASS_INDEX{$class}{$cache_key};
    if ( defined $index ) {
        return "${class}::$index";
    }
    $index = ++$composed_index;
    $COMPOSED_CLASS_INDEX{$class}{$cache_key} = $index;
    return "${class}::$index";
}

sub _anon_cache_key {
    # Makes something like Role|Role::1
    return join( '|', @{ $_[0] || [] } );
}

sub with_traits {
    my ( $class, @traits ) = @_;

    @traits = resolve_traits( $class, @traits );
    return $class->meta unless ( scalar @traits );

    my $class_name = $class->meta->name;
    my $new_class_name = composed_class_name( class => $class_name, roles => \@traits, );
    my $meta;
    if ( $meta = $COMPOSED_META{$new_class_name} ) {
        return $meta->name;
    }
    else {
        $meta = $class->meta->create(
            $new_class_name,
            superclasses => [$class_name],
            roles        => \@traits,
        );
        $COMPOSED_META{$new_class_name} = $meta;
        return $meta->name;
    }
}

sub new_with_traits {
    my ( $class, %args ) = @_;

    my $traits = delete $args{traits} || [];
    my $new_class   = $class->with_traits(@$traits);
    my $constructor = $new_class->meta->constructor_name;
    return $new_class->$constructor(%args);
}


no Moose::Role;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

HTML::FormHandler::Traits - customized replacement for MooseX::Traits

=head1 VERSION

version 0.40057

=head1 SYNOPSIS

Use to get a new composed class with traits:

   my $class = My::Form->with_traits( 'My::Trait', 'Another::Trait' );
   my $form = $class->new;

=head1 AUTHOR

FormHandler Contributors - see HTML::FormHandler

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Gerda Shank.

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