| 1 | package MooseX::Role::Registry; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | our $VERSION = '1.02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: MooseX::Role::Registry watches a file which describes a hashref of objects in yml format | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | MooseX::Role::Registry | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package Foo::Registry; | ||||||||||||||||||||||||||||
| 16 | use Moose; | ||||||||||||||||||||||||||||
| 17 | with 'MooseX::Role::Registry'; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub config_file { | ||||||||||||||||||||||||||||
| 20 | return '/foo_objects.yml'; | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub build_registry_object { | ||||||||||||||||||||||||||||
| 24 | my $self = shift; | ||||||||||||||||||||||||||||
| 25 | my $name = shift; | ||||||||||||||||||||||||||||
| 26 | my $values = shift || {}; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | return Foo->new({ | ||||||||||||||||||||||||||||
| 29 | name => $name, | ||||||||||||||||||||||||||||
| 30 | %$values | ||||||||||||||||||||||||||||
| 31 | }); | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | package main; | ||||||||||||||||||||||||||||
| 35 | my $registry = Foo::Registry->instance; | ||||||||||||||||||||||||||||
| 36 | my $foo = $registry->get('bar'); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | This role watches a file which describes a hashref of objects in yml format. | ||||||||||||||||||||||||||||
| 41 | This hashref is called a "registry" because the objects in the hashref can be | ||||||||||||||||||||||||||||
| 42 | requested by name using I<get>. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Implementations should be singletons! In other words, when using a class that is | ||||||||||||||||||||||||||||
| 45 | derived from MooseX::Role::Registry, you shouldn't call I<new>. Instead, | ||||||||||||||||||||||||||||
| 46 | just get the singelton object using the I<instance> method and call I<get> on | ||||||||||||||||||||||||||||
| 47 | the result. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | use YAML::XS qw(LoadFile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 REQUIRED SUBCLASS METHODS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 config_filename | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Returns the filesystem path of the default location of the configuration file | ||||||||||||||||||||||||||||
| 62 | that is watched by a given consumer of MooseX::Role::Registry | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | requires 'config_file'; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 build_registry_object | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | A function to create an object of the registy entry | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | requires 'build_registry_object'; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 METHODS | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 get($name) | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Returns the registered entity called $name, or undef if none exists. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | 2 | my $key = shift; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | return unless ($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | return $self->_registry->{$key}; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 all | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Returns all of the objects stored in the registry. Useful for generic grep() calls. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | sub all { | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 1 | return values %{$self->_registry}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head2 keys | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Returns a list of all of the (lookup) keys of objects currently registered in $self. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub keys ## no critic (ProhibitBuiltinHomonyms) | ||||||||||||||||||||||||||||
| 110 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 1 | my @result = sort { $a cmp $b } (keys %{$self->_registry}); | |||||||||||||||||||||||||||
| 113 | 1 | return @result; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 registry_fixup | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | A callback which allows subclasses to modify the hashref of loaded objects before | ||||||||||||||||||||||||||||
| 119 | they are stored in memory as part of I<$self>. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | sub registry_fixup { | ||||||||||||||||||||||||||
|
Subroutine
registry_fixup: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | 1 | my $registry = shift; | |||||||||||||||||||||||||||
| 126 | 1 | return $registry; # Default implementation is to leave the loaded hashref alone | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 4 | has _registry => ( | |||||||||||||||||||||||||||
| 130 | is => 'rw', | ||||||||||||||||||||||||||||
| 131 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 132 | lazy_build => 1 | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | has _db => ( | |||||||||||||||||||||||||||
| 136 | is => 'rw', | ||||||||||||||||||||||||||||
| 137 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 138 | lazy_build => 1 | ||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 2 | ▶ | sub _build__db { | ||||||||||||||||||||||||||
|
Subroutine
_build__db: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2 | return YAML::XS::LoadFile($self->config_file); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | ▶ | sub _build__registry { | ||||||||||||||||||||||||||
|
Subroutine
_build__registry: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 148 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 149 | 2 | my $registry = $self->_db; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # If we've made it this far we no longer need this key | ||||||||||||||||||||||||||||
| 152 | 2 | delete $registry->{version}; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | foreach my $key (CORE::keys %$registry) { | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # TOTALLY coding to the coverage tool here. This sucks. | ||||||||||||||||||||||||||||
| 157 | 4 | my $reg_defn = $registry->{$key}; | |||||||||||||||||||||||||||
| 158 | 4 | my $reg_defn_type = ref $reg_defn; | |||||||||||||||||||||||||||
| 159 | 4 | ▶ | if (not $reg_defn_type or ($reg_defn_type eq 'HASH')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $reg_defn_type or $reg_defn_type eq "HASH"
MC/DC50%1/2
not $reg_defn_type or $reg_defn_type eq "HASH"
not $reg_defn_type
$reg_defn_type eq "HASH"
Truth table66%2/3
not(A) or B
A= not $reg_defn_typeB= $reg_defn_type eq "HASH"
| |||||||||||||||||||||||||||||
| 160 | try { | ||||||||||||||||||||||||||||
| 161 | 3 | $registry->{$key} = $self->build_registry_object($key, $reg_defn); | |||||||||||||||||||||||||||
| 162 | 3 | } catch ($e) { | |||||||||||||||||||||||||||
| 163 | 0 | Carp::croak("Unable to convert entry $key in " . $self->config_file . " into a registry entry : $e"); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 1 | Carp::croak("Invalid entry $key in " . $self->config_file . ", not a hash"); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 1 | return $self->registry_fixup($registry); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =for Pod::Coverage BUILD | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | 2 | $self->_registry; | |||||||||||||||||||||||||||
| 180 | 1 | return; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 4 | 1; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | __END__ | ||||||||||||||||||||||||||||