File Coverage

lib/Egg/Component.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Egg::Component;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Component.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 37     37   374 use strict;
  37         76  
  37         1517  
8 37     37   215 use warnings;
  37         84  
  37         1259  
9 37     37   1326 use UNIVERSAL::require;
  37         77746  
  37         363  
10 37     37   367694 use Class::C3;
  37         306167  
  37         253  
11 37     37   23636 use Tie::Hash::Indexed;
  0            
  0            
12             use Carp qw/ croak /;
13             use base qw/ Class::Data::Inheritable /;
14             use Egg::Component::Base;
15              
16             our $VERSION= '3.02';
17              
18             sub initialize {
19             my $class= ref($_[0]) || $_[0];
20             for my $method (qw/ namespace regists config /) {
21             next if $class->can($method);
22             $class->mk_classdata($method);
23             }
24             unless ($class->namespace) {
25             $class->namespace($class);
26             $class->regists($class->ixhash);
27             ## $class->config({}) unless $class->config;
28             }
29             no strict 'refs'; ## no critic.
30             no warnings 'redefine';
31             *{"${class}::___add_regists"}= sub {
32             my $self = shift;
33             my $label= shift || die q{I want label name.};
34             my $pkg = shift || die q{I want package name.};
35             my $conf = shift || undef;
36             $self->regists->{$label}= [$pkg, ($pkg->VERSION || '0.00'), $conf];
37             };
38             $class;
39             }
40             sub isa_register {
41             my($proto, $label, $pkg)= _get_args(@_);
42             $proto= ref($proto) if ref($proto);
43             no strict 'refs'; ## no critic.
44             push @{"${proto}::ISA"}, $pkg;
45             $proto->___add_regists($label, $pkg);
46             }
47             sub add_register {
48             my($proto, $label, $pkg, $conf)= _get_args(@_);
49             $proto->___add_regists($label, $pkg, $conf);
50             }
51             sub isa_terminator {
52             my $proto= shift;
53             $proto= ref($proto) if ref($proto);
54             no strict 'refs'; ## no critic.
55             my $isa= \@{"${proto}::ISA"};
56             if (my $base= shift) {
57             my $regex= quotemeta($base);
58             @$isa= grep !/^$regex$/, @$isa;
59             push @$isa, $base;
60             }
61             return $proto if ($isa->[$#{$isa}] eq 'Egg::Component::Base');
62             @$isa= grep !/^Egg\:+Component\:+Base/, @$isa;
63             push @$isa, 'Egg::Component::Base';
64             $proto;
65             }
66             sub ixhash {
67             shift;
68             tie my %hash, 'Tie::Hash::Indexed';
69             %hash= @_ if @_;
70             \%hash;
71             }
72             sub _get_args {
73             my $proto = shift;
74             $proto = ref($proto) if ref($proto);
75             my $load = shift || 0;
76             my $label = shift || croak q{ I want label name. };
77             my $pkg = shift || $label;
78             my $config= shift || undef;
79             if ($load) {
80             $load > 1 ? do { $pkg->use or croak "$proto - $@" }
81             : do { $pkg->require or croak "$proto - $@" };
82             }
83             $pkg->config($config) if ($config and $pkg->can('config'));
84             ($proto, lc($label), $pkg, $config, @_);
85             }
86             sub ___add_regists { $_[0] }
87              
88             1;
89              
90             __END__
91              
92             =head1 NAME
93              
94             Egg::Component - Base class to treat Egg component.
95              
96             =head1 SYNOPSIS
97              
98             package MyComponent;
99             use strict;
100             use base qw/ Egg::Component /;
101            
102             our @ISA;
103            
104             sub import {
105             my $class= shift;
106             $class->initialize;
107             for (@_) {
108             $class->isa_register(1, $_);
109             }
110             $class->isa_terminator;
111             $class;
112             }
113              
114             =head1 DESCRIPTION
115              
116             It is a base class to handle various components of the plug-in, the model, and the
117             view, etc.
118              
119             This module contains the class for the terminal corresponding to the hook call.
120              
121             =head1 METHODS
122              
123             =head2 initialize
124              
125             It initializes it.
126              
127             Namespace, config, and the regists method of the relation to the class that calls
128             it by this method are generated.
129              
130             =head2 namespace
131              
132             The class name that calls initialize is returned.
133              
134             =head2 config
135              
136             The configuration that relates to the class that calls initialize is returned.
137              
138             =head2 regists
139              
140             It returns it with HASH to which the list of the component that relates to the
141             class that calls initialize is generated by the ixhash method.
142              
143             The value of HASH is ARRAY reference.
144             The first element is a package name.
145             The second element is a version of the package.
146             The third element is a configuration.
147             It becomes a structure.
148              
149             =head2 isa_register ([LOAD_BOOL], [LABEL_STRING], [PACKAGE_STRING], [CONFIG_DATA])
150              
151             It registers in @ISA that relates to the class that calls the component, and
152             it registers in the component list by the regists method.
153              
154             Require does PACKAGE_STRING at the same time as passing an effective value to
155             LOAD_BOOL.
156              
157             LABEL_STRING is a name of the key when registering in the regists method.
158              
159             PACKAGE_STRING is a package name of the component module. It is assumed the one
160             that the package name is specified for LABEL_STRING when omitting it.
161              
162             When registering in regists, it preserves it in the third element when CONFIG_DATA
163             is specified. It calls it in PACKAGE_STRING, and in addition, CONFIG_DATA is
164             passed and called to the Japanese oak including the config method and the method.
165              
166             =head2 isa_terminator
167              
168             L<Egg::Component::Base> is added to @ISA that relates to the class that calls the
169             component.
170              
171             If the terminal class has already been registered, nothing is done.
172             Moreover, if the terminal class is not located at the end of @ISA, @ISA is
173             adjusted.
174              
175             This method assumes the thing called after a series of 'isa_register' method is
176             processed. Please call this method and adjust @ISA when you call 'isa_register'
177             again afterwards.
178              
179             for (@comps) {
180             ............
181             .....
182             $class->isa_register( .... );
183             }
184             $class->isa_terminator;
185              
186             =head2 add_register ([LOAD_BOOL], [LABEL_STRING], [PACKAGE_STRING], [CONFIG_DATA])
187              
188             The operation of @ISA does all processing similar to the isa_register method
189             excluding the thing not done.
190              
191             =head2 ixhash ([HASH_DATA])
192              
193             L<Tie::Hash::Indexed> ¤Ë¤è¤ëHASH¤òÀ¸À®¤·¤Æ¡¢¤½¤ì¤òHASH¥ê¥Õ¥¡¥ì¥ó¥¹¤ÇÊÖ¤·¤Þ¤¹¡£
194              
195             HASH defined to pass HASH_DATA is returned.
196              
197             HASH_DATA is bad in the reference. It is made to pass with usual HASH.
198              
199             my $hash= $component->ixhash(
200             hoge => 'booo',
201             zooo => 'baaa',
202             .....
203             );
204              
205             =head1 SEE ALSO
206              
207             L<Egg::Release>,
208             L<Egg::Component::Base>,
209             L<UNIVERSAL::require>,
210             L<Class::C3>,
211             L<Tie::Hash::Indexed>,
212             L<Class::Data::Inheritable>,
213              
214             =head1 AUTHOR
215              
216             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
221              
222             This library is free software; you can redistribute it and/or modify
223             it under the same terms as Perl itself, either Perl version 5.8.6 or,
224             at your option, any later version of Perl 5 you may have available.
225              
226             =cut
227