| 1 | package Dancer::Config::Object; | ||||||||||||||
| 2 | 234 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||
| 3 | #ABSTRACT: Access the config via methods instead of hashrefs | ||||||||||||||
| 4 | 234 | $Dancer::Config::Object::VERSION = '1.3520'; | |||||||||||||
| 5 | 234 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 234 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 234 | ▶ | use base 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 234 | ▶ | use Carp 'croak'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 234 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 234 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 234 | register_exception('BadConfigMethod', | |||||||||||||
| 14 | message_pattern => | ||||||||||||||
| 15 | qq{Can't locate config attribute "%s".\nAvailable attributes: %s}); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 234 | our @EXPORT_OK = qw(hashref_to_object); | |||||||||||||
| 18 | |||||||||||||||
| 19 | { | ||||||||||||||
| 20 | 234 | my $index = 1; | |||||||||||||
| 21 | |||||||||||||||
| 22 | sub hashref_to_object { | ||||||||||||||
| 23 | 10 | ▶ | my ($hashref) = @_; | ||||||||||||
|
Subroutine
hashref_to_object: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||
| 24 | 10 | my $class = __PACKAGE__; | |||||||||||||
| 25 | 10 | my $target = "${class}::__ANON__$index"; | |||||||||||||
| 26 | 10 | $index++; | |||||||||||||
| 27 | 10 | ▶ | if ('HASH' ne ref $hashref) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 1 | ▶ | if ( blessed $hashref ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | # we have already converted this to an object. This can happen | ||||||||||||||
| 30 | # in cases where Dancer::Config->load is called more than | ||||||||||||||
| 31 | # once. | ||||||||||||||
| 32 | 1 | return $hashref; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | else { | ||||||||||||||
| 35 | # should never happen | ||||||||||||||
| 36 | 0 | raise 'Core::Config' => "Argument to $class must be a hashref"; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | } | ||||||||||||||
| 39 | 9 | my $object = bless $hashref => $target; | |||||||||||||
| 40 | 9 | _add_methods($object); | |||||||||||||
| 41 | |||||||||||||||
| 42 | 9 | return $object; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | |||||||||||||||
| 47 | 234 | sub _add_methods { | |||||||||||||
| 48 | 9 | ▶ | my ($object) = @_; | ||||||||||||
|
Subroutine
_add_methods: called CC=7 SCAR=19.5
| |||||||||||||||
| 49 | 9 | my $target = ref $object; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 9 | foreach my $key ( keys %$object ) { | |||||||||||||
| 52 | 35 | my $value = $object->{$key}; | |||||||||||||
| 53 | 35 | ▶ | if ( 'HASH' eq ref $value ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 54 | 6 | $value = hashref_to_object($value); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | elsif ( 'ARRAY' eq ref $value ) { | ||||||||||||||
| 57 | 1 | foreach (@$value) { | |||||||||||||
| 58 | 2 | ▶ | $_ = 'HASH' eq ref($_) ? hashref_to_object($_) : $_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | } | ||||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | # match a (more or less) valid identifier | ||||||||||||||
| 63 | 35 | ▶ | next unless $key =~ qr/^[[:alpha:]_][[:word:]]*$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 33 | my $method = "${target}::$key"; | |||||||||||||
| 65 | 234 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 66 | 33 | ▶ | *$method = sub {$value}; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 67 | } | ||||||||||||||
| 68 | 9 | _setup_bad_method_trap($target); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | # AUTOLOAD will only be called if a non-existent method is called. It's used | ||||||||||||||
| 72 | # to generate the list of available methods. It's slow, but we're going to | ||||||||||||||
| 73 | # die. Who wants to die quickly? | ||||||||||||||
| 74 | sub _setup_bad_method_trap { | ||||||||||||||
| 75 | 9 | ▶ | my ($target) = @_; | ||||||||||||
|
Subroutine
_setup_bad_method_trap: called CC=1 SCAR=0.0
| |||||||||||||||
| 76 | 234 | ▶ | no strict; ## no critic (ProhibitNoStrict) | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 77 | 9 | *{"${target}::AUTOLOAD"} = sub { | |||||||||||||
| 78 | 1 | ▶ | $AUTOLOAD =~ /.*::(.*)$/; | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | # should never happen | ||||||||||||||
| 81 | 1 | ▶ | my $bad_method = $1 ## no critic (ProhibitCaptureWithoutTest) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | or croak "Could not determine method called via $AUTOLOAD"; | ||||||||||||||
| 83 | 1 | ▶ | return if 'DESTROY' eq $bad_method; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 1 | my $symbol_table = "${target}::"; | |||||||||||||
| 85 | |||||||||||||||
| 86 | # In these fake classes, we only have methods | ||||||||||||||
| 87 | my $methods = | ||||||||||||||
| 88 | 1 | join ', ' => grep { !/^(?:AUTOLOAD|DESTROY|$bad_method)$/ } | |||||||||||||
| 89 | sort keys %$symbol_table; | ||||||||||||||
| 90 | 1 | raise BadConfigMethod => $bad_method, $methods; | |||||||||||||
| 91 | 9 | }; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 234 | 1; | |||||||||||||
| 95 | |||||||||||||||
| 96 | __END__ | ||||||||||||||