DBIx::Class::InflateColumn::Serializer::Role::HashContentAccessor - Parameterized Moose role which provides accessor methods for values stored in a hash-like structure
version 0.001000
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();
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.
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.
nullable
NULL
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.
Column where the data is stored.
Suffix for the accessors that will be generated.
Set given properties. Takes a hash of key/value pairs to set.
Get the given property. Also takes an optional default value which is returned if the property is undefined.
Delete the properties with the given names.
DBIx::Class::InflateColumn - Underlying mechanism for deflating/inflating complex data.
DBIx::Class::InflateColumn::Serializer - Inflators for data structures, uses eg. JSON to store the data in the database.
DBIx::Class::InflateColumn::Serializer::Hstore - Serializer which uses the HStore type for storage in the database.
http://www.postgresql.org/docs/current/static/hstore.html - hstore type in PostgreSQL.
hstore
Manfred Stock <mstock@cpan.org>
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.
To install DBIx::Class::InflateColumn::Serializer::Role::HashContentAccessor, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Class::InflateColumn::Serializer::Role::HashContentAccessor
CPAN shell
perl -MCPAN -e shell install DBIx::Class::InflateColumn::Serializer::Role::HashContentAccessor
For more information on module installation, please visit the detailed CPAN module installation guide.