--- /dev/null
+package BSE::CustomData;
+use strict;
+use JSON ();
+
+our $VERSION = "1.000";
+
+my $json;
+
+sub get_custom_all {
+ my ($self) = @_;
+
+ my $data = $self->custom_data;
+ my $result;
+ if (defined $data && length $data) {
+ eval { $result = JSON::decode_json($data); 1};
+ }
+ return $result || {};
+}
+
+sub set_custom_all {
+ my ($self, $hash) = @_;
+
+ ref $hash eq "HASH"
+ or die "set_custom_all() called with non-hash";
+ my $data;
+ eval { $data = JSON::encode_json($hash) };
+ $data ||= "{}";
+ $self->set_custom_data($data);
+}
+
+sub set_custom {
+ my ($self, $name, $value) = @_;
+
+ my $data = $self->get_custom_all;
+ $data->{$name} = $value;
+ $self->set_custom_all($data);
+}
+
+sub get_custom {
+ my ($self, $name) = @_;
+
+ my $data = $self->get_custom_all;
+ return $data->{$name};
+}
+
+1;
+
+=head1 NAME
+
+BSE::CustomData - mix in for treating a field of a table as JSON.
+
+=head1 SYNOPSIS
+
+ package YourTable;
+ use parent 'Squirrel::Row', 'BSE::Custom';
+
+ sub columns { ... custom_data }
+
+ my $item = YourTable->getByPkey(...);
+ $item->set_custom($name => $value);
+ my $value = $item->get_custom($name);
+
+=head1 DESCRIPTION
+
+A simpler, cheaper alternative to the table metadata system, intended
+for uses where that would be far too heavy.
+
+Expects your class to define the following methods:
+
+=over
+
+=item *
+
+set_custom_data($value) - sets the custom data field in the row object.
+
+=item *
+
+custom_data - retrieves the custom data field in the row object.
+
+=back
+
+The data should be stored/retrieved as a BLOB, though JSON column
+types may also work.
+
+Provides the following methods:
+
+=over
+
+=item *
+
+set_custom($name, $value) - set the given field in the custom data to
+C<$value>. C<$name> must be a simple string. C<$value> must be a
+JSON compatible structure, including strings, numbers, arrays, hashes,
+and may not include blessed objects.
+
+=item *
+
+set_custom_all($hash) - set the entire custom data to a given hash,
+used by set_custom().
+
+=item *
+
+get_custom($name) - retrieve a value set previously by set_custom.
+
+=item *
+
+get_custom_all() - get the custom data hash. Used by get_custom() and
+set_custom().
+
+=back
+
+=cut
use strict;
# represents an order line item from the database
use Squirrel::Row;
+use BSE::CustomData;
use vars qw/@ISA/;
-@ISA = qw/Squirrel::Row/;
+@ISA = qw/Squirrel::Row BSE::CustomData/;
-our $VERSION = "1.007";
+our $VERSION = "1.008";
sub table { "order_item" }
return qw/id productId orderId units price wholesalePrice gst options
customInt1 customInt2 customInt3 customStr1 customStr2 customStr3
title description subscription_id subscription_period max_lapsed
- session_id product_code tier_id product_discount product_discount_units/;
+ session_id product_code tier_id product_discount product_discount_units
+ custom_data/;
}
sub db_columns {
tier_id => undef,
product_discount => 0,
product_discount_units => 0,
+ custom_data => undef,
);
}