File: MixinFactory.pm

package info (click to toggle)
libclass-mixinfactory-perl 0.92-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 172 kB
  • sloc: perl: 135; makefile: 2
file content (187 lines) | stat: -rw-r--r-- 5,807 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package Class::MixinFactory;

$VERSION = 0.92;

use strict;

########################################################################

use Class::MixinFactory::Factory;
sub base_factory_class { 'Class::MixinFactory::Factory' }

use Class::MixinFactory::HasAFactory;
sub hasa_factory_class { 'Class::MixinFactory::HasAFactory' }

########################################################################

sub import {
  my ( $facade, $import, @args ) = @_;

  return unless $import;
  my $target_class = ( caller )[0];

  no strict 'refs';

  if ( $import eq '-isafactory' ) {
    push @{"$target_class\::ISA"}, $facade->base_factory_class;

  } elsif ( $import eq '-hasafactory' ) {
    push @{"$target_class\::ISA"}, $facade->hasa_factory_class();

  } elsif ( $import eq '-isasubclass' ) {
    push @{"$target_class\::ISA"}, (shift @args)->class( @args );
  
  } else {
    require Exporter;
    goto &Exporter::import
  }
}

########################################################################

sub new {
  (shift)->base_factory_class->new( @_ )
}

########################################################################

1;

__END__

=head1 NAME

Class::MixinFactory - Class Factory with Selection of Mixins

=head1 SYNOPSIS

  package MyClass;
  use Class::MixinFactory -hasafactory;
  sub new { ... }
  sub foo { return "Foo Bar" }

  package MyClass::Logging;
  sub foo { warn "Calling foo"; (shift)->NEXT('foo', @_) }

  package MyClass::UpperCase;
  sub foo { uc( (shift)->NEXT('foo', @_) ) }

  package main;

  my $class = MyClass->class( 'Logging', 'UpperCase' );
  print $class->new()->foo(); 
  # Calls MyClass::Logging::foo, MyClass::UpperCase::foo, MyClass::foo


=head1 DESCRIPTION

This distribution facilitates the run-time generation of classes which inherit from a base class and some optional selection of mixin classes. 

A factory is provided to generate the mixed classes with multiple inheritance. 
A NEXT method allows method redispatch up the inheritance chain.

=head1 USAGE

The Class::MixinFactory package is just a facade that loads the necessary classes and provides a few import options for compile-time convenience.

=head2 Factory Interface

To generate an object with some combination of mixins, you first pass the names of the mixin classes to a class factory which will generate a mixed class. (Or return the name of the already generated class, if there has been a previous request with the same combination of mixins.) 

You can add a factory method to your base class, create a separate factory object, or inherit to produce a factory class.

=over 4

=item Factory Method

To add a factory method to a base class, inherit from the Class::MixinFactory::HasAFactory class, or use the C<-hasafactory> import option:

  package MyClass;
  use Class::MixinFactory -hasafactory;

  package main;
  my $class = MyClass->class( 'Logging', 'UpperCase' );
  print $class->new()->foo(); 

=item Factory Class

To create a new class which will act as a factory for another base class, inherit from the Class::MixinFactory::Factory class, or use the C<-isafactory> import option:

  package MyClass::Factory;
  use Class::MixinFactory -isafactory;
  MyClass::Factory->base_class( "MyClass" );

  package main;
  my $class = MyClass::Factory->class( 'Logging', 'UpperCase' );
  print $class->new()->foo();

=item Factory Object

To create an object which will act as a factory, create a Class::MixinFactory::Factory instance by calling the new() method:

  use Class::MixinFactory;
  my $factory = Class::MixinFactory->new();
  $factory->base_class( "MyClass" );

  my $class = $factory->class( 'Logging', 'UpperCase' );
  print $class->new()->foo();

=back

=head2 Inheriting from a Mixed Class

=over 4

=item Inheriting with a Factory Method or Factory Object

A subclass can inherit from a mixed class:

  package MyClass::CustomWidget;
  @ISA = MyClass->class( 'Logging', 'UpperCase' );
  sub foo { local $_ = (shift)->NEXT('foo', @_); tr[a-z][z-a]; $_ }

  package main;
  print MyClass::CustomWidget->new()->foo();

=item Inheriting with a Factory Class

A subclass can use a factory class to define its own inheritance:

  package MyClass::CustomWidget;
  use Class::MixinFactory -isasubclass,
	MyClass::Factory => 'Logging', 'UpperCase';
  sub foo { local $_ = (shift)->NEXT('foo', @_); tr[a-z][z-a]; $_ }

  package main;
  print MyClass::CustomWidget->new()->foo();

=back

=head2 Configuring a Factory

Factories support methods that control which classes they will use.

The base class will be inherited from by all mixed classes. 

  $factory->base_class( "HelloWorld" );

The mixin prefix is prepended to the mixin names passed to the class() method. Mixin names that contain a "::" are assumed to be fully qualified and are not changed. If empty, the base_class is used.

  $factory->mixin_prefix( 'HelloFeature' );

The mixed prefix is at the start of all generated class names. If empty, the base_class is used, or the factory's class name.

  $factory->mixed_prefix( 'HelloClass' );

=head2 Writing a Mixin Class

Writing a mixin class is almost the same as writing a subclass, except where methods need to redispatch to the base-class implementation. (The SUPER::method syntax will only search for classes that the mixin itself inherits from; to search back up the inheritance tree and explore other branches, another redispatch mechanism is needed.) 

A method named NEXT is provided to continue the search through to the next class which provides a given method. The order in which mixins are stacked is significant, so the caller should understand how their behaviors interact. (See L<Class::MixinFactory::NEXT>.)

=head1 SEE ALSO

For distribution, installation, support, copyright and license 
information, see L<Class::MixinFactory::ReadMe>.

=cut