Dancer-Plugin-Chain

 view release on metacpan or  search on metacpan

lib/Dancer/Plugin/Chain.pm  view on Meta::CPAN

package Dancer::Plugin::Chain;
our $AUTHORITY = 'cpan:YANICK';
# ABSTRACT: Chained actions for Dancer
$Dancer::Plugin::Chain::VERSION = '0.1.1';
use strict;
use warnings;

use Dancer ':syntax';
use Dancer::Plugin;

register chain => sub {
    my $link = Dancer::Plugin::Chain::Link->new( segments => [ @_ ] );
    
    return wantarray ? @$link : $link;
};

register_plugin;

package 
    Dancer::Plugin::Chain::Link;

use Moose;

use Ref::Util qw/ is_coderef is_ref /;
use List::Util qw/ reduce /;

use overload '@{}' => sub { [ $_[0]->as_route ] };

# all segments as passed to the chain
has segments => (
    traits  => [ qw/ Array /],
    isa     => 'ArrayRef',
    is      => 'ro',
    default => sub { [] },
    handles => { all_segments => 'elements' },
);


# segments that are strings ('/foo', '/bar/:id')
has path_segments => (
    traits  => [ qw/ Array /],
    isa     => 'ArrayRef',
    is      => 'ro',
    default => sub { [] },
    lazy    => 1,
    default => sub { 
        my $self = shift;
        [
            grep { !is_ref($_) }
            map  { 
                eval { $_->isa( __PACKAGE__ ) } ? $_->all_path_segments : $_;
            } $self->all_segments
        ]
    },
    handles => {
        add_to_path       => 'push',
        all_path_segments => 'elements',
        path              => [ join => '' ],

    },
);

# segments that are code blocks
has code_blocks => (
    traits  => [ qw/ Array /],
    isa     => 'ArrayRef',
    is      => 'ro',
    lazy    => 1,
    default => sub { 
        my $self = shift;
        [
            grep { is_coderef($_) }
            map  { 
                eval { $_->isa( __PACKAGE__ ) } ? $_->all_code_blocks : $_;
            } $self->all_segments
        ]
    },
    handles => {
        add_to_code     => 'push',
        all_code_blocks => 'elements'
    },
);

sub code {



( run in 1.506 second using v1.01-cache-2.11-cpan-39bf76dae61 )