summaryrefslogtreecommitdiff
path: root/lib/DeWeave/WBO.pm
blob: 74e5c084d49b75ab98e8d6148bfb8f3b79f35b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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 <dakkar@thenautilus.net>
 
=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