line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Telegram::Bot::Object::Base; |
2
|
|
|
|
|
|
|
$Telegram::Bot::Object::Base::VERSION = '0.021'; |
3
|
|
|
|
|
|
|
# ABSTRACT: The base class for all Telegram::Bot::Object objects. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
3107
|
use Mojo::Base -base; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
39
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has '_brain'; # a reference to our brain |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
55
|
|
|
55
|
1
|
103
|
sub arrays { qw// } # override if needed |
12
|
|
|
|
|
|
|
sub _field_is_array { |
13
|
86
|
|
|
86
|
|
111
|
my $self = shift; |
14
|
86
|
|
|
|
|
106
|
my $field = shift; |
15
|
86
|
100
|
|
|
|
179
|
if (grep /^$field$/, $self->arrays) { |
16
|
1
|
|
|
|
|
4
|
return 1; |
17
|
|
|
|
|
|
|
} |
18
|
85
|
|
|
|
|
243
|
return; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
85
|
|
|
85
|
1
|
161
|
sub array_of_arrays { qw// } #override if needed |
23
|
|
|
|
|
|
|
sub _field_is_array_of_arrays { |
24
|
85
|
|
|
85
|
|
127
|
my $self = shift; |
25
|
85
|
|
|
|
|
106
|
my $field = shift; |
26
|
85
|
50
|
|
|
|
141
|
if (grep /^$field$/, $self->array_of_arrays) { |
27
|
0
|
|
|
|
|
0
|
return 1; |
28
|
|
|
|
|
|
|
} |
29
|
85
|
|
|
|
|
135
|
return; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# create an object from a hash. Needs to deal with the nested types, and |
34
|
|
|
|
|
|
|
# arrays |
35
|
|
|
|
|
|
|
sub create_from_hash { |
36
|
23
|
|
|
23
|
1
|
1802
|
my $class = shift; |
37
|
23
|
|
|
|
|
34
|
my $hash = shift; |
38
|
23
|
|
50
|
|
|
54
|
my $brain = shift || die "no brain supplied"; |
39
|
23
|
|
|
|
|
99
|
my $obj = $class->new(_brain => $brain); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# deal with each type of field |
42
|
23
|
|
|
|
|
160
|
foreach my $type (keys %{ $class->fields }) { |
|
23
|
|
|
|
|
83
|
|
43
|
177
|
|
|
|
|
241
|
my @fields_of_this_type = @{ $class->fields->{$type} }; |
|
177
|
|
|
|
|
340
|
|
44
|
|
|
|
|
|
|
|
45
|
177
|
|
|
|
|
326
|
foreach my $field (@fields_of_this_type) { |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# ignore fields for which we have no value in the hash |
48
|
435
|
100
|
|
|
|
1282
|
next if (! defined $hash->{$field} ); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# warn "type: $type field $field\n"; |
51
|
86
|
100
|
|
|
|
150
|
if ($type eq 'scalar') { |
52
|
72
|
50
|
|
|
|
131
|
if ($obj->_field_is_array($field)) { |
|
|
50
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# simple scalar array ref, so just copy it |
54
|
0
|
|
|
|
|
0
|
my $val = $hash->{$field}; |
55
|
|
|
|
|
|
|
# deal with boolean stuff so we don't pollute our object |
56
|
|
|
|
|
|
|
# with JSON |
57
|
0
|
0
|
|
|
|
0
|
if (ref($val) eq 'JSON::PP::Boolean') { |
58
|
0
|
|
|
|
|
0
|
$val = !!$val; |
59
|
|
|
|
|
|
|
} |
60
|
0
|
|
|
|
|
0
|
$obj->$field($val); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
elsif ($obj->_field_is_array_of_arrays) { |
63
|
0
|
|
|
|
|
0
|
die "not yet implemented for scalars"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
72
|
|
|
|
|
110
|
my $val = $hash->{$field}; |
67
|
72
|
50
|
|
|
|
129
|
if (ref($val) eq 'JSON::PP::Boolean') { |
68
|
0
|
|
|
|
|
0
|
$val = 0+$val; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} |
71
|
72
|
|
|
|
|
210
|
$obj->$field($val); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
else { |
76
|
14
|
100
|
|
|
|
43
|
if ($obj->_field_is_array($field)) { |
|
|
50
|
|
|
|
|
|
77
|
1
|
|
|
|
|
2
|
my @sub_array; |
78
|
1
|
|
|
|
|
2
|
foreach my $data ( @{ $hash->{$field} } ) { |
|
1
|
|
|
|
|
3
|
|
79
|
3
|
|
|
|
|
11
|
push @sub_array, $type->create_from_hash($data, $brain); |
80
|
|
|
|
|
|
|
} |
81
|
1
|
|
|
|
|
3
|
$obj->$field(\@sub_array); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
elsif ($obj->_field_is_array_of_arrays) { |
84
|
0
|
|
|
|
|
0
|
die "not yet implemented for scalars"; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
13
|
|
|
|
|
104
|
$obj->$field($type->create_from_hash($hash->{$field}, $brain)); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
23
|
|
|
|
|
137
|
return $obj; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub as_hashref { |
99
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
100
|
0
|
|
|
|
|
|
my $hash = {}; |
101
|
|
|
|
|
|
|
# add the simple scalar values |
102
|
0
|
|
|
|
|
|
foreach my $type ( keys %{ $self->fields }) { |
|
0
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my @fields = @{ $self->fields->{$type} }; |
|
0
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
foreach my $field (@fields) { |
105
|
0
|
0
|
|
|
|
|
if ($type eq 'scalar') { |
106
|
0
|
|
|
|
|
|
$hash->{$field} = $self->$field; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
|
|
|
|
|
|
# non array types |
110
|
0
|
0
|
0
|
|
|
|
$hash->{$field} = $self->$field->as_hashref |
111
|
|
|
|
|
|
|
if (ref($self->$field) ne 'ARRAY' && defined $self->$field); |
112
|
|
|
|
|
|
|
# array types |
113
|
|
|
|
|
|
|
$hash->{$field} = [ |
114
|
0
|
0
|
|
|
|
|
map { $_->as_hashref } @{ $self->$field } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
] if (ref($self->$field) eq 'ARRAY'); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
0
|
|
|
|
|
|
return $hash; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
1; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
__END__ |