package DeWeave::WBO;{ use Moose; use namespace::autoclean; use MooseX::Types::Moose qw(Int Str Num); use JSON::Any; use Try::Tiny; use Log::Log4perl ':easy'; use Data::Dump 'pp'; has id => ( isa => Str, required => 1, is => 'ro', ); has modified => ( isa => Num, required => 1, is => 'ro', ); has sortindex => ( isa => Num, required => 0, is => 'ro', ); has payload => ( traits => ['WBOInternal'], isa => Str, required => 1, is => 'ro', ); has ttl => ( isa => Int, required => 0, is => 'ro', ); sub _debug_data { my ($self,$msg,$data) = @_; return unless get_logger()->is_debug; local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth+1; my %data_clean;my @keys_clean = grep {!/^_/} keys %$data; @data_clean{@keys_clean} = @$data{@keys_clean}; DEBUG $msg, { filter => \&pp, value => \%data_clean, }; } sub from_json { my ($class,$json,$crypt)=@_; my $j = JSON::Any->new; my $args = $j->decode($json); $class->_debug_data('$args ',$args); $args = $class->unpack_args($args,$crypt); return $class->new($args); } sub unpack_args { my ($class,$args,$crypt) = @_; return $args unless defined $args->{payload}; my $j = JSON::Any->new; my $extra_args = $j->decode($args->{payload}); my $decrypt_args = {}; if (defined $crypt && exists $extra_args->{ciphertext}) { my $decrypted_payload = $crypt->decrypt($extra_args); if (defined $decrypted_payload) { $decrypt_args = $j->decode($decrypted_payload); } } @$args{keys %$extra_args} = values %$extra_args; @$args{keys %$decrypt_args} = values %$decrypt_args; delete @$args{grep {!defined $args->{$_}} keys %$args}; $class->_debug_data('buildargs: ',$args); 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; __END__ =head1 AUTHOR Gianni Ceccarelli =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2012 by Gianni Ceccarelli. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3. =cut