Catalyst-Model-FormFu

 view release on metacpan or  search on metacpan

lib/Catalyst/Model/FormFu.pm  view on Meta::CPAN

package Catalyst::Model::FormFu;
BEGIN {
  $Catalyst::Model::FormFu::VERSION = '0.004';
}

# ABSTRACT: Speedier interface to HTML::FormFu for Catalyst

use strict;
use warnings;
use HTML::FormFu;
use HTML::FormFu::Library;
use Scalar::Util qw(weaken);
use Moose;
use namespace::clean -except => 'meta';

extends 'Catalyst::Model';
with 'Catalyst::Component::InstancePerContext';

has model_stash             => ( is => 'ro', isa => 'HashRef' );
has constructor             => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
has context_stash           => ( is => 'ro', isa => 'Str', default => 'context' );
has config_callback         => ( is => 'ro', isa => 'Bool', default => 1 );
has forms                   => ( is => 'ro', isa => 'HashRef' );
has cache                   => ( is => 'ro', isa => 'HashRef', builder => '_build_cache' );
has languages_from_context  => ( is => 'ro', isa => 'Bool', default => 0 );
has localize_from_context   => ( is => 'ro', isa => 'Bool', default => 0 );
has default_action_use_name => ( is => 'ro', isa => 'Bool', default => 0 );
has default_action_use_path => ( is => 'ro', isa => 'Bool', default => 0 );


sub _build_cache
{
    my $self = shift;

    my %cache;

    while ( my ($id, $config_file) = each %{$self->forms} )
    {
        my %args = ( query_type => 'Catalyst', %{$self->constructor} );
        my $form = HTML::FormFu->new(\%args);
        $form->load_config_file($config_file);
        $cache{$id} = $form;
    }

    return \%cache;
}

sub build_per_context_instance {

    my ($self, $c) = @_;

    my %args;

    # cache and query
    $args{cache} = $self->cache;
    $args{query} = $c->request;

    ### stash
    $args{stash}{$self->context_stash} = $c;
    weaken $args{stash}{$self->context_stash};
    $args{stash}{schema} = $c->model($self->model_stash->{schema}) if $self->model_stash;

    ### config_callback
    $args{config_callback}{plain_value} = sub
    {
        return unless defined $_;

        if ( /__uri_for\(/ )
        {
            s{__uri_for\((.+?)\)__}
             { $c->uri_for( split( '\s*,\s*', $1 ) ) }eg
         }

        if ( /__path_to\(/ )
        {
            s{__path_to\(\s*(.+?)\s*\)__}
             { $c->path_to( split( '\s*,\s*', $1 ) ) }eg
        }

        if ( /__config\(/ )
        {
            s{__config\((.+?)\)__}
             { $c->config->{$1}  }eg
        }



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