The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Form::Factory::Processor - Moos-ish helper for action classes

VERSION

version 0.015

SYNOPSIS

  package MyApp::Action::Foo;
  use Form::Factory::Processor;

  has_control name => (
      control => 'text',
      options => {
          label => 'Your Name',
      },
      features => {
          trim     => 1,
          required => 1,
          length   => {
              minimum => 3,
              maximum => 15,
          },
      },
  );

  has_cleaner convert_to_underscores => sub {
      my $self = shift;
      my $name = $self->controls->{name}->current_value;
      $name =~ s/\W+/_/g;
      $self->controls->{name}->current_value($name);
  };

  has_checker do_not_car_for_names_start_with_r => sub {
      my $self = shift;
      my $value = $self->controls->{name}->current_value;

      if ($value =~ /^R/i) {
          $self->error('i do not like names start with "R," get a new name');
          $self->result->is_valid(0);
      }
  };

  has_pre_processor log_start => sub {
      my $self = shift;
      MyApp->logger->debug("START Foo " . Time::HiRes::time());
  };

  has_post_processor log_stop => sub {
      my $self = shift;
      MyApp->logger->debug("STOP Foo " . Time::HiRes::time());
  };

  sub run {
      my $self = shift;
      MyApp->do_something_to_your_name($self->name);
  }

DESCRIPTION

This is the helper class used to define actions. This class automatically imports the subroutines described in this documentaiton as well as any defined in Moose. It also grants your action class and its meta-class the correct roles.

METHODS

init_meta

Sets up the roles and meta-class information for your action class.

has_control

  has_control $name => (
      %usual_has_options,

      control  => $control_short_name,
      options  => \%control_options,
      features => \%control_features,
  );

This works very similar to "has" in Moose. This applies the Form::Factory::Action::Meta::Attribute::Control trait to the attribute and sets up other defaults.

The following defaults are set:

is

Control attributes are read-only by default.

isa

Control attributes are string by default. Be careful about using an isa setting that differs from the control's value. If you do, make sure you use features to make certain the type is the correct kind of thing or that a coercion to the correct type of thing is also setup.

control

This will default to "text" if not set.

options

An empty hash reference is used by default.

features

An empty hash references is used by default.

You may pass any options you could pass to has as well as the additional options for features, control options, etc. This also supports the '+name' syntax for altering attributes that are inherited from a parent class. Currently, only the features option is supported for this, which allows you to add new features or even to turn off features from the parent class. For example, if a control is setup in a parent like this:

  has_control name => (
      control   => 'text',
      features  => {
          trim     => 1,
          required => 1,
          length   => {
              maximum => 20,
              minimum => 3,
           },
      },
  );

A child class may choose to turn the required off and change the length checks by placing this in the subclass definition:

  has_control '+name' => (
      features => {
          required => 0,
          length   => {
              maximum => 20,
              minimum => 10,
          },
      },
  );

The trim feature in the parent would remain in place as originally defined, the required feature is now turned off in the child class, and the length feature options have been replaced. This is done with a shallow merge, so top-level keys in the child class will replace top-level keys in the parent, but any listed in the parent, but not the child remain unchanged.

DO NOT use the required attribute option on controls. If you try to do so, the call to has_control will croak because this will not work with how attributes are setup. If you need an attribute to be required, do not use a control or use the required feature instead.

use_feature

This function is used to make an action use a particular form feature. It's usage is as follows:

  use_feature $name => \%options;

The %options are optional. So,

  use_feature $name;

will also work if you do not need to pass any features.

The $name is a short name for the feature class. For example, the name "require_none_or_all" will load the feature defined in Form::Factory::Feature::RequireNoneOrAll.

deferred_value

  has_control publish_on => (
      control => 'text',
      options => {
          default_value => deferred_value {
              my ($action, $control_name) = @_;
              DateTime->now->iso8601,
          },
      },
  );

This is a helper for deferring the calculation of a value. This works similar to Scalar::Defer to defer the calculation, but with an important difference. The subroutine is passed the action object (such as it exists while the controls are being constructed) and the control's name. The control itself doesn't exist yet when the subroutine is called.

has_cleaner

  has_cleaner $name => sub { ... }

Adds some code called during the clean phase.

has_checker

  has_checker $name => sub { ... }

Adds some code called during the check phase.

has_pre_processor

  has_pre_processor $name => sub { ... }

Adds some code called during the pre-process phase.

has_post_processor

  has_post_processor $name => sub { ... }

Adds some code called during the post-process phase.

SEE ALSO

Form::Factory::Action

AUTHOR

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2009 Qubling Software LLC.

This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.