File Coverage

blib/lib/Mixin/ExtraFields/Hive.pm
Criterion Covered Total %
statement 56 57 98.2
branch 13 14 92.8
condition n/a
subroutine 15 15 100.0
pod 5 5 100.0
total 89 91 97.8


line stmt bran cond sub pod time code
1             package Mixin::ExtraFields::Hive 0.008;
2             # ABSTRACT: infest your objects with hives
3              
4 1     1   78860 use Mixin::ExtraFields 0.002 ();
  1         14359  
  1         27  
5 1     1   8 use parent qw(Mixin::ExtraFields);
  1         2  
  1         6  
6              
7             #pod =head1 SYNOPSIS
8             #pod
9             #pod use Mixin::ExtraFields::Hive -hive => {
10             #pod moniker => 'registry',
11             #pod driver => 'DBI',
12             #pod };
13             #pod
14             #pod =head1 DESCRIPTION
15             #pod
16             #pod This module provides a Data::Hive to other classes' objects as a mix-in,
17             #pod powered by Mixin::ExtraFields. It behaves like Mixin::ExtraFields, but
18             #pod generates a different set of methods. It can use any Mixin::ExtraFields
19             #pod driver.
20             #pod
21             #pod =head1 GENERATED METHODS
22             #pod
23             #pod =head2 hive
24             #pod
25             #pod The main export of this module is the C method, generated by importing
26             #pod the C<-hive> group. The method will be imported under the moniker given to the
27             #pod C group. If this all sounds like Greek, you should probably re-read the
28             #pod L documentation. As a simple example, however, the code in
29             #pod the L, above, would generate a C method instead of a
30             #pod C method.
31             #pod
32             #pod This method will return a L object for extra fields for the object
33             #pod on which it's called. At present, the Data::Hive object is recreated for each
34             #pod call to the C method. In the future, it will be possible to cache these
35             #pod on the object or in some other manner.
36             #pod
37             #pod =head2 other methods
38             #pod
39             #pod At present, two support methods are installed by this mixin. These methods may
40             #pod go away in the future, when a more purpose-built subclass of Data::Hive::Store
41             #pod is used.
42             #pod
43             #pod These methods are:
44             #pod
45             #pod _mutate_hive - acts as a combined get/set extra accessor
46             #pod _exists_hive - acts as the standard exists_extra method
47             #pod _empty_hive - deletes all hive data
48             #pod _delete_hive - deletes a single hive entry
49             #pod
50             #pod =cut
51              
52 1     1   70 use Data::Hive 1.006;
  1         14  
  1         23  
53 1     1   464 use Data::Hive::Store::Param 1.001;
  1         830  
  1         42  
54              
55             # I wish this was easier. -- rjbs, 2006-12-09
56 1         7 use Sub::Exporter -setup => {
57             groups => [ hive => \'gen_fields_group', ],
58 1     1   7 };
  1         3  
59              
60 1     1 1 373 sub default_moniker { 'hive' }
61              
62 2     2 1 2920 sub methods { qw(hive mutate exists empty delete) }
63              
64             sub _build_mutate_method {
65 2     2   5 my ($self, $arg) = @_;
66              
67 2         3 my $id_method = $arg->{id_method};
68 2         3 my $driver = $arg->{driver};
69 2         4 my $driver_set = $self->driver_method_name('set');
70 2         16 my $driver_get = $self->driver_method_name('get');
71 2         15 my $driver_all = $self->driver_method_name('get_all');
72              
73             return sub {
74 59     59   34977 my $self = shift;
75 59         159 my $id = $self->$$id_method;
76              
77 59 100       343 if (@_ == 0) {
    100          
    50          
78 15         44 my %all = $$driver->$driver_all($self, $id);
79 15         359 return keys %all;
80             } elsif (@_ == 1) {
81 28         55 my ($name) = @_;
82 28         88 return $$driver->$driver_get($self, $id, $name);
83             } elsif (@_ == 2) {
84 16         38 my ($name, $value) = @_;
85 16         57 return $$driver->$driver_set($self, $id, $name, $value);
86             } else {
87 0         0 Carp::confess 'too many arguments passed to hive mutator';
88             }
89 2         22 };
90             }
91              
92             sub _build_hive_method {
93 2     2   3 my ($self, $arg) = @_;
94              
95 2         4 my $id_method = $arg->{id_method};
96 2         4 my $moniker = ${ $arg->{moniker} };
  2         3  
97              
98 2         5 my %store_args = (
99             method => $self->method_name('mutate', $moniker),
100             );
101              
102 2         5 for my $which (qw(exists delete)) {
103 4         8 my $method_name = $self->method_name($which, $moniker);
104              
105 4     31   16 $store_args{ $which } = sub { $_[0]->param_store->$method_name($_[1]) };
  31         27309  
106             }
107              
108             sub {
109 11     11   5868 my ($self) = @_;
110 11         34 my $id = $self->$$id_method;
111              
112             # We should really get around to caching these in some awesome way.
113             # -- rjbs, 2006-12-09
114 11         90 Data::Hive->NEW({
115             store_class => 'Param',
116             store_args => [ $self, \%store_args ],
117             });
118             }
119 2         13 }
120              
121             sub build_method {
122 10     10 1 47 my ($self, $method, $arg) = @_;
123              
124 10 100       25 return $self->_build_mutate_method($arg) if $method eq 'mutate';
125 8 100       19 return $self->_build_hive_method($arg) if $method eq 'hive';
126              
127 6 100       11 $method = 'delete_all' if $method eq 'empty';
128              
129 6         15 $self->SUPER::build_method($method, $arg);
130             }
131              
132             sub driver_method_name {
133 12     12 1 49 my ($self, $method) = @_;
134 12         25 $self->SUPER::method_name($method, 'extra');
135             }
136              
137             sub method_name {
138 16     16 1 84 my ($self, $method, $moniker) = @_;
139              
140 16 100       36 return $moniker if $method eq 'hive';
141              
142 14         32 return "_$method\_$moniker";
143             }
144              
145             #pod =head1 TODO
146             #pod
147             #pod =for :list
148             #pod * provide a customizable means to cache created Data::Hive objects
149             #pod
150             #pod =cut
151              
152             1;
153              
154             __END__