MooseX::AttributeCloner - MooseX::AttributeCloner documentation


MooseX-AttributeCloner documentation  | view source Contained in the MooseX-AttributeCloner distribution.

Index


NAME

Top

MooseX::AttributeCloner

VERSION

Top

0.2

SYNOPSIS

Top

  package My::Class;
  use Moose;
  with qw{MooseX::AttributeCloner};

  my $NewClassObject = $self->new_with_cloned_attributes(q{New::Class}, {});
  1;

DESCRIPTION

Top

The purpose of this Role is to take all the attributes which have values in the current class, and populate them directly into a new class object. The purpose of which is that if you have data inputted on the command line that needs to propagate through to later class objects, you shouldn't need to do the following

  my $oNewClass = New::Class->new({
    attr1 => $self->attr1,
    attr2 => $self->attr2,
    ...
  });

Which is going to get, quite frankly, tedious in the extreme. Particularly when you have more 2 class objects in your chain.

SUBROUTINES/METHODS

Top

new_with_cloned_attributes

This takes a package name as the first argument, plus an optional additional $arg_refs hash. It will return a class object of the package populated with any matching attribute data from the current object, plus anything in the $arg_refs hash.

attributes_as_command_options

returns all the built attributes that are not objects as a string of command_line options only the first level of references will be passed through, multi-dimensional data structures should use the json serialisation option and deserialise it on object construction or script running

  my $command_line_string = $class->attributes_as_command_options();
  --attr1 val1 --attr2 val2

By default, it returns the options with a double dash, space separated, and not quoted (as above). These can be switched by submitting a hash_ref as follows

  my $command_line_string = $class->attributes_as_command_options({
    equal => 1,
    quotes => 1,
    single_dash => 1,
  });

Although, if you are passing a hash_ref, this will always be space separated attr val.

You may exclude some values if you wish. To do this, use the example below

  my $command_line_string = $class->attributes_as_command_options({
    excluded_attributes => [ qw( init_arg1 init_arg2 init_arg3 ) ],
  });

Note here you are using the init_arg, rather than any reader/accessor method names to exclude the option, as it is the init_arg which will be used in the command_line string generated

Sometimes you may have floating attributes for argv and ARGV (we have discovered this with MooseX::Getopt). As such, these are being treated as 'special', and these will be excluded by default. You can request them to be included as follows.

  my $command_line_string = $class->attributes_as_command_options({
    included_argv_attributes => [ qw( argv ARGV ) ],
  });

No additional command_line params can be pushed into this, it only deals with the attributes already set in the current object

Note, it is your responsibility to know where you may need any of these to be on or off, unless they have no init_arg (init_arg => undef)

attributes_as_json

returns all the built attributes that are not objects as a JSON string

  my $sAttributesAsJSON = $class->attributes_as_json();

attributes_as_escaped_json

as attributes_as_json, except it is an escaped JSON string, so that this could be used on a command line

  my $sAttributesAsEscapedJSON = $class->attributes_as_escaped_json();

This uses JSON to generate the string, removing any objects before stringifying, and then parses it through a regex to generate a string with escaped characters Note, because objects are removed, arrays will remain the correct length, but have null in them =cut

sub attributes_as_escaped_json { my ($self) = @_; my $json = $self->attributes_as_json(); $json =~ s{([^A-Za-z0-9_-])}{\\$1}gmxs; return $json; }

sub attributes_as_json { my ($self) = @_;

  my $attributes = $self->_hash_of_attribute_values();
  # remove any objects from the hash
  $self->_traverse_hash($attributes);
  my $json = to_json($attributes);
  return $json;
}

attributes_as_hashref

Returns a hashref of the attributes this object has built, optionally excluding any specified attributes. Includes objects which may have been built.

  my $hAttributesAsHashref = $class->attributes_as_hashref({
    excluded_attributes => [ qw( init_arg1 init_arg2 init_arg3 ) ],
  });

Note here you are using the init_arg, rather than any reader/accessor method names to exclude the option

DIAGNOSTICS

Top

CONFIGURATION AND ENVIRONMENT

Top

DEPENDENCIES

Top

Moose::Role
Carp
English -no_match_vars
Readonly
JSON

INCOMPATIBILITIES

Top

BUGS AND LIMITATIONS

Top

This is more than likely to have bugs in it. Please contact me with any you find (or submit to RT) and any patches.

AUTHOR

Top

setitesuk

LICENSE AND COPYRIGHT

Top


MooseX-AttributeCloner documentation  | view source Contained in the MooseX-AttributeCloner distribution.