| 1 | package Telegram::Bot::Object::Base; | ||||||||||||||
| 2 | 5 | $Telegram::Bot::Object::Base::VERSION = '0.023'; | |||||||||||||
| 3 | # ABSTRACT: The base class for all Telegram::Bot::Object objects. | ||||||||||||||
| 4 | |||||||||||||||
| 5 | |||||||||||||||
| 6 | 5 | ▶ | use Mojo::Base -base; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 5 | has '_brain'; # a reference to our brain | |||||||||||||
| 9 | |||||||||||||||
| 10 | |||||||||||||||
| 11 | 56 | ▶ | sub arrays { qw// } # override if needed | ||||||||||||
|
Subroutine
arrays: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 12 | sub _field_is_array { | ||||||||||||||
| 13 | 91 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_field_is_array: called CC=2 SCAR=6.9
| |||||||||||||||
| 14 | 91 | my $field = shift; | |||||||||||||
| 15 | 91 | ▶ | if (grep /^$field$/, $self->arrays) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 16 | 1 | return 1; | |||||||||||||
| 17 | } | ||||||||||||||
| 18 | 90 | return; | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | |||||||||||||||
| 22 | 90 | ▶ | sub array_of_arrays { qw// } #override if needed | ||||||||||||
|
Subroutine
array_of_arrays: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 23 | sub _field_is_array_of_arrays { | ||||||||||||||
| 24 | 90 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_field_is_array_of_arrays: called CC=2 SCAR=7.4
| |||||||||||||||
| 25 | 90 | my $field = shift; | |||||||||||||
| 26 | 90 | ▶ | if (grep /^$field$/, $self->array_of_arrays) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | 0 | return 1; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | 90 | 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 | 25 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
create_from_hash: called CC=13 SCAR=28.4
Pod
documented
| |||||||||||||||
| 37 | 25 | my $hash = shift; | |||||||||||||
| 38 | 25 | ▶ | my $brain = shift || die "no brain supplied"; | ||||||||||||
|
Condition50%1/2
shift() || die('no brain supplied')
MC/DC0%0/1
shift() || die('no brain supplied')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||
| 39 | 25 | my $obj = $class->new(_brain => $brain); | |||||||||||||
| 40 | |||||||||||||||
| 41 | # deal with each type of field | ||||||||||||||
| 42 | 25 | foreach my $type (keys %{ $class->fields }) { | |||||||||||||
| 43 | 208 | my @fields_of_this_type = @{ $class->fields->{$type} }; | |||||||||||||
| 44 | |||||||||||||||
| 45 | 208 | foreach my $field (@fields_of_this_type) { | |||||||||||||
| 46 | |||||||||||||||
| 47 | # ignore fields for which we have no value in the hash | ||||||||||||||
| 48 | 496 | ▶ | next if (! defined $hash->{$field} ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | 91 | ▶ | if ($type eq 'scalar') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 76 | ▶ | if ($obj->_field_is_array($field)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 52 | # simple scalar array ref, so just copy it | ||||||||||||||
| 53 | 0 | my $val = $hash->{$field}; | |||||||||||||
| 54 | # deal with boolean stuff so we don't pollute our object | ||||||||||||||
| 55 | # with JSON | ||||||||||||||
| 56 | 0 | ▶ | if (ref($val) eq 'JSON::PP::Boolean') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 0 | $val = !!$val; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | 0 | $obj->$field($val); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | elsif ($obj->_field_is_array_of_arrays) { | ||||||||||||||
| 62 | 0 | die "not yet implemented for scalars"; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | else { | ||||||||||||||
| 65 | 76 | my $val = $hash->{$field}; | |||||||||||||
| 66 | 76 | ▶ | if (ref($val) eq 'JSON::PP::Boolean') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 0 | $val = 0+$val; | |||||||||||||
| 68 | |||||||||||||||
| 69 | } | ||||||||||||||
| 70 | 76 | $obj->$field($val); | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | else { | ||||||||||||||
| 75 | 15 | ▶ | if ($obj->_field_is_array($field)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 76 | 1 | my @sub_array; | |||||||||||||
| 77 | 1 | foreach my $data ( @{ $hash->{$field} } ) { | |||||||||||||
| 78 | 3 | push @sub_array, $type->create_from_hash($data, $brain); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | 1 | $obj->$field(\@sub_array); | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | elsif ($obj->_field_is_array_of_arrays($field)) { | ||||||||||||||
| 83 | 0 | die "not yet implemented for objects"; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | else { | ||||||||||||||
| 86 | 14 | $obj->$field($type->create_from_hash($hash->{$field}, $brain)); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 25 | return $obj; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | |||||||||||||||
| 97 | sub as_hashref { | ||||||||||||||
| 98 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
as_hashref: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||
| 99 | 0 | my $hash = {}; | |||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | foreach my $type ( keys %{ $self->fields }) { | |||||||||||||
| 102 | 0 | my @fields = @{ $self->fields->{$type} }; | |||||||||||||
| 103 | 0 | foreach my $field (@fields) { | |||||||||||||
| 104 | |||||||||||||||
| 105 | # add the simple scalar values | ||||||||||||||
| 106 | 0 | ▶ | if ($type eq 'scalar') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | |||||||||||||||
| 108 | # TODO support scalar arrays? | ||||||||||||||
| 109 | 0 | ▶ | $hash->{$field} = $self->$field | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | if defined $self->$field; | ||||||||||||||
| 111 | } | ||||||||||||||
| 112 | else { | ||||||||||||||
| 113 | 0 | ▶ | if ($self->_field_is_array($field)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 114 | |||||||||||||||
| 115 | # skip if it's not defined, though I'm not sure if there are cases | ||||||||||||||
| 116 | # where we should be sending an empty array instead? | ||||||||||||||
| 117 | 0 | ▶ | next if (! defined $self->$field); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | |||||||||||||||
| 119 | $hash->{$field} = [ | ||||||||||||||
| 120 | 0 | map { $_->as_hashref } @{ $self->$field } | |||||||||||||
| 121 | ]; | ||||||||||||||
| 122 | } | ||||||||||||||
| 123 | elsif ($self->_field_is_array_of_arrays($field)) { | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 0 | ▶ | next if (! defined $self->$field); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | |||||||||||||||
| 127 | # lets not nest maps | ||||||||||||||
| 128 | 0 | my $a_of_a = []; | |||||||||||||
| 129 | 0 | foreach my $outer ( @{ $self->$field } ) { | |||||||||||||
| 130 | 0 | my $inner = [ map { $_->as_hashref } @$outer ]; | |||||||||||||
| 131 | 0 | push @$a_of_a, $inner; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | 0 | $hash->{$field} = $a_of_a; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | else { | ||||||||||||||
| 136 | |||||||||||||||
| 137 | # non array, non scalar | ||||||||||||||
| 138 | 0 | ▶ | if (defined $self->$field) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 0 | my $hashref = $self->$field->as_hashref; | |||||||||||||
| 140 | 0 | ▶ | $hash->{$field} = $hashref | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | unless ! $hashref; | ||||||||||||||
| 142 | } | ||||||||||||||
| 143 | } | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | } | ||||||||||||||
| 146 | } | ||||||||||||||
| 147 | 0 | return $hash; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | 5 | 1; | |||||||||||||
| 151 | |||||||||||||||
| 152 | __END__ | ||||||||||||||