From 35889e01e09360ee274434d77f5f1024a537ed5d Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Thu, 10 Nov 2011 12:28:48 +0000 Subject: ugly serialization helper role no, I'm not changing the name, it's really an ugly thing --- .../MultiValued/UglySerializationHelperRole.pm | 35 ++++++++++++++++++++++ Data-MultiValued/t/json.t | 33 ++------------------ 2 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 Data-MultiValued/lib/Data/MultiValued/UglySerializationHelperRole.pm diff --git a/Data-MultiValued/lib/Data/MultiValued/UglySerializationHelperRole.pm b/Data-MultiValued/lib/Data/MultiValued/UglySerializationHelperRole.pm new file mode 100644 index 0000000..e586dec --- /dev/null +++ b/Data-MultiValued/lib/Data/MultiValued/UglySerializationHelperRole.pm @@ -0,0 +1,35 @@ +package Data::MultiValued::UglySerializationHelperRole; +use Moose::Role; + +sub new_in_place { + my ($class,$hash) = @_; + + my $self = bless $hash,$class; + + for my $attr ($class->meta->get_all_attributes) { + if ($attr->does('Data::MultiValued::AttributeTrait')) { + $attr->_rebless_slot($self); + } + } + return $self; +} + +sub as_hash { + my ($self) = @_; + + my %ret = %$self; + for my $attr ($self->meta->get_all_attributes) { + if ($attr->does('Data::MultiValued::AttributeTrait')) { + my $st = $attr->_as_hash($self); + if ($st) { + $ret{$attr->full_storage_slot} = $st; + } + else { + delete $ret{$attr->full_storage_slot}; + } + } + } + return \%ret; +} + +1; diff --git a/Data-MultiValued/t/json.t b/Data-MultiValued/t/json.t index ed3a31a..5e00080 100644 --- a/Data-MultiValued/t/json.t +++ b/Data-MultiValued/t/json.t @@ -6,7 +6,8 @@ use Moose; use Data::MultiValued::AttributeTrait::Tags; use Data::MultiValued::AttributeTrait::Ranges; use Data::MultiValued::AttributeTrait::TagsAndRanges; -use Data::Printer; + +with 'Data::MultiValued::UglySerializationHelperRole'; has tt => ( is => 'rw', @@ -34,36 +35,6 @@ has ttrr => ( clearer => 'clear_ttrr', ); -sub new_in_place { - my ($class,$hash) = @_; - - my $self = bless $hash,$class; - - for my $attr ($class->meta->get_all_attributes) { - if ($attr->does('Data::MultiValued::AttributeTrait')) { - $attr->_rebless_slot($self); - } - } - return $self; -} - -sub as_hash { - my ($self) = @_; - - my %ret = %$self; - for my $attr ($self->meta->get_all_attributes) { - if ($attr->does('Data::MultiValued::AttributeTrait')) { - my $st = $attr->_as_hash($self); - if ($st) { - $ret{$attr->full_storage_slot} = $st; - } - else { - delete $ret{$attr->full_storage_slot}; - } - } - } - return \%ret; -} } package main; -- cgit v1.2.3