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

NAME

App::vaporcalc::Role::UI::Cmd - Helper for vaporcalc command objects

SYNOPSIS

  # An example command subject;
  # placed in the App::vaporcalc::Cmd::Subject:: namespace so it can be found
  # by an App::vaporcalc::CmdEngine instance:
  package App::vaporcalc::Cmd::Subject::Foo;

  use Defaults::Modern;

  use Moo;
  with 'App::vaporcalc::Role::UI::Cmd';

  # To be listed in an App::vaporcalc::CmdEngine's 'subject_list';
  # must be a class method (not an attribute):
  sub _subject { 'foo' }

  # To provide a default verb:
  has '+verb' => ( builder => sub { 'show' } );

  # Add verbs that return an App::vaporcalc::Cmd::Result:
  method _action_view { $self->_action_show }
  method _action_show {
    $self->create_result(
      string => "hello world!"
    )
  }

DESCRIPTION

A Moo::Role providing attributes & behavior common to App::vaporcalc command objects.

Command objects define a "subject" that provides "verbs" -- methods prefixed with _action_ that have access to the current "recipe" object and command "params". Executing a verb returns a App::vaporcalc::Cmd::Result (see "create_result") or throws an exception (see "throw_exception") in case of failure.

If using an App::vaporcalc::CmdEngine, consumers of this role will likely also want to define a _subject class method returning a string to be taken as the command subject when building the CmdEngine's subject_list.

The given _subject string must translate to the relevant class name by trimming spaces and CamelCasing; for example:

  sub _subject { 'flavor' }    # -> ::Cmd::Subject::Flavor
  sub _subject { 'nic base' }  # -> ::Cmd::Subject::NicBase

ATTRIBUTES

verb

The action to perform.

An empty string by default. Consumers may want to override to provide a default action.

params

The parameters for the command, as a "ArrayObj" in List::Objects::Types.

Can be coerced from a plain ARRAY.

recipe

The App::vaporcalc::Recipe object being operated on.

Required by default.

METHODS

execute

A default dispatcher that, when called, converts "verb" to lowercase and attempts to find a method named _action_$verb to call.

throw_exception

  $self->throw_exception(
    message => 'failed!'
  );

Throw an exception object.

create_result

Builds an App::vaporcalc::Cmd::Result instance.

munge_recipe

  my $new_recipe = $self->munge_recipe( 
    target_vg => 50, 
    target_pg => 50 
  );

Calls TO_JSON on the current "recipe" object, merges in the given key/value pairs, and returns a new App::vaporcalc::Recipe with the appropriate values.

SEE ALSO

App::vaporcalc::Cmd::Result

App::vaporcalc::CmdEngine

App::vaporcalc::Role::UI::ParseCmd

App::vaporcalc::Role::UI::PrepareCmd

AUTHOR

Jon Portnoy <avenj@cobaltirc.org>