From: Arthur Axel 'fREW' Schmidt Date: Wed, 10 Mar 2010 04:04:06 +0000 (-0600) Subject: cleaner inflation interface X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=dbsrgits%2FDBIx-Data-Store-old.git cleaner inflation interface --- diff --git a/lib/DBIx/Data/Collection/Set.pm b/lib/DBIx/Data/Collection/Set.pm index 3e965ba..384dc79 100644 --- a/lib/DBIx/Data/Collection/Set.pm +++ b/lib/DBIx/Data/Collection/Set.pm @@ -6,6 +6,24 @@ use Data::Perl::Stream::Array; use Data::Perl::Collection::Set; use Scalar::Util qw(weaken refaddr); +has _inflator => ( + is => 'ro', + handles => { + _inflate => 'inflate', + _deflate => 'deflate', + _merge => 'merge', + _refresh => 'refresh', + _deflate_spec => 'deflate_spec', + _merge_spec => 'merge_spec', + }, + default => method { + require DBIx::Data::Store::SimpleInflator; + my $args = {}; + $args->{class} = $self->_class if $self->_has_class; + DBIx::Data::Store::SimpleInflator->new($args) + }, +); + has _store => (is => 'ro', required => 1, init_arg => 'store'); has _class => (is => 'ro', predicate => '_has_class', init_arg => 'class'); @@ -140,46 +158,6 @@ method _setup_observation_of ($other) { return } -## 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 - -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 -} ## methods to get ids diff --git a/lib/DBIx/Data/Store/SimpleInflator.pm b/lib/DBIx/Data/Store/SimpleInflator.pm new file mode 100644 index 0000000..645769d --- /dev/null +++ b/lib/DBIx/Data/Store/SimpleInflator.pm @@ -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;