cleaner inflation interface
[dbsrgits/DBIx-Data-Store-old.git] / lib / DBIx / Data / Store / SimpleInflator.pm
diff --git a/lib/DBIx/Data/Store/SimpleInflator.pm b/lib/DBIx/Data/Store/SimpleInflator.pm
new file mode 100644 (file)
index 0000000..645769d
--- /dev/null
@@ -0,0 +1,51 @@
+package DBIx::Data::Store::SimpleInflator;
+
+## thunking between the store representation and the set representation
+#
+# _inflate is raw data -> final repr
+# _deflate is final repr -> raw data
+# _merge takes final repr + raw data and updates the repr
+#    (this is used for pk-generated values and later lazy loading)
+#
+# _deflate_spec is attributes of final repr -> raw data
+# _merge_spec is final repr + extra attributes and update repr
+
+use Moose;
+use Method::Signatures::Simple;
+
+has class => (is => 'ro', predicate => '_has_class' );
+
+method inflate($raw) {
+  bless($raw, $self->class) if $self->_has_class;
+  $raw
+}
+
+method deflate($obj) {
+  +{ %$obj }
+}
+
+method merge ($obj, $raw) {
+  @{$obj}{keys %$raw} = values %$raw;
+  $obj
+}
+
+method refresh ($obj, $raw) {
+  # if $obj has been changed but not flushed we'd destroy data doing
+  # a blind merge - but if $obj has change tracking of some sort then
+  # we -could- do something safely, so this method exists to be mangled
+  # by subclasses
+  $obj
+}
+
+method deflate_spec ($spec) {
+  $spec
+}
+
+method merge_spec ($obj, $spec) {
+  @{$obj}{keys %$spec} = values %$spec;
+  $obj
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;