PageRenderTime 33ms CodeModel.GetById 2ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/HTML/FormHandler/Moose.pm

http://github.com/gshank/html-formhandler
Perl | 81 lines | 61 code | 19 blank | 1 comment | 6 complexity | b2a5adcbcf761241e95f79608188feb6 MD5 | raw file
 1package  HTML::FormHandler::Moose;
 2# ABSTRACT: to add FormHandler sugar
 3
 4use Moose;
 5use Moose::Exporter;
 6use Moose::Util::MetaRole;
 7use HTML::FormHandler::Meta::Role;
 8
 9=head1 SYNOPSIS
10
11Enables the use of field specification sugar (has_field).
12Use this module instead of C< use Moose; >
13
14   package MyApp::Form::Foo;
15   use HTML::FormHandler::Moose;
16   extends 'HTML::FormHandler';
17
18   has_field 'username' => ( type => 'Text', ... );
19   has_field 'something_else' => ( ... );
20
21   no HTML::FormHandler::Moose;
22   1;
23
24=cut
25
26Moose::Exporter->setup_import_methods(
27    with_meta => [ 'has_field', 'has_page', 'has_block', 'apply' ],
28    also        => 'Moose',
29);
30
31sub init_meta {
32    my $class = shift;
33
34    my %options = @_;
35    Moose->init_meta(%options);
36    my $meta = Moose::Util::MetaRole::apply_metaroles(
37        for             => $options{for_class},
38        class_metaroles => {
39            class => [ 'HTML::FormHandler::Meta::Role' ]
40        }
41    );
42    return $meta;
43}
44
45sub has_field {
46    my ( $meta, $name, %options ) = @_;
47    my $names = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
48
49    unless ($meta->found_hfh) {
50        my @linearized_isa = $meta->linearized_isa;
51        if( grep { $_ eq 'HTML::FormHandler' || $_ eq 'HTML::FormHandler::Field' } @linearized_isa ) {
52            $meta->found_hfh(1);
53        }
54        else {
55            die "Package '" . $linearized_isa[0] . "' uses HTML::FormHandler::Moose without extending HTML::FormHandler[::Field]";
56        }
57    }
58
59    $meta->add_to_field_list( { name => $_, %options } ) for @$names;
60}
61
62sub has_page {
63    my ( $meta, $name, %options ) = @_;
64    my $names = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
65
66    $meta->add_to_page_list( { name => $_, %options } ) for @$names;
67}
68
69sub has_block {
70    my ( $meta, $name, %options ) = @_;
71    $meta->add_to_block_list( { name => $name, %options } );
72}
73
74sub apply {
75    my ( $meta, $arrayref ) = @_;
76
77    $meta->add_to_apply_list( @{$arrayref} );
78}
79
80use namespace::autoclean;
811;