PageRenderTime 18ms CodeModel.GetById 7ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/HTML/FormHandler/Pages.pm

http://github.com/gshank/html-formhandler
Perl | 68 lines | 57 code | 8 blank | 3 comment | 7 complexity | 10024b0c33c232feb742abdd3e9dd597 MD5 | raw file
 1package HTML::FormHandler::Pages;
 2# ABSTRACT: used in Wizard
 3
 4use Moose::Role;
 5
 6has 'pages' => (
 7    traits     => ['Array'],
 8    isa        => 'ArrayRef[HTML::FormHandler::Page]',
 9    is         => 'rw',
10    default    => sub { [] },
11    auto_deref => 1,
12    handles   => {
13        all_pages => 'elements',
14        clear_pages => 'clear',
15        push_page => 'push',
16        num_pages => 'count',
17        has_pages => 'count',
18        set_page_at => 'set',
19        get_page => 'get',
20    }
21);
22
23has 'page_name_space' => (
24    isa     => 'Str|ArrayRef[Str]|Undef',
25    is      => 'rw',
26    lazy    => 1,
27    builder => 'build_page_name_space',
28);
29
30sub build_page_name_space { '' }
31
32sub page_index {
33    my ( $self, $name ) = @_;
34    my $index = 0;
35    for my $page ( $self->all_pages ) {
36        return $index if $page->name eq $name;
37        $index++;
38    }
39    return;
40}
41
42sub page {
43    my ( $self, $name, $die ) = @_;
44
45    my $index;
46    # if this is a full_name for a compound page
47    # walk through the pages to get to it
48    return undef unless ( defined $name );
49    if ( $name =~ /\./ ) {
50        my @names = split /\./, $name;
51        my $f = $self->form || $self;
52        foreach my $pname (@names) {
53            $f = $f->page($pname);
54            return unless $f;
55        }
56        return $f;
57    }
58    else    # not a compound name
59    {
60        for my $page ( $self->all_pages ) {
61            return $page if ( $page->name eq $name );
62        }
63    }
64    return unless $die;
65    die "Page '$name' not found in '$self'";
66}
67
681;