| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | use strict; | 
| 2 | 3 |  |  | 3 |  | 2016 | use warnings; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 90 |  | 
| 3 | 3 |  |  | 3 |  | 14 |  | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 142 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = '1.02'; | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # ABSTRACT: MooseX::Role::Registry watches a file which describes a hashref of objects in yml format | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 NAME | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | MooseX::Role::Registry | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 SYNOPSYS | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | package Foo::Registry; | 
| 15 |  |  |  |  |  |  | use Moose; | 
| 16 |  |  |  |  |  |  | with 'MooseX::Role::Registry'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub config_file { | 
| 19 |  |  |  |  |  |  | return '/foo_objects.yml'; | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub build_registry_object { | 
| 23 |  |  |  |  |  |  | my $self   = shift; | 
| 24 |  |  |  |  |  |  | my $name   = shift; | 
| 25 |  |  |  |  |  |  | my $values = shift || {}; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | return Foo->new({ | 
| 28 |  |  |  |  |  |  | name                   => $name, | 
| 29 |  |  |  |  |  |  | %$values | 
| 30 |  |  |  |  |  |  | }); | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | package main; | 
| 34 |  |  |  |  |  |  | my $registry = Foo::Registry->instance; | 
| 35 |  |  |  |  |  |  | my $foo = $registry->get('bar'); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | This role watches a file which describes a hashref of objects in yml format. | 
| 40 |  |  |  |  |  |  | This hashref is called a "registry" because the objects in the hashref can be | 
| 41 |  |  |  |  |  |  | requested by name using I<get>. | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | Implementations should be singletons! In other words, when using a class that is | 
| 44 |  |  |  |  |  |  | derived from MooseX::Role::Registry, you shouldn't call I<new>. Instead, | 
| 45 |  |  |  |  |  |  | just get the singelton object using the I<instance> method and call I<get> on | 
| 46 |  |  |  |  |  |  | the result. | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =cut | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | use Moose::Role; | 
| 51 | 3 |  |  | 3 |  | 1250 | use namespace::autoclean; | 
|  | 3 |  |  |  |  | 483826 |  | 
|  | 3 |  |  |  |  | 12 |  | 
| 52 | 3 |  |  | 3 |  | 19331 | use Carp; | 
|  | 3 |  |  |  |  | 24146 |  | 
|  | 3 |  |  |  |  | 13 |  | 
| 53 | 3 |  |  | 3 |  | 247 | use Syntax::Keyword::Try; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 178 |  | 
| 54 | 3 |  |  | 3 |  | 1487 | use YAML::XS qw(LoadFile); | 
|  | 3 |  |  |  |  | 5784 |  | 
|  | 3 |  |  |  |  | 15 |  | 
| 55 | 3 |  |  | 3 |  | 1446 |  | 
|  | 3 |  |  |  |  | 8361 |  | 
|  | 3 |  |  |  |  | 1777 |  | 
| 56 |  |  |  |  |  |  | =head1 REQUIRED SUBCLASS METHODS | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =head2 config_filename | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | Returns the filesystem path of the default location of the configuration file | 
| 61 |  |  |  |  |  |  | that is watched by a given consumer of MooseX::Role::Registry | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | =cut | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | requires 'config_file'; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =head2 build_registry_object | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | A function to create an object of the registy entry | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =cut | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | requires 'build_registry_object'; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head1 METHODS | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =head2 get($name) | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | Returns the registered entity called $name, or undef if none exists. | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =cut | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | my $self = shift; | 
| 84 |  |  |  |  |  |  | my $key  = shift; | 
| 85 | 2 |  |  | 2 | 1 | 818 |  | 
| 86 | 2 |  |  |  |  | 3 | return unless ($key); | 
| 87 |  |  |  |  |  |  | return $self->_registry->{$key}; | 
| 88 | 2 | 100 |  |  |  | 9 | } | 
| 89 | 1 |  |  |  |  | 28 |  | 
| 90 |  |  |  |  |  |  | =head2 all | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Returns all of the objects stored in the registry. Useful for generic grep() calls. | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =cut | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | my $self = shift; | 
| 97 |  |  |  |  |  |  | return values %{$self->_registry}; | 
| 98 |  |  |  |  |  |  | } | 
| 99 | 1 |  |  | 1 | 1 | 650 |  | 
| 100 | 1 |  |  |  |  | 2 | =head2 keys | 
|  | 1 |  |  |  |  | 27 |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Returns a list of all of the (lookup) keys of objects currently registered in $self. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =cut | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | { | 
| 107 |  |  |  |  |  |  | my $self   = shift; | 
| 108 |  |  |  |  |  |  | my @result = sort { $a cmp $b } (keys %{$self->_registry}); | 
| 109 |  |  |  |  |  |  | return @result; | 
| 110 |  |  |  |  |  |  | } | 
| 111 | 1 |  |  | 1 | 1 | 784 |  | 
| 112 | 1 |  |  |  |  | 2 | =head2 registry_fixup | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 113 | 1 |  |  |  |  | 4 |  | 
| 114 |  |  |  |  |  |  | A callback which allows subclasses to modify the hashref of loaded objects before | 
| 115 |  |  |  |  |  |  | they are stored in memory as part of I<$self>. | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =cut | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | my $self     = shift; | 
| 120 |  |  |  |  |  |  | my $registry = shift; | 
| 121 |  |  |  |  |  |  | return $registry;    # Default implementation is to leave the loaded hashref alone | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 1 |  |  | 1 | 1 | 2 | has _registry => ( | 
| 125 | 1 |  |  |  |  | 2 | is         => 'rw', | 
| 126 | 1 |  |  |  |  | 22 | isa        => 'HashRef', | 
| 127 |  |  |  |  |  |  | lazy_build => 1 | 
| 128 |  |  |  |  |  |  | ); | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | has _db => ( | 
| 131 |  |  |  |  |  |  | is         => 'rw', | 
| 132 |  |  |  |  |  |  | isa        => 'HashRef', | 
| 133 |  |  |  |  |  |  | lazy_build => 1 | 
| 134 |  |  |  |  |  |  | ); | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | my $self = shift; | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | return YAML::XS::LoadFile($self->config_file); | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | my $self     = shift; | 
| 142 | 2 |  |  | 2 |  | 3 | my $registry = $self->_db; | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 2 |  |  |  |  | 8 | # If we've made it this far we no longer need this key | 
| 145 |  |  |  |  |  |  | delete $registry->{version}; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | foreach my $key (CORE::keys %$registry) { | 
| 148 | 2 |  |  | 2 |  | 4 |  | 
| 149 | 2 |  |  |  |  | 54 | # TOTALLY coding to the coverage tool here. This sucks. | 
| 150 |  |  |  |  |  |  | my $reg_defn      = $registry->{$key}; | 
| 151 |  |  |  |  |  |  | my $reg_defn_type = ref $reg_defn; | 
| 152 | 2 |  |  |  |  | 4 | if (not $reg_defn_type or ($reg_defn_type eq 'HASH')) { | 
| 153 |  |  |  |  |  |  | try { | 
| 154 | 2 |  |  |  |  | 9 | $registry->{$key} = $self->build_registry_object($key, $reg_defn); | 
| 155 |  |  |  |  |  |  | } catch ($e) { | 
| 156 |  |  |  |  |  |  | Carp::croak("Unable to convert entry $key in " . $self->config_file . " into a registry entry : $e"); | 
| 157 | 4 |  |  |  |  | 12 | } | 
| 158 | 4 |  |  |  |  | 7 | } else { | 
| 159 | 4 | 100 | 66 |  |  | 20 | Carp::croak("Invalid entry $key in " . $self->config_file . ", not a hash"); | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  | } | 
| 162 | 3 |  |  |  |  | 8 |  | 
| 163 |  |  |  |  |  |  | return $self->registry_fixup($registry); | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 1 |  |  |  |  | 6 | =for Pod::Coverage BUILD | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =cut | 
| 169 |  |  |  |  |  |  |  | 
| 170 | 1 |  |  |  |  | 7 | my $self = shift; | 
| 171 |  |  |  |  |  |  | $self->_registry; | 
| 172 |  |  |  |  |  |  | return; | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | 1; | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  |  | 
| 178 | 2 |  |  | 2 | 0 | 5970 | =head1 DEPENDENCIES | 
| 179 | 2 |  |  |  |  | 54 |  | 
| 180 | 1 |  |  |  |  | 3 | =over 4 | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =item L<Moose::Role> | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =item L<namespace::autoclean> | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =item L<Syntax::Keyword::Try> | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =item L<YAML::XS> | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =back | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | =head1 SOURCE CODE | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | L<GitHub|https://github.com/binary-com/perl-MooseX-Role-Registry> | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | =head1 AUTHOR | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | binary.com, C<< <perl at binary.com> >> | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | =head1 BUGS | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | Please report any bugs or feature requests to | 
| 203 |  |  |  |  |  |  | C<bug-moosex-role-registry at rt.cpan.org>, or through the web | 
| 204 |  |  |  |  |  |  | interface at | 
| 205 |  |  |  |  |  |  | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Role-Registry>. | 
| 206 |  |  |  |  |  |  | We will be notified, and then you'll automatically be notified of progress on | 
| 207 |  |  |  |  |  |  | your bug as we make changes. | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | =head1 SUPPORT | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | perldoc MooseX::Role::Registry | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | You can also look for information at: | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | =over 4 | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | =item * RT: CPAN's request tracker (report bugs here) | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Role-Registry> | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | L<http://annocpan.org/dist/MooseX-Role-Registry> | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | L<http://cpanratings.perl.org/d/MooseX-Role-Registry> | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | =item * Search CPAN | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | L<http://search.cpan.org/dist/MooseX-Role-Registry/> | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | =back | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =cut | 
| 238 |  |  |  |  |  |  |  |