NAME

DBIx::Class::InflateColumn::Serializer::Role::HashContentAccessor - Parameterized Moose role which provides accessor methods for values stored in a hash-like structure

VERSION

version 0.001000

SYNOPSIS

In your result (table) classes:

__PACKAGE__->load_components("InflateColumn::Serializer");

__PACKAGE__->add_columns(
	...,
	hproperties => {
		data_type        => "hstore",
		is_nullable      => 1,
		serializer_class => "Hstore"
	},
	jproperties => {
		data_type        => "text",
		is_nullable      => 1,
		serializer_class => "JSON"
	},
	...,
);

with 'DBIx::Class::InflateColumn::Serializer::Role::HashContentAccessor' => {
	column => 'jproperties',
	name   => 'jproperty',
};
with 'DBIx::Class::InflateColumn::Serializer::Role::HashContentAccessor' => {
	column => 'hproperties',
	name   => 'hproperty',
};

Then in your application code:

$object->set_hproperty(foo => 10, bar => 20);
$object->set_jproperty(key => "test");
$object->update();

$object->get_hproperty('foo');
$object->get_hproperty('bar');
$object->get_hproperty('baz', 'default');

$object->delete_hproperty('foo');
$object->delete_hproperty('bar');
$object->update();

DESCRIPTION

This parameterized role provides methods to access values of a column that stores 'hash-like' data in a DBIx::Class-based database schema that uses Moose. It assumes that the (de)serializing of the column in done using something like DBIx::Class::InflateColumn::Serializer, i.e. that the inflated values are hash references that get serialized to something the database can store on INSERT/UPDATE.

This module provides the following advantages over using the inflated hashref directly:

  • If the column is nullable, you don't have to take care of NULL values yourself - the methods provided by this role do that already.

  • It's easy to provide a default when getting the value of a key which is not necessarily already stored in the column data.

  • If you remove the key-value-based column and replace it with dedicated columns in the future, you can simply remove the role and provide compatible accessors yourself. This allows you to keep the interface of the result class.

PARAMETERS

column

Column where the data is stored.

name

Suffix for the accessors that will be generated.

GENERATED METHODS

set_$name

Set given properties. Takes a hash of key/value pairs to set.

get_$name

Get the given property. Also takes an optional default value which is returned if the property is undefined.

delete_$name

Delete the properties with the given names.

SEE ALSO

AUTHOR

Manfred Stock <mstock@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Manfred Stock.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.