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 --- bin/client | 16 ++++++++-------- lib/DeWeave/BO/Client.pm | 26 ++++++++++++++++++++++++++ lib/DeWeave/Collection.pm | 36 ++++++++++++++++++++++++++++++++++-- lib/DeWeave/Collection/Clients.pm | 8 ++++++++ lib/DeWeave/EDO.pm | 5 ++++- lib/DeWeave/WBO.pm | 33 ++++++++++++++++++++++++++++++++- 6 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 lib/DeWeave/BO/Client.pm create mode 100644 lib/DeWeave/Collection/Clients.pm diff --git a/bin/client b/bin/client index 8675f18..c5e2a65 100644 --- a/bin/client +++ b/bin/client @@ -6,8 +6,9 @@ use DeWeave::Storage; use DeWeave::Crypto; use DeWeave::Collection; use Getopt::Long; +use Class::MOP; -my ($server,$username,$password,$sync_key); +my ($server,$username,$password,$sync_key,$what); { my $p = Getopt::Long::Parser->new( @@ -38,12 +39,11 @@ my $crypto = DeWeave::Crypto->new({ sync_key => $sync_key, }); -use Data::Dump 'pp'; -my $data = $storage->get_item('storage/tabs'); +$what=ucfirst(shift); +my $class = "DeWeave::Collection::$what"; +Class::MOP::load_class($class); -my $foo = DeWeave::Collection->from_json( - $data, - $crypto, -); +my $foo = $class->fetch($storage,$crypto); + +print $_->as_string,"\n" for @{$foo->items}; -print pp $foo; diff --git a/lib/DeWeave/BO/Client.pm b/lib/DeWeave/BO/Client.pm new file mode 100644 index 0000000..e9f9ae8 --- /dev/null +++ b/lib/DeWeave/BO/Client.pm @@ -0,0 +1,26 @@ +package DeWeave::BO::Client; +use Moose; +use namespace::autoclean; +use MooseX::Types::Moose qw(Str ArrayRef); + +extends 'DeWeave::EDO'; + +has name => ( + isa => Str, + required => 1, + is => 'ro', +); + +has type => ( + isa => Str, + required => 1, + is => 'ro', +); + +has commands => ( + isa => ArrayRef[Str], + required => 0, + is => 'ro', +); + +1; 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; diff --git a/lib/DeWeave/Collection/Clients.pm b/lib/DeWeave/Collection/Clients.pm new file mode 100644 index 0000000..a49e54a --- /dev/null +++ b/lib/DeWeave/Collection/Clients.pm @@ -0,0 +1,8 @@ +package DeWeave::Collection::Clients; +use Moose; +use namespace::autoclean; +use DeWeave::BO::Client; + +extends 'DeWeave::Collection'; + +1; diff --git a/lib/DeWeave/EDO.pm b/lib/DeWeave/EDO.pm index f7a605f..3c32c84 100644 --- a/lib/DeWeave/EDO.pm +++ b/lib/DeWeave/EDO.pm @@ -4,23 +4,25 @@ use namespace::autoclean; use MooseX::Types::Moose qw(Int Str Num); use JSON::Any; use Try::Tiny; -use Data::Dump 'pp'; extends 'DeWeave::WBO'; has ciphertext => ( + traits => ['WBOInternal'], isa => Str, required => 1, is => 'ro', ); has IV => ( + traits => ['WBOInternal'], isa => Str, required => 1, is => 'ro', ); has hmac => ( + traits => ['WBOInternal'], isa => Str, required => 1, is => 'ro', @@ -43,6 +45,7 @@ around BUILDARGS => sub { @$args{keys %$extra_args} = values %$extra_args; } + return $args; }; diff --git a/lib/DeWeave/WBO.pm b/lib/DeWeave/WBO.pm index 8d8a1ba..56bc3e1 100644 --- a/lib/DeWeave/WBO.pm +++ b/lib/DeWeave/WBO.pm @@ -1,9 +1,10 @@ -package DeWeave::WBO; +package DeWeave::WBO;{ use Moose; use namespace::autoclean; use MooseX::Types::Moose qw(Int Str Num); use JSON::Any; use Try::Tiny; +use Data::Dump 'pp'; has id => ( isa => Str, @@ -24,6 +25,7 @@ has sortindex => ( ); has payload => ( + traits => ['WBOInternal'], isa => Str, required => 1, is => 'ro', @@ -66,5 +68,34 @@ around BUILDARGS => sub { return $args; }; +sub as_string { + my ($self) = @_; + + my $meta = $self->meta; + + my $str = ''; + + for my $attribute ( $meta->get_all_attributes ) { + + next if $attribute->does('DeWeave::WBO::Meta::Attribute::Internal'); + next unless $attribute->has_value($self); + + my $reader = $attribute->get_read_method; + $str .= sprintf "%s -> %s\n", + $attribute->name, + pp ($self->$reader); + } + + return $str; +} +} + +package DeWeave::WBO::Meta::Attribute::Internal;{ +use Moose::Role; +} + +package Moose::Meta::Attribute::Custom::Trait::WBOInternal;{ +sub register_implementation { 'DeWeave::WBO::Meta::Attribute::Internal' } +} 1; -- cgit v1.2.3