From b398c46825dc73ac92677893dd2a714cd21f8a4d Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 26 Mar 2011 12:40:12 +0000 Subject: collections working, subclasses, strings --- lib/DeWeave/Collection.pm | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'lib/DeWeave/Collection.pm') diff --git a/lib/DeWeave/Collection.pm b/lib/DeWeave/Collection.pm index 42669ba..92c5342 100644 --- a/lib/DeWeave/Collection.pm +++ b/lib/DeWeave/Collection.pm @@ -1,9 +1,10 @@ package DeWeave::Collection; use Moose; use namespace::autoclean; -use MooseX::Types::Moose qw(ArrayRef Int Str Num); +use MooseX::Types::Moose qw(ArrayRef); use JSON::Any; use DeWeave::EDO; +use Lingua::EN::Inflect::Number 'to_S'; has items => ( isa => ArrayRef['DeWeave::WBO'], @@ -11,6 +12,25 @@ has items => ( required => 1, ); +sub _local_part { + my ($class) = @_; + my ($sub) = ($class =~ m{^DeWeave::Collection::(\w+)}); + return $sub; +} + +sub item_class { + my ($class) = @_; + my $sub = ucfirst(to_S(lc($class->_local_part))); + return "DeWeave::BO::$sub" if $sub; + return 'DeWeave::WBO' +} + +sub items_path { + my ($class) = @_; + my $sub = lc($class->_local_part); + return "storage/$sub"; +} + sub from_json { my ($class,$json,$crypt)=@_; @@ -19,11 +39,23 @@ sub from_json { my $args = $j->decode($json); my @items = map { - DeWeave::EDO->new({%$_,__crypt=>$crypt}) + $class->item_class->new({%$_,__crypt=>$crypt}) } @$args; return $class->new({ items => \@items, }); } +sub fetch { + my ($class,$storage,$crypto) = @_; + + my $path = $class->items_path; + my $data = $storage->get_item($path); + + return $class->from_json( + $data, + $crypto, + ); +} + 1; -- cgit v1.2.3