| 1 | package Data::Collector::Info; | ||||||||||
| 2 | { | ||||||||||
| 3 | 5 | $Data::Collector::Info::VERSION = '0.15'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: A base class for information classes | ||||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 5 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Set::Object; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 5 | ▶ | use List::Util 'first'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 5 | has 'raw_data' => ( is => 'rw', isa => 'Str', lazy_build => 1 ); | |||||||||
| 15 | 5 | has 'engine' => ( is => 'ro', isa => 'Object' ); | |||||||||
| 16 | |||||||||||
| 17 | 5 | my $REGISTRY = Set::Object->new(); | |||||||||
| 18 | 5 | my $INFO_MODULES = Set::Object->new(); | |||||||||
| 19 | |||||||||||
| 20 | 7 | ▶ | sub register { | ||||||||
|
Subroutine
register: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 21 | 7 | my $class = shift; | |||||||||
| 22 | 7 | my @keys = @_; | |||||||||
| 23 | |||||||||||
| 24 | 7 | foreach my $key (@keys) { | |||||||||
| 25 | 5 | ▶ | if ( first { $key eq $_ } $REGISTRY->members ) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 26 | 2 | croak "Sorry, key already reserved\n" . | |||||||||
| 27 | 'Is it possible you\'re collecting twice?'; | ||||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 3 | $REGISTRY->insert($key); | |||||||||
| 31 | } | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | sub unregister { | ||||||||
|
Subroutine
unregister: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 35 | 0 | my @keys = @_; | |||||||||
| 36 | 0 | $REGISTRY->remove($_) for @keys; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | sub clear_registry { $REGISTRY = Set::Object->new } | ||||||||
|
Subroutine
clear_registry: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | # overridable method | ||||||||||
| 42 | 5 | ▶ | sub load {1} | ||||||||
|
Subroutine
load: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 2 | ▶ | sub info_keys { die 'No default info_keys method' } | ||||||||
|
Subroutine
info_keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 44 | 1 | ▶ | sub all { die 'No default all method' } | ||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | |||||||||||
| 46 | 8 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 47 | 8 | my $self = shift; | |||||||||
| 48 | 8 | my $class = ref $self; | |||||||||
| 49 | 8 | my $contained = $INFO_MODULES->contains($class); | |||||||||
| 50 | |||||||||||
| 51 | 8 | ▶ | if ( ! $contained ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 6 | $INFO_MODULES->insert($class); | |||||||||
| 53 | |||||||||||
| 54 | 6 | my $keys = $self->info_keys; | |||||||||
| 55 | 5 | ▶ | ref $keys eq 'ARRAY' and Data::Collector::Info->register( @{$keys} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 4 | $self->load(); | |||||||||
| 58 | } | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||
| 62 | 5 | 1; | |||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | =pod | ||||||||||
| 67 | |||||||||||
| 68 | =head1 NAME | ||||||||||
| 69 | |||||||||||
| 70 | Data::Collector::Info - A base class for information classes | ||||||||||
| 71 | |||||||||||
| 72 | =head1 VERSION | ||||||||||
| 73 | |||||||||||
| 74 | version 0.15 | ||||||||||
| 75 | |||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||
| 77 | |||||||||||
| 78 | package Data::Collector::Info::Bamba; | ||||||||||
| 79 | use Moose; | ||||||||||
| 80 | extends 'Data::Collector::Info'; | ||||||||||
| 81 | |||||||||||
| 82 | sub info_keys { ['bamba'] } | ||||||||||
| 83 | |||||||||||
| 84 | sub _build_raw_data { | ||||||||||
| 85 | my $self = shift; | ||||||||||
| 86 | my $engine = $self->engine; | ||||||||||
| 87 | |||||||||||
| 88 | return $engine->run(/usr/bin/bamba-counter); | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | sub all { | ||||||||||
| 92 | my $self = shift; | ||||||||||
| 93 | return $self->raw_data; | ||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | This synopsis shows how to create your own piece of info (in this case it counts | ||||||||||
| 97 | bambas (which is a peanut snack). | ||||||||||
| 98 | |||||||||||
| 99 | =head1 REGISTRY | ||||||||||
| 100 | |||||||||||
| 101 | Since all info modules return values which are all gathered in a single hash, | ||||||||||
| 102 | they might step on each other's toes. In order to avoid this, there is a | ||||||||||
| 103 | registry that keeps all the keys from each info module. If you create an info | ||||||||||
| 104 | module, you should register your keys in the registry. This is best done while | ||||||||||
| 105 | subclassing the C<load> method as shown in the synopsis. | ||||||||||
| 106 | |||||||||||
| 107 | =head1 ATTRIBUTES | ||||||||||
| 108 | |||||||||||
| 109 | =head2 raw_data | ||||||||||
| 110 | |||||||||||
| 111 | This contains the data received from the engine. You should implement a builder | ||||||||||
| 112 | for it under the name C<_build_raw_data>. | ||||||||||
| 113 | |||||||||||
| 114 | =head2 engine | ||||||||||
| 115 | |||||||||||
| 116 | This contains the object of the engine the info module would be using to fetch | ||||||||||
| 117 | information. This is set by L<Data::Collector> on initialize. | ||||||||||
| 118 | |||||||||||
| 119 | =head1 SUBROUTINES/METHODS | ||||||||||
| 120 | |||||||||||
| 121 | =head2 register | ||||||||||
| 122 | |||||||||||
| 123 | This method registers keys in the registry. You can provide as many as you want. | ||||||||||
| 124 | |||||||||||
| 125 | It should be called using the class, not any object, as such: | ||||||||||
| 126 | |||||||||||
| 127 | Data::Collector::Info->register('bamba_count'); | ||||||||||
| 128 | |||||||||||
| 129 | Now if anyone else will try to register another key (such as another bamba | ||||||||||
| 130 | module), L<Data::Collector::Info> will prevent it from happening. | ||||||||||
| 131 | |||||||||||
| 132 | =head2 unregister | ||||||||||
| 133 | |||||||||||
| 134 | This method can be used to remove keys from the registry. However, B<refrain> | ||||||||||
| 135 | from using this method in order to provide two collections. The reason is that | ||||||||||
| 136 | there is still a boolean in L<Data::Collector> that will stll prevent you from | ||||||||||
| 137 | running another collection. | ||||||||||
| 138 | |||||||||||
| 139 | =head2 clear_registry | ||||||||||
| 140 | |||||||||||
| 141 | This method ostensibly clears all keys from the registry. In actuality, it | ||||||||||
| 142 | simply replaces the existing registry with a new one. | ||||||||||
| 143 | |||||||||||
| 144 | =head2 info_keys | ||||||||||
| 145 | |||||||||||
| 146 | This method will run before an information module is loaded. You should subclass | ||||||||||
| 147 | this method to indicate what keys you're going to acquire. | ||||||||||
| 148 | |||||||||||
| 149 | Any type of data other than an arrayref will be ignored. | ||||||||||
| 150 | |||||||||||
| 151 | You B<must> subclass this method or your code will C<die>. | ||||||||||
| 152 | |||||||||||
| 153 | =head2 load | ||||||||||
| 154 | |||||||||||
| 155 | This method will run when an information module is loaded. You should subclass | ||||||||||
| 156 | this method if you're writing an info module that requires some extra bells | ||||||||||
| 157 | and whistles you could use this method. | ||||||||||
| 158 | |||||||||||
| 159 | However, you do not have to subclass it. | ||||||||||
| 160 | |||||||||||
| 161 | =head2 all | ||||||||||
| 162 | |||||||||||
| 163 | This method is run to get all the information attainable by the info module. | ||||||||||
| 164 | |||||||||||
| 165 | If you have several bits of information in your module, you can have methods | ||||||||||
| 166 | for each, but they should all be attainable using the C<all> method. | ||||||||||
| 167 | |||||||||||
| 168 | You B<must> subclass this method or your code will C<die>. | ||||||||||
| 169 | |||||||||||
| 170 | =head2 BUILD | ||||||||||
| 171 | |||||||||||
| 172 | This is a L<Moose> method which is done after module initialization. It's set | ||||||||||
| 173 | to run the C<load> method of the module. However, if you wish to do something | ||||||||||
| 174 | else, you could subclass it. | ||||||||||
| 175 | |||||||||||
| 176 | Unless you're pulling off something especially fancy, subclass the L<load> | ||||||||||
| 177 | and use that instead - for the sake of clarity if not anything else. | ||||||||||
| 178 | |||||||||||
| 179 | =head1 AUTHOR | ||||||||||
| 180 | |||||||||||
| 181 | Sawyer X <xsawyerx@cpan.org> | ||||||||||
| 182 | |||||||||||
| 183 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 184 | |||||||||||
| 185 | This software is copyright (c) 2012 by Sawyer X. | ||||||||||
| 186 | |||||||||||
| 187 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 188 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 189 | |||||||||||
| 190 | =cut | ||||||||||
| 191 | |||||||||||
| 192 | |||||||||||
| 193 | __END__ | ||||||||||