| 1 | package Reddit::Client::Thing; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 5 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 5 | ▶ | use List::Util qw/first/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | # If a field is used by another class it must also be added to that class's | ||||||||||||||
| 10 | # fields | ||||||||||||||
| 11 | # Why does adding a field that exists in link but not comment (can_mod_post) | ||||||||||||||
| 12 | # break comments but not other types of things, like profiles? | ||||||||||||||
| 13 | # because can_mod_post DOES exist for comments. this would get to that field, | ||||||||||||||
| 14 | # which was previously ignored, but now it's in BOOL here, so it would try to | ||||||||||||||
| 15 | # set it, but the field didn't exist in Comment, so it would fail. | ||||||||||||||
| 16 | 5 | our @BOOL_FIELDS = qw/ | |||||||||||||
| 17 | can_mod_post | ||||||||||||||
| 18 | clicked | ||||||||||||||
| 19 | has_mail | ||||||||||||||
| 20 | has_mod_mail | ||||||||||||||
| 21 | has_verified_email | ||||||||||||||
| 22 | hidden | ||||||||||||||
| 23 | is_employee | ||||||||||||||
| 24 | is_gold | ||||||||||||||
| 25 | is_mod | ||||||||||||||
| 26 | is_original_content | ||||||||||||||
| 27 | is_self | ||||||||||||||
| 28 | likes | ||||||||||||||
| 29 | locked | ||||||||||||||
| 30 | new | ||||||||||||||
| 31 | over18 | ||||||||||||||
| 32 | over_18 | ||||||||||||||
| 33 | public_traffic | ||||||||||||||
| 34 | quarantine | ||||||||||||||
| 35 | removed | ||||||||||||||
| 36 | saved | ||||||||||||||
| 37 | spam | ||||||||||||||
| 38 | spoiler | ||||||||||||||
| 39 | stickied | ||||||||||||||
| 40 | verified | ||||||||||||||
| 41 | was_comment | ||||||||||||||
| 42 | |||||||||||||||
| 43 | isAuto | ||||||||||||||
| 44 | isHighlighted | ||||||||||||||
| 45 | isInternal | ||||||||||||||
| 46 | isRepliable | ||||||||||||||
| 47 | /; | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 5 | ▶ | use fields qw/session name id/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 52 | # $reddit is a Reddit::Client object | ||||||||||||||
| 53 | 0 | my ($class, $reddit, $source_data) = @_; | |||||||||||||
| 54 | 0 | my $self = fields::new($class); | |||||||||||||
| 55 | 0 | $self->{session} = $reddit; # could this be called something more sensible | |||||||||||||
| 56 | 0 | ▶ | $self->load_from_source_data($source_data) if $source_data; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 0 | return $self; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | ▶ | sub load_from_source_data { | ||||||||||||
|
Subroutine
load_from_source_data: not called CC=7 SCAR=37.7
Pod
documented
| |||||||||||||||
| 61 | 0 | require Reddit::Client; | |||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | my ($self, $source_data) = @_; | |||||||||||||
| 64 | 0 | ▶ | if ($source_data) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | # Hack that allows us to set link_id reliably when creating a MoreCommen | ||||||||||||||
| 66 | # object from Comment. This ensures that link_id is always set before | ||||||||||||||
| 67 | # replies. | ||||||||||||||
| 68 | #foreach my $field (keys %$source_data) { | ||||||||||||||
| 69 | 0 | foreach my $field (sort keys %$source_data) { | |||||||||||||
| 70 | # Set data fields | ||||||||||||||
| 71 | 0 | my $setter = sprintf 'set_%s', $field; | |||||||||||||
| 72 | 0 | ▶ | if ($self->can($setter)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 73 | 0 | $self->can($setter)->($self, $source_data->{$field}); | |||||||||||||
| 74 | 0 | ▶ | } elsif (first {$_ eq $field} @BOOL_FIELDS) { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 75 | 0 | $self->set_bool($field, $source_data->{$field}); | |||||||||||||
| 76 | } else { | ||||||||||||||
| 77 | 0 | eval { $self->{$field} = $source_data->{$field} }; | |||||||||||||
| 78 | 0 | ▶ | Reddit::Client::DEBUG("Field %s is missing from package %s\n", $field, ref $self) if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | # Add getter for field | ||||||||||||||
| 82 | 0 | ▶ | my $getter = sub { $_[0]->{$field} }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 83 | 0 | my $class = ref $self; | |||||||||||||
| 84 | 0 | my $method = sprintf '%s::get_%s', $class, $field; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | ▶ | unless ($self->can($method)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 5 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 88 | 0 | *{$method} = \&$getter; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | ▶ | sub set_bool { | ||||||||||||
|
Subroutine
set_bool: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 95 | 0 | my ($self, $field, $value) = @_; | |||||||||||||
| 96 | 0 | ▶ | $self->{$field} = $value ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 5 | 1; | |||||||||||||
| 100 | |||||||||||||||
| 101 | __END__ | ||||||||||||||