| 1 | 1 | ▶ | use 5.008; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | package Property::Lookup; | ||||||||||||||
| 6 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | $Property::Lookup::VERSION = '1.101400'; | |||||||||||||
| 8 | } | ||||||||||||||
| 9 | # ABSTRACT: Object property lookup across multiple layers | ||||||||||||||
| 10 | 1 | ▶ | use Error::Hierarchy::Util qw/assert_defined load_class/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Property::Lookup::Local; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use Property::Lookup::Hash; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | # Don't rely on UNIVERSAL::throw if we defined an AUTOLOAD... | ||||||||||||||
| 15 | 1 | ▶ | use Error::Hierarchy::Internal::CustomMessage; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use parent qw(Class::Accessor::Complex Class::Accessor::Constructor); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | __PACKAGE__->mk_singleton_constructor(qw(new instance)) | |||||||||||||
| 18 | ->mk_array_accessors(qw(layers)) | ||||||||||||||
| 19 | ->mk_scalar_accessors(qw(local_layer default_layer)); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 0 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 22 | 0 | my $self = shift; | |||||||||||||
| 23 | 0 | $self->local_layer(Property::Lookup::Local->new); | |||||||||||||
| 24 | 0 | $self->default_layer(Property::Lookup::Hash->new); | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 0 | ▶ | sub add_layer { | ||||||||||||
|
Subroutine
add_layer: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 28 | 0 | my $self = shift; | |||||||||||||
| 29 | 0 | my $type = shift; | |||||||||||||
| 30 | 0 | assert_defined $type, 'missing configuration type'; | |||||||||||||
| 31 | 0 | ▶ | if ($type eq 'file') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 32 | 0 | my $spec = shift; | |||||||||||||
| 33 | 0 | assert_defined $spec, 'missing file configuration spec'; | |||||||||||||
| 34 | 0 | my ($class, $conf_filename); | |||||||||||||
| 35 | 0 | ▶ | if (index($spec, ';') != -1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 0 | ($class, $conf_filename) = split /;/ => $spec; | |||||||||||||
| 37 | assert_defined $_, | ||||||||||||||
| 38 | sprintf("can't determine file configuration class from spec [%s]", | ||||||||||||||
| 39 | $spec) | ||||||||||||||
| 40 | 0 | for $class, $conf_filename; | |||||||||||||
| 41 | } else { | ||||||||||||||
| 42 | |||||||||||||||
| 43 | # assume a default class, and the spec _is_ the conf file name | ||||||||||||||
| 44 | 0 | $class = 'Property::Lookup::File'; | |||||||||||||
| 45 | 0 | $conf_filename = $spec; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | 0 | load_class $class, 0; | |||||||||||||
| 48 | 0 | $self->layers_push($class->new(filename => $conf_filename)); | |||||||||||||
| 49 | } elsif ($type eq 'hash') { | ||||||||||||||
| 50 | 0 | my $options = shift; | |||||||||||||
| 51 | 0 | assert_defined $options, 'missing hash configuration spec'; | |||||||||||||
| 52 | 0 | $self->layers_push(Property::Lookup::Hash->new(hash => $options)); | |||||||||||||
| 53 | } else { | ||||||||||||||
| 54 | 0 | throw Error::Hierarchy::Internal::CustomMessage( | |||||||||||||
| 55 | custom_message => sprintf 'unknown configuration type [%s]', | ||||||||||||||
| 56 | $type | ||||||||||||||
| 57 | ); | ||||||||||||||
| 58 | } | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | sub get_layers { | ||||||||||||
|
Subroutine
get_layers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 62 | 0 | my $self = shift; | |||||||||||||
| 63 | 0 | ($self->local_layer, $self->layers, $self->default_layer) | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | # FIXME Provide a way to tell Property::Lookup about cumulative methods. | ||||||||||||||
| 67 | # get_config() differs from all other autoloaded methods in that the results | ||||||||||||||
| 68 | # are cumulative over the layers; for the other methods, the first layer that | ||||||||||||||
| 69 | # responds wins. So maybe the user of Property::Lookup should be able to | ||||||||||||||
| 70 | # specify which methods are supposed to be cumulative. | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | ▶ | sub get_config { | ||||||||||||
|
Subroutine
get_config: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 73 | 0 | my $self = shift; | |||||||||||||
| 74 | 0 | my %config; | |||||||||||||
| 75 | 0 | for my $layer ($self->get_layers) { | |||||||||||||
| 76 | 0 | %config = (%config, $layer->get_config); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | 0 | ▶ | wantarray ? %config : \%config; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | # Define functions and class methods lest they be handled by AUTOLOAD. | ||||||||||||||
| 82 | 0 | ▶ | sub DEFAULTS { () } | ||||||||||||
|
Subroutine
DEFAULTS: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 83 | 0 | ▶ | sub FIRST_CONSTRUCTOR_ARGS { () } | ||||||||||||
|
Subroutine
FIRST_CONSTRUCTOR_ARGS: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 84 | 0 | ▶ | sub DESTROY { } | ||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||
| 85 | |||||||||||||||
| 86 | # Ask every layer in turn; return the first defined answer we're given. | ||||||||||||||
| 87 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||
|
Subroutine
AUTOLOAD: not called CC=4 SCAR=30.0
| |||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||
| 89 | 0 | (my $method = our $AUTOLOAD) =~ s/.*://; | |||||||||||||
| 90 | 0 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 0 | throw Error::Hierarchy::Internal::CustomMessage( | |||||||||||||
| 92 | custom_message => sprintf 'configuration key [%s] is read-only', | ||||||||||||||
| 93 | $method | ||||||||||||||
| 94 | ); | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | for my $layer ($self->get_layers) { | |||||||||||||
| 98 | 0 | my $answer = $layer->$method; | |||||||||||||
| 99 | 0 | ▶ | return $answer if defined $answer; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | } | ||||||||||||||
| 101 | 0 | undef; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 1 | 1; | |||||||||||||
| 105 | |||||||||||||||
| 106 | |||||||||||||||
| 107 | __END__ | ||||||||||||||