| 1 | package Dancer::Object; | ||||||||||||
| 2 | 259 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||
| 3 | #ABSTRACT: Objects base class for Dancer | ||||||||||||
| 4 | 259 | $Dancer::Object::VERSION = '1.3520'; | |||||||||||
| 5 | # This class is a root class for each object in Dancer. | ||||||||||||
| 6 | # It provides basic OO tools for Perl5 without being... Moose ;-) | ||||||||||||
| 7 | |||||||||||||
| 8 | 259 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 259 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 259 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 259 | ▶ | use Dancer::Exception qw(:all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | # constructor | ||||||||||||
| 14 | sub new { | ||||||||||||
| 15 | 4852 | ▶ | my ($class, %args) = @_; | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 16 | 4852 | my $self = \%args; | |||||||||||
| 17 | 4852 | bless $self, $class; | |||||||||||
| 18 | 4852 | $self->init(%args); | |||||||||||
| 19 | 4849 | return $self; | |||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | sub clone { | ||||||||||||
| 23 | 1 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
clone: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 24 | 1 | ▶ | raise core => "The 'Clone' module is needed" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | unless Dancer::ModuleLoader->load('Clone'); | ||||||||||||
| 26 | 1 | return Clone::clone($self); | |||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | # initializer | ||||||||||||
| 30 | 146 | ▶ | sub init {1} | ||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 31 | |||||||||||||
| 32 | # meta information about classes | ||||||||||||
| 33 | 259 | my $_attrs_per_class = {}; | |||||||||||
| 34 | sub get_attributes { | ||||||||||||
| 35 | 379 | ▶ | my ($class, $visited_parents) = @_; | ||||||||||
|
Subroutine
get_attributes: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 36 | # $visited_parents keeps track of parent classes we already handled, to | ||||||||||||
| 37 | # avoid infinite recursion (in case of dependencies loop). It's not stored as class singleton, otherwise | ||||||||||||
| 38 | # get_attributes wouldn't be re-entrant. | ||||||||||||
| 39 | 379 | ▶ | $visited_parents ||= {}; | ||||||||||
|
Condition100%2/2
$visited_parents ||= {}
MC/DC100%1/1
$visited_parents ||= {}
$visited_parents
Truth table100%2/2
A
A= $visited_parents
| |||||||||||||
| 40 | 379 | ▶ | my @attributes = @{$_attrs_per_class->{$class} || [] }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 379 | my @parents; | |||||||||||
| 42 | 259 | ▶ | { no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 379 | @parents = @{"$class\::ISA"}; } | |||||||||||
| 44 | 379 | foreach my $parent (@parents) { | |||||||||||
| 45 | # cleanup $parent | ||||||||||||
| 46 | 191 | $parent =~ s/'/::/g; | |||||||||||
| 47 | 191 | ▶ | $parent =~ /^::/ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | and $parent = 'main' . $parent; | ||||||||||||
| 49 | |||||||||||||
| 50 | # check we didn't visited it already | ||||||||||||
| 51 | 191 | ▶ | $visited_parents->{$parent}++ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | and next; | ||||||||||||
| 53 | |||||||||||||
| 54 | # check it's a Dancer::Object | ||||||||||||
| 55 | 191 | ▶ | $parent->isa(__PACKAGE__) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | or next; | ||||||||||||
| 57 | |||||||||||||
| 58 | # merge parents attributes | ||||||||||||
| 59 | 191 | push @attributes, @{$parent->get_attributes($visited_parents)}; | |||||||||||
| 60 | } | ||||||||||||
| 61 | 379 | return \@attributes; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | # accessor code for normal objects | ||||||||||||
| 65 | # (overloaded in D::O::Singleton for instance) | ||||||||||||
| 66 | sub _setter_code { | ||||||||||||
| 67 | 12094 | ▶ | my ($class, $attr) = @_; | ||||||||||
|
Subroutine
_setter_code: called CC=1 SCAR=0.0
| |||||||||||||
| 68 | sub { | ||||||||||||
| 69 | 68672 | ▶ | my ($self, $value) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 70 | 68672 | ▶ | if (@_ == 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 61230 | return $self->{$attr}; | |||||||||||
| 72 | } | ||||||||||||
| 73 | else { | ||||||||||||
| 74 | 7442 | return $self->{$attr} = $value; | |||||||||||
| 75 | } | ||||||||||||
| 76 | 12094 | }; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | # accessors builder | ||||||||||||
| 80 | sub attributes { | ||||||||||||
| 81 | 3079 | ▶ | my ($class, @attributes) = @_; | ||||||||||
|
Subroutine
attributes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 82 | |||||||||||||
| 83 | # save meta information | ||||||||||||
| 84 | 3079 | $_attrs_per_class->{$class} = \@attributes; | |||||||||||
| 85 | |||||||||||||
| 86 | # define setters and getters for each attribute | ||||||||||||
| 87 | 3079 | foreach my $attr (@attributes) { | |||||||||||
| 88 | 13015 | my $code = $class->_setter_code($attr); | |||||||||||
| 89 | 13015 | my $method = "${class}::${attr}"; | |||||||||||
| 90 | 259 | ▶ | { no strict 'refs'; *$method = $code; } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 91 | } | ||||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | sub attributes_defaults { | ||||||||||||
| 95 | 1090 | ▶ | my ($self, %defaults) = @_; | ||||||||||
|
Subroutine
attributes_defaults: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 96 | 1090 | ▶ | while (my ($k, $v) = each %defaults) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 6320 | ▶ | exists $self->{$k} or $self->{$k} = $v; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | } | ||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 259 | 1; | |||||||||||
| 102 | |||||||||||||
| 103 | __END__ | ||||||||||||