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