| 1 | #======================================================================== | ||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||
| 3 | # Badger::Hub | ||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 6 | # A hub provides a central configuration and management point for | ||||||||||||||||||||||||||||||||||||
| 7 | # Badger components to access other Badger components. | ||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||
| 9 | # AUTHOR | ||||||||||||||||||||||||||||||||||||
| 10 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||
| 12 | #======================================================================== | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | package Badger::Hub; | ||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | 70 | ▶ | use Badger::Debug ':dump'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | 70 | ▶ | use Badger::Config; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | use Badger::Class | ||||||||||||||||||||||||||||||||||||
| 19 | 70 | version => 0.01, | |||||||||||||||||||||||||||||||||||
| 20 | debug => 0, | ||||||||||||||||||||||||||||||||||||
| 21 | base => 'Badger::Prototype', | ||||||||||||||||||||||||||||||||||||
| 22 | import => 'class', | ||||||||||||||||||||||||||||||||||||
| 23 | auto_can => 'auto_can', | ||||||||||||||||||||||||||||||||||||
| 24 | constants => 'HASH ARRAY REFS PKG', | ||||||||||||||||||||||||||||||||||||
| 25 | utils => 'blessed params is_object', | ||||||||||||||||||||||||||||||||||||
| 26 | words => 'COMPONENTS DELEGATES COMP_CACHE DELG_CACHE', | ||||||||||||||||||||||||||||||||||||
| 27 | constant => { | ||||||||||||||||||||||||||||||||||||
| 28 | CONFIG_MODULE => 'Badger::Config', | ||||||||||||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||||||||||
| 30 | messages => { | ||||||||||||||||||||||||||||||||||||
| 31 | no_module => 'No %s module defined.', | ||||||||||||||||||||||||||||||||||||
| 32 | bad_method => "Invalid method '%s' called on %s at %s line %s", | ||||||||||||||||||||||||||||||||||||
| 33 | 70 | ▶ | }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 70 | our $COMPONENTS = { }; | |||||||||||||||||||||||||||||||||||
| 36 | 70 | our $RESOURCES = { }; | |||||||||||||||||||||||||||||||||||
| 37 | 70 | our $DELEGATES = { }; | |||||||||||||||||||||||||||||||||||
| 38 | 70 | our $LOADED = { }; | |||||||||||||||||||||||||||||||||||
| 39 | #our $COLLECTIONS = [qw( components resources delegates )]; | ||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | sub init { | ||||||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | my ($self, $config) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 44 | 4 | $self->init_hub($config); | |||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | sub init_hub { | ||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | my ($self, $config) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_hub: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 49 | 4 | $self->init_config($config); | |||||||||||||||||||||||||||||||||||
| 50 | 4 | $self->init_items($config); | |||||||||||||||||||||||||||||||||||
| 51 | # $self->init_collections($config); # init on demand | ||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | sub init_config { | ||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_config: called CC=8 SCAR=22.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 56 | 4 | my $class = $self->class; | |||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | # We're looking for a specific 'config' item which the user can provide to | ||||||||||||||||||||||||||||||||||||
| 59 | # points to a master configuration object or class name. We default to the | ||||||||||||||||||||||||||||||||||||
| 60 | # value in the $CONFIG package variable, which in this case is Badger::Config, | ||||||||||||||||||||||||||||||||||||
| 61 | # but could be re-defined by a subclass to be something else. | ||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | my $config = delete($args->{ config }) | ||||||||||||||||||||||||||||||||||||
| 64 | 4 | ▶ | || $self->class->any_var('CONFIG') | ||||||||||||||||||||||||||||||||||
|
Condition80%4/5
delete $args->{'config'} || $self->class->any_var('CONFIG') || {}
delete $args->{"config"} or $self->class->any_var("CONFIG")
MC/DC50%1/2
delete $args->{'config'} || $self->class->any_var('CONFIG') || {}
delete $args->{"config"}
$self->class->any_var("CONFIG")
Truth table66%2/3
A or B
A= delete $args->{"config"}B= $self->class->any_var("CONFIG")
| |||||||||||||||||||||||||||||||||||||
| 65 | || { }; | ||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | my $module = delete($args->{ config_module }) | ||||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | || $class->any_var('CONFIG_MODULE') | ||||||||||||||||||||||||||||||||||
|
Condition33%2/6
delete $args->{'config_module'} || $class->any_var('CONFIG_MODULE') || $self->CONFIG_MODULE
delete $args->{"config_module"} or $class->any_var("CONFIG_MODULE")
MC/DC0%0/3
delete $args->{'config_module'} || $class->any_var('CONFIG_MODULE') || $self->CONFIG_MODULE
delete $args->{"config_module"}
$class->any_var("CONFIG_MODULE")
$self->CONFIG_MODULE
Truth table25%1/4
A or B || C
A= delete $args->{"config_module"}B= $class->any_var("CONFIG_MODULE")C= $self->CONFIG_MODULE
| |||||||||||||||||||||||||||||||||||||
| 69 | || $self->CONFIG_MODULE; | ||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if (is_object($module, $config)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | # $config is already a config module object of the right heritage | ||||||||||||||||||||||||||||||||||||
| 73 | 0 | $self->debug("config is already a $module object") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||||||||||
| 76 | 4 | ▶ | if (ref $config eq HASH) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 77 | # cool | ||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||
| 79 | elsif (! ref $config) { | ||||||||||||||||||||||||||||||||||||
| 80 | # $config is a module name | ||||||||||||||||||||||||||||||||||||
| 81 | 1 | $module = $config; | |||||||||||||||||||||||||||||||||||
| 82 | 1 | $config = { }; | |||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||||||||||
| 85 | 0 | return $self->error_msg( invalid => config => $config ); | |||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||
| 87 | 4 | $self->debug("loading and instantiating a custom $module config object") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 88 | 4 | class($module)->load; | |||||||||||||||||||||||||||||||||||
| 89 | 4 | $config = $module->new($config); | |||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | 4 | $self->{ config } = $config; | |||||||||||||||||||||||||||||||||||
| 93 | 4 | $self->debug("hub config: $self->{ config }\n") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | sub init_items { | ||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_items: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | # merge all values in $CONFIG_ITEMS in with $args->{ items }; | ||||||||||||||||||||||||||||||||||||
| 100 | $args->{ items } = $self->class->list_vars( | ||||||||||||||||||||||||||||||||||||
| 101 | CONFIG_ITEMS => delete($args->{ config_items }), $args->{ items } | ||||||||||||||||||||||||||||||||||||
| 102 | 4 | ); | |||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||
| 104 | $self->debug( | ||||||||||||||||||||||||||||||||||||
| 105 | "hub config items: ", | ||||||||||||||||||||||||||||||||||||
| 106 | $self->dump_data($args->{ items }) | ||||||||||||||||||||||||||||||||||||
| 107 | 4 | ) if DEBUG; | |||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | 4 | return $self; | |||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||
| 112 | sub init_collections { | ||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($self, $config) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_collections: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 114 | 0 | $self->init_components($config); | |||||||||||||||||||||||||||||||||||
| 115 | 0 | $self->init_resource($config); | |||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->init_delegates($config); | |||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||
| 119 | sub init_components { | ||||||||||||||||||||||||||||||||||||
| 120 | 2 | ▶ | shift->init_collection( components => @_ ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_components: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | sub init_resources { | ||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | shift->init_collection( resources => @_ ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_resources: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||
| 127 | sub init_delegates { | ||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | shift->init_collection( delegates => @_ ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_delegates: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | sub init_collection { | ||||||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | my ($self, $type) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
init_collection: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 133 | 3 | my $TYPE = uc $type; | |||||||||||||||||||||||||||||||||||
| 134 | 3 | my $cfg = $self->config($type); | |||||||||||||||||||||||||||||||||||
| 135 | 3 | my $bits = $self->{ $type } = $self->class->hash_vars( | |||||||||||||||||||||||||||||||||||
| 136 | $TYPE => $self->config($type) | ||||||||||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||||||||||
| 138 | 3 | $self->debug("init_collection: $type: ", $self->dump_data($bits)) if DEBUG; | |||||||||||||||||||||||||||||||||||
| 139 | 3 | return $bits; | |||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||
| 142 | sub configure { | ||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | my ($self, $config) = self_params(@_); | ||||||||||||||||||||||||||||||||||
|
Subroutine
configure: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 144 | 0 | my $item; | |||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($item = delete $config->{ components }) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 147 | 0 | $self->components($item); | |||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ($item = delete $config->{ resources }) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 150 | 0 | $self->resources($item); | |||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($item = delete $config->{ delegates }) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 153 | 0 | $self->delegates($item); | |||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| 158 | # comment | ||||||||||||||||||||||||||||||||||||
| 159 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||
| 161 | sub components { | ||||||||||||||||||||||||||||||||||||
| 162 | 2 | ▶ | my $self = shift->prototype; | ||||||||||||||||||||||||||||||||||
|
Subroutine
components: called CC=4 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 163 | my $comps = $self->{ components } | ||||||||||||||||||||||||||||||||||||
| 164 | 2 | ▶ | || $self->init_components; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'components'} || $self->init_components
MC/DC0%0/2
$self->{'components'} || $self->init_components
$self->{'components'}
$self->init_components
Truth table33%1/3
A || B
A= $self->{'components'}B= $self->init_components
| |||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | 2 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my $args = ref $_[0] eq HASH ? shift : { @_ }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 168 | 0 | @$comps{ keys %$args } = values %$args; | |||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | 2 | return $comps; | |||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | sub component { | ||||||||||||||||||||||||||||||||||||
| 176 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
component: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 177 | 2 | my $comps = $self->components; | |||||||||||||||||||||||||||||||||||
| 178 | 2 | $self->debug("components: ", $self->dump_data($comps)) if DEBUG; | |||||||||||||||||||||||||||||||||||
| 179 | return @_ | ||||||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | ? $comps->{ $_[0] } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 181 | : $comps; | ||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||
| 185 | sub delegates { | ||||||||||||||||||||||||||||||||||||
| 186 | 1 | ▶ | my $self = shift->prototype; | ||||||||||||||||||||||||||||||||||
|
Subroutine
delegates: called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 187 | 1 | my $class = $self->class; | |||||||||||||||||||||||||||||||||||
| 188 | my $delgs = $self->{ delegates } | ||||||||||||||||||||||||||||||||||||
| 189 | 1 | ▶ | || $self->init_delegates; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'delegates'} || $self->init_delegates
MC/DC0%0/2
$self->{'delegates'} || $self->init_delegates
$self->{'delegates'}
$self->init_delegates
Truth table33%1/3
A || B
A= $self->{'delegates'}B= $self->init_delegates
| |||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | 1 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | my $args = ref $_[0] eq HASH ? shift : { @_ }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 193 | 0 | @$delgs{ keys %$args } = values %$args; | |||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||
| 196 | 1 | return $delgs; | |||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||
| 200 | sub delegate { | ||||||||||||||||||||||||||||||||||||
| 201 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
delegate: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 202 | 1 | my $delegs = $self->delegates; | |||||||||||||||||||||||||||||||||||
| 203 | 1 | $self->debug("delegates: ", $self->dump_data($delegs)) if DEBUG; | |||||||||||||||||||||||||||||||||||
| 204 | return @_ | ||||||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | ? $delegs->{ $_[0] } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 206 | : $delegs; | ||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||
| 210 | sub auto_can { | ||||||||||||||||||||||||||||||||||||
| 211 | 2 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
auto_can: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 212 | 2 | my $target; | |||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||
| 214 | 2 | ▶ | if ($target = $self->component($name)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 215 | 1 | $self->debug("creating component method in $self for $name") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 216 | 1 | return $self->auto_component( $name => $target ); | |||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||
| 218 | elsif ($target = $self->delegate($name)) { | ||||||||||||||||||||||||||||||||||||
| 219 | 0 | $self->debug("creating delegate method in $self for $name") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 220 | 0 | return $self->auto_delegate( $name => $target ); | |||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||
| 222 | elsif (DEBUG) { | ||||||||||||||||||||||||||||||||||||
| 223 | $self->debug("no component or delegate found in $self for $name"); | ||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||
| 226 | 1 | return undef; | |||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||
| 230 | sub auto_component { | ||||||||||||||||||||||||||||||||||||
| 231 | 1 | ▶ | my ($self, $name, $comp) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
auto_component: called CC=3 SCAR=13.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 232 | 1 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||
| 234 | 1 | ▶ | $LOADED->{ $name } ||= class($comp)->load; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$Badger::Hub::LOADED->{$name} ||= Badger::Hub::class($comp)->load
MC/DC0%0/2
$Badger::Hub::LOADED->{$name} ||= Badger::Hub::class($comp)->load
$Badger::Hub::LOADED->{$name}
Badger::Hub::class($comp)->load
Truth table33%1/3
A ||= B
A= $Badger::Hub::LOADED->{$name}B= Badger::Hub::class($comp)->load
| |||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||
| 236 | return sub { | ||||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=19.4
| |||||||||||||||||||||||||||||||||||||
| 238 | 1 | ▶ | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_
ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||
| 239 | 1 | ▶ | $self = $self->prototype unless ref $self; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||
| 241 | 1 | ▶ | return $self->{ $name } | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{$name} ||= $self->construct($name, {%$args, "hub", $self, "module", $comp})
MC/DC0%0/2
$self->{$name} ||= $self->construct($name, {%$args, "hub", $self, "module", $comp})
$self->{$name}
$self->construct($name, {%$args, "hub", $self, "module", $comp})
Truth table33%1/3
A ||= B
A= $self->{$name}B= $self->construct($name, {%$args, "hub", $self, "module", $comp})
| |||||||||||||||||||||||||||||||||||||
| 242 | ||= $self->construct( | ||||||||||||||||||||||||||||||||||||
| 243 | $name => { | ||||||||||||||||||||||||||||||||||||
| 244 | # TODO: figure out what's going on here in terms of | ||||||||||||||||||||||||||||||||||||
| 245 | # possible combinations of configuration options | ||||||||||||||||||||||||||||||||||||
| 246 | %$args, | ||||||||||||||||||||||||||||||||||||
| 247 | hub => $self, | ||||||||||||||||||||||||||||||||||||
| 248 | module => $comp | ||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||
| 250 | ); | ||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||
| 252 | 1 | } | |||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | sub auto_delegate { | ||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | my ($self, $name, $deleg) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
auto_delegate: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||
| 259 | # foo => bar is mapped to $self->bar->foo | ||||||||||||||||||||||||||||||||||||
| 260 | # foo => [bar, baz] is mapped to $self->bar->baz | ||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my ($m1, $m2) = ref $deleg eq ARRAY ? @$deleg : ($deleg, $name); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | return $self->error("Cannot auto_delegate() a method to itself: $m1 -> $m2") | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 264 | if $m1 eq $m2; | ||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||
| 266 | return sub { | ||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | shift->$m1->$m2(@_); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 268 | 0 | }; | |||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||
| 273 | # Configure and create a sub-component identified by $name, using | ||||||||||||||||||||||||||||||||||||
| 274 | # any configuration items in $params and any values defined locally | ||||||||||||||||||||||||||||||||||||
| 275 | # in a configuration hash, object or class. | ||||||||||||||||||||||||||||||||||||
| 276 | # | ||||||||||||||||||||||||||||||||||||
| 277 | # The $self->{ config } can contain a hash array of configuration | ||||||||||||||||||||||||||||||||||||
| 278 | # items, or it can be a Badger::Config object or the class name of a | ||||||||||||||||||||||||||||||||||||
| 279 | # Badger::Config object. We look in the hash, or call the object/class | ||||||||||||||||||||||||||||||||||||
| 280 | # method to find $name (e.g. $hash->{ $name }, $object->$name, or | ||||||||||||||||||||||||||||||||||||
| 281 | # $class->name()). This is merged with $params. | ||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||
| 283 | sub construct { | ||||||||||||||||||||||||||||||||||||
| 284 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
construct: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 285 | 1 | my $name = shift; | |||||||||||||||||||||||||||||||||||
| 286 | 1 | my $config = $self->config($name, @_); | |||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||
| 288 | 1 | $self->debug("$name module config: ", $self->dump_data($config)) if DEBUG; | |||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||
| 290 | # see if a module name is specified in $args, config hash or use $pkgmod | ||||||||||||||||||||||||||||||||||||
| 291 | my $module = $config->{ module } | ||||||||||||||||||||||||||||||||||||
| 292 | 1 | ▶ | || return $self->error_msg( no_module => $name ); | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'module'} || (return $self->error_msg('no_module', $name))
MC/DC0%0/1
$config->{'module'} || (return $self->error_msg('no_module', $name))
$config->{'module'}
Truth table50%1/2
A
A= $config->{'module'}
| |||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||
| 294 | # load the module | ||||||||||||||||||||||||||||||||||||
| 295 | 1 | class($module)->load; | |||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | 1 | return $module->new($config); | |||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||
| 300 | sub config { | ||||||||||||||||||||||||||||||||||||
| 301 | 7 | ▶ | my $self = shift->prototype; | ||||||||||||||||||||||||||||||||||
|
Subroutine
config: called CC=9 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 302 | 7 | ▶ | my $config = $self->{ config }; return $config unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 303 | 7 | my $name = shift; | |||||||||||||||||||||||||||||||||||
| 304 | 7 | my $params = params(@_); | |||||||||||||||||||||||||||||||||||
| 305 | 7 | my $defaults; | |||||||||||||||||||||||||||||||||||
| 306 | my $method; | ||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||
| 309 | 7 | ▶ | if ($config && ref $config eq HASH) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$config and ref $config eq "HASH"
MC/DC0%0/2
$config and ref $config eq "HASH"
$config
ref $config eq "HASH"
Truth table33%1/3
A and B
A= $configB= ref $config eq "HASH"
| |||||||||||||||||||||||||||||||||||||
| 310 | # $self->{ config } can be a hash ref with a $name item | ||||||||||||||||||||||||||||||||||||
| 311 | 0 | $defaults = $config->{ $name }; | |||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||
| 313 | ▶ | elsif (blessed $config && ($method = $config->can('get'))) { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($config) and $method = $config->can("get")
MC/DC0%0/2
builtin::blessed($config) and $method = $config->can("get")
builtin::blessed($config)
$method = $config->can("get")
Truth table33%1/3
A and B
A= builtin::blessed($config)B= $method = $config->can("get")
| |||||||||||||||||||||||||||||||||||||
| 314 | # $self->{ config } can be an object with a get($name) method which we call | ||||||||||||||||||||||||||||||||||||
| 315 | 7 | $self->debug("calling config->get($name)") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 316 | 7 | $defaults = $method->($config, $name); | |||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||
| 318 | ▶ | elsif (blessed $config && ($method = $config->can($name))) { | |||||||||||||||||||||||||||||||||||
|
Condition0%0/3
builtin::blessed($config) and $method = $config->can($name)
MC/DC0%0/2
builtin::blessed($config) and $method = $config->can($name)
builtin::blessed($config)
$method = $config->can($name)
Truth table0%0/3
A and B
A= builtin::blessed($config)B= $method = $config->can($name)
| |||||||||||||||||||||||||||||||||||||
| 319 | # $self->{ config } can be an object with a $name method which we call | ||||||||||||||||||||||||||||||||||||
| 320 | 0 | $self->debug("calling config->$name") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 321 | 0 | $defaults = $method->($config); | |||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||
| 323 | # no defaults | ||||||||||||||||||||||||||||||||||||
| 324 | 7 | ▶ | return $self->no_config($name, $params) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 325 | unless $defaults; | ||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||
| 327 | 0 | $self->debug( | |||||||||||||||||||||||||||||||||||
| 328 | "config for $name: DEFAULTS: ", | ||||||||||||||||||||||||||||||||||||
| 329 | $self->dump_data($defaults), | ||||||||||||||||||||||||||||||||||||
| 330 | "\nPARAMS: ", $self->dump_data($params) | ||||||||||||||||||||||||||||||||||||
| 331 | ) if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||
| 333 | return { | ||||||||||||||||||||||||||||||||||||
| 334 | 0 | %$defaults, | |||||||||||||||||||||||||||||||||||
| 335 | %$params | ||||||||||||||||||||||||||||||||||||
| 336 | }; | ||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||
| 339 | sub no_config { | ||||||||||||||||||||||||||||||||||||
| 340 | 7 | ▶ | my ($self, $name, $params) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
no_config: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||
| 342 | 7 | $self->debug( | |||||||||||||||||||||||||||||||||||
| 343 | "no_config for $name: ", | ||||||||||||||||||||||||||||||||||||
| 344 | "PARAMS: ", $self->dump_data($params) | ||||||||||||||||||||||||||||||||||||
| 345 | ) if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||
| 347 | # TODO: option to make this a failure | ||||||||||||||||||||||||||||||||||||
| 348 | 7 | ▶ | return $self->pkgvar_config($name, $params) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->pkgvar_config($name, $params) || ($params ? {%$params} : {})
MC/DC0%0/2
$self->pkgvar_config($name, $params) || ($params ? {%$params} : {})
$self->pkgvar_config($name, $params)
($params ? {%$params} : {})
Truth table33%1/3
A || B
A= $self->pkgvar_config($name, $params)B= ($params ? {%$params} : {})
| |||||||||||||||||||||||||||||||||||||
| 349 | || ($params ? { %$params } : { }); | ||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||
| 352 | sub pkgvar_config { | ||||||||||||||||||||||||||||||||||||
| 353 | 7 | ▶ | my ($self, $name, $params) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
pkgvar_config: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 354 | 7 | ▶ | my $pkgvar = $self->class->any_var(uc $name) || return $params; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->class->any_var(uc $name) || (return $params)
MC/DC100%1/1
$self->class->any_var(uc $name) || (return $params)
$self->class->any_var(uc $name)
Truth table100%2/2
A
A= $self->class->any_var(uc $name)
| |||||||||||||||||||||||||||||||||||||
| 355 | 6 | my $config; | |||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||
| 357 | 6 | ▶ | if (ref $pkgvar eq HASH) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 358 | # package variable can be a hash ref of config options | ||||||||||||||||||||||||||||||||||||
| 359 | 6 | $config = $pkgvar; | |||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||
| 361 | else { | ||||||||||||||||||||||||||||||||||||
| 362 | # or the name of a module | ||||||||||||||||||||||||||||||||||||
| 363 | 0 | $config = { | |||||||||||||||||||||||||||||||||||
| 364 | module => $pkgvar | ||||||||||||||||||||||||||||||||||||
| 365 | }; | ||||||||||||||||||||||||||||||||||||
| 366 | }; | ||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||
| 368 | return { | ||||||||||||||||||||||||||||||||||||
| 369 | 6 | %$config, | |||||||||||||||||||||||||||||||||||
| 370 | %$params | ||||||||||||||||||||||||||||||||||||
| 371 | }; | ||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||
| 376 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||
| 377 | # destroy() | ||||||||||||||||||||||||||||||||||||
| 378 | # | ||||||||||||||||||||||||||||||||||||
| 379 | # Destroy the hub and cleanup any cache items we may have stored. | ||||||||||||||||||||||||||||||||||||
| 380 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||
| 382 | sub destroy { | ||||||||||||||||||||||||||||||||||||
| 383 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
destroy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||
| 385 | # if called as a class method we cleanup any prototype object | ||||||||||||||||||||||||||||||||||||
| 386 | # stored as a singleton in the $PROTOTYPE package variable | ||||||||||||||||||||||||||||||||||||
| 387 | 3 | ▶ | unless (ref $self) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 388 | 70 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 389 | 1 | my $class = $self; | |||||||||||||||||||||||||||||||||||
| 390 | 1 | ▶ | $self = ${"$class\::PROTOTYPE"} || return; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
${"${class}::PROTOTYPE";} || (return)
MC/DC0%0/1
${"${class}::PROTOTYPE";} || (return)
${"${class}::PROTOTYPE";}
Truth table50%1/2
A
A= ${"${class}::PROTOTYPE";}
| |||||||||||||||||||||||||||||||||||||
| 391 | 1 | $self->debug("deleting hub prototype from \$$class\::PROTOTYPE\n") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 392 | 1 | ${"$class\::PROTOTYPE"} = undef; | |||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||
| 395 | 3 | $self->debug("destroying hub: $self\n") if DEBUG; | |||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||
| 397 | # empty content of $self to break any circular references that | ||||||||||||||||||||||||||||||||||||
| 398 | # we may have established with other items that point back to us | ||||||||||||||||||||||||||||||||||||
| 399 | 3 | %$self = (); | |||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||
| 403 | sub DESTROY { | ||||||||||||||||||||||||||||||||||||
| 404 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 405 | 2 | $self->destroy; | |||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||
| 409 | 70 | 1; | |||||||||||||||||||||||||||||||||||
| 410 | __END__ | ||||||||||||||||||||||||||||||||||||