1 package Class::C3::Componentised;
5 Class::C3::Componentised
9 Load mix-ins or components to your C3-based class.
18 use base 'Class::C3::Componentised';
20 sub component_base_class { "MyModule::Component" }
24 MyModule->load_components( qw/Foo Bar/ );
25 # Will load MyModule::Component::Foo and MyModule::Component::Bar
29 This will inject base classes to your module using the L<Class::C3> method
32 Please note: these are not plugins that can take precedence over methods
33 declared in MyModule. If you want something like that, consider
34 L<MooseX::Object::Pluggable>.
43 # see Makefile.PL for discussion on why we load both Class::C3 and MRO::Compat
49 our $VERSION = 1.0006;
51 =head2 load_components( @comps )
53 Loads the given components into the current module. If a module begins with a
54 C<+> character, it is taken to be a fully qualified class name, otherwise
55 C<< $class->component_base_class >> is prepended to it.
57 Calling this will call C<Class::C3::reinitialize>.
66 : join ('::', $class->component_base_class, $_)
68 grep { $_ !~ /^#/ } @_;
69 $class->_load_components(@comp);
72 =head2 load_own_components( @comps )
74 Similar to L<load_components>, but assumes every class is C<"$class::$comp">.
78 sub load_own_components {
80 my @comp = map { "${class}::$_" } grep { $_ !~ /^#/ } @_;
81 $class->_load_components(@comp);
84 sub _load_components {
85 my ($class, @comp) = @_;
86 foreach my $comp (@comp) {
87 $class->ensure_class_loaded($comp);
89 $class->inject_base($class => @comp);
90 Class::C3::reinitialize();
93 =head2 load_optional_components
95 As L<load_components>, but will silently ignore any components that cannot be
100 sub load_optional_components {
102 my @comp = grep { $class->load_optional_class( $_ ) }
106 : join ('::', $class->component_base_class, $_)
108 grep { $_ !~ /^#/ } @_;
110 $class->_load_components( @comp ) if scalar @comp;
113 =head2 ensure_class_loaded
115 Given a class name, tests to see if it is already loaded or otherwise
116 defined. If it is not yet loaded, the package is require'd, and an exception
117 is thrown if the class is still not loaded.
119 BUG: For some reason, packages with syntax errors are added to %INC on
124 # TODO: handle ->has_many('rel', 'Class'...) instead of
125 # ->has_many('rel', 'Some::Schema::Class'...)
127 sub ensure_class_loaded {
128 my ($class, $f_class) = @_;
130 croak "Invalid class name $f_class"
131 if ($f_class=~m/(?:\b:\b|\:{3,})/);
132 return if Class::Inspector->loaded($f_class);
133 my $file = $f_class . '.pm';
135 eval { CORE::require($file) }; # require needs a bareword or filename
137 if ($class->can('throw_exception')) {
138 $class->throw_exception($@);
145 =head2 ensure_class_found
147 Returns true if the specified class is installed or already loaded, false
150 Note that the underlying mechanism (Class::Inspector->installed()) used by this
151 sub will not, at the time of writing, correctly function when @INC includes
152 coderefs. Since PAR relies upon coderefs in @INC, this function should be
153 avoided in modules that are likely to be included within a PAR.
157 sub ensure_class_found {
158 my ($class, $f_class) = @_;
159 return Class::Inspector->loaded($f_class) ||
160 Class::Inspector->installed($f_class);
166 Does the actual magic of adjusting @ISA on the target module.
171 my ($class, $target, @to_inject) = @_;
174 foreach my $to (reverse @to_inject) {
175 unshift ( @{"${target}::ISA"}, $to )
176 unless ($target eq $to || $target->isa($to));
180 mro::set_mro($target, 'c3');
183 =head2 load_optional_class
185 Returns a true value if the specified class is installed and loaded
186 successfully, throws an exception if the class is found but not loaded
187 successfully, and false if the class is not installed
191 sub load_optional_class {
192 my ($class, $f_class) = @_;
193 eval { $class->ensure_class_loaded($f_class) };
194 my $err = $@; # so we don't lose it
199 my $fn = (join ('/', split ('::', $f_class) ) ) . '.pm';
200 if ($err =~ /Can't locate ${fn} in \@INC/ ) {
211 Matt S. Trout and the DBIx::Class team
213 Pulled out into seperate module by Ash Berlin C<< <ash@cpan.org> >>
217 You may distribute this code under the same terms as Perl itself.