diff options
Diffstat (limited to 'lib/DeWeave/Collection.pm')
-rw-r--r-- | lib/DeWeave/Collection.pm | 36 |
1 files changed, 34 insertions, 2 deletions
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; |