Luke Saunders

VERSION

Version 1.001000

NAME

DBIx::Class::ResultSet::WithMetaData

SYNOPSIS

  package MyApp::Schema::ResultSet::ObjectType;

  use Moose;
  use MooseX::Method::Signatures;
  extends 'DBIx::Class::ResultSet::WithMetaData;

  method with_substr () {
    return $self->_with_meta_key( 
      substr => sub {
        return substr(shift->{name}, 0, 3);
      }
    );
  }

  ...


  # then somewhere else

  my $object_type_arrayref = $object_type_rs->with_substr->display();

  # [{
  #    'artistid' => '1',
  #    'name' => 'Caterwauler McCrae',
  #    'substr' => 'Cat'
  #  },
  #  {
  #    'artistid' => '2',
  #    'name' => 'Random Boy Band',
  #    'substr' => 'Ran'
  #  },
  #  {
  #    'artistid' => '3',
  #    'name' => 'We Are Goth',
  #    'substr' => 'We '
  #  }]

DESCRIPTION

Attach metadata to rows by chaining ResultSet methods together. When the ResultSet is flattened to an ArrayRef the metadata is merged with the row hashes to give a combined 'hash-plus-other-stuff' representation.

METHODS

display

Arguments: none
Return Value: ArrayRef
 $arrayref_of_row_hashrefs = $rs->display();

This method uses DBIx::Class::ResultClass::HashRefInflator to convert all rows in the ResultSet to HashRefs. Then the subrefs that were added via "_with_meta_key" or "_with_meta_hash" are run for each row and the resulting data merged with them.

_with_meta_key

Arguments: key_name => subref($row_hash)
Return Value: ResultSet
 $self->_with_meta_key( substr => sub ($row) { 
   return substr(shift->{name}, 0, 3);
 });

This method allows you populate a certain key for each row hash at "display" time.

_with_object_meta_key

Arguments: key_name => subref($row_hash, $row_obj)
Return Value: ResultSet
 $self->_with_object_meta_key( substr => sub { 
   my ($row_hash, $row_obj) = @_;
   return substr($row_obj->row_method, 0, 3);
 });

The same as "_with_meta_key" but the subref gets the row object as well as the row hash. This should only be used when you need to access row methods as it's slower to inflate objects.

_with_meta_hash

Arguments: subref($row_hash)
Return Value: ResultSet
 $self->_with_meta_hash( sub ($row) { 
   my $row = shift;
   my $return_hash = { substr => substr($row->{name}, 0, 3), substr2 => substr($row->{name}, 0, 4) };
   return $return_hash;
 });

Use this method when you want to populate multiple keys of the hash at the same time. If you just want to populate one key, use "_with_meta_key".

_with_object_meta_hash

Arguments: subref($row_hash, $row_object)
Return Value: ResultSet
 $self->_with_object_meta_hash( sub { 
   my ($row_hash, $row_object) = @_;

   my $return_hash = { substr => substr($row_object->name, 0, 3), substr2 => substr($row_hash->{name}, 0, 4) };
   return $return_hash;
 });

Like "_with_meta_hash" but the subref gets the row object as well as the row hash. This should only be used when you need to access row methods as it's slower to inflate objects.

add_row_info (DEPRECATED)

Arguments: row => DBIx::Class::Row object, info => HashRef to attach to the row
Return Value: ResultSet
 $rs = $rs->add_row_info(row => $row, info => { dates => [qw/mon weds fri/] } );

DEPRECATED - this method is quite slow as it requires that you iterate through the resultset each time you want to add metadata. Replaced by "build_metadata".

AUTHOR

  Luke Saunders <luke.saunders@gmail.com>

THANKS

As usual, thanks to Matt S Trout for the sanity check.

LICENSE

  This library is free software under the same license as perl itself



Hosting generously
sponsored by Bytemark