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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!/usr/local/bin/perl
use strict;
use warnings;
use Test::More;
=pod
This is an example of making Moose behave
more like a prototype based object system.
Why?
Well cause merlyn asked if it could :)
=cut
## ------------------------------------------------------------------
## make some metaclasses
{
package ProtoMoose::Meta::Instance;
use Moose;
BEGIN { extends 'Moose::Meta::Instance' };
# NOTE:
# do not let things be inlined by
# the attribute or accessor generator
sub is_inlinable { 0 }
}
{
package ProtoMoose::Meta::Method::Accessor;
use Moose;
BEGIN { extends 'Moose::Meta::Method::Accessor' };
# customize the accessors to always grab
# the correct instance in the accessors
sub find_instance {
my ($self, $candidate, $accessor_type) = @_;
my $instance = $candidate;
my $attr = $self->associated_attribute;
# if it is a class calling it ...
unless (blessed($instance)) {
# then grab the class prototype
$instance = $attr->associated_class->prototype_instance;
}
# if its an instance ...
else {
# and there is no value currently
# associated with the instance and
# we are trying to read it, then ...
if ($accessor_type eq 'r' && !defined($attr->get_value($instance))) {
# again, defer the prototype in
# the class in which is was defined
$instance = $attr->associated_class->prototype_instance;
}
# otherwise, you want to assign
# to your local copy ...
}
return $instance;
}
sub _generate_accessor_method {
my $self = shift;
my $attr = $self->associated_attribute;
return sub {
if (scalar(@_) == 2) {
$attr->set_value(
$self->find_instance($_[0], 'w'),
$_[1]
);
}
$attr->get_value($self->find_instance($_[0], 'r'));
};
}
sub _generate_reader_method {
my $self = shift;
my $attr = $self->associated_attribute;
return sub {
confess "Cannot assign a value to a read-only accessor" if @_ > 1;
$attr->get_value($self->find_instance($_[0], 'r'));
};
}
sub _generate_writer_method {
my $self = shift;
my $attr = $self->associated_attribute;
return sub {
$attr->set_value(
$self->find_instance($_[0], 'w'),
$_[1]
);
};
}
# deal with these later ...
sub generate_predicate_method {}
sub generate_clearer_method {}
}
{
package ProtoMoose::Meta::Attribute;
use Moose;
BEGIN { extends 'Moose::Meta::Attribute' };
sub accessor_metaclass { 'ProtoMoose::Meta::Method::Accessor' }
}
{
package ProtoMoose::Meta::Class;
use Moose;
BEGIN { extends 'Moose::Meta::Class' };
has 'prototype_instance' => (
is => 'rw',
isa => 'Object',
predicate => 'has_prototypical_instance',
lazy => 1,
default => sub { (shift)->new_object }
);
sub initialize {
# NOTE:
# I am not sure why 'around' does
# not work here, have to investigate
# it later - SL
(shift)->SUPER::initialize(@_,
instance_metaclass => 'ProtoMoose::Meta::Instance',
attribute_metaclass => 'ProtoMoose::Meta::Attribute',
);
}
around 'construct_instance' => sub {
my $next = shift;
my $self = shift;
# NOTE:
# we actually have to do this here
# to tie-the-knot, if you take it
# out, then you get deep recursion
# several levels deep :)
$self->prototype_instance($next->($self, @_))
unless $self->has_prototypical_instance;
return $self->prototype_instance;
};
}
{
package ProtoMoose::Object;
use metaclass 'ProtoMoose::Meta::Class';
use Moose;
sub new {
my $prototype = blessed($_[0])
? $_[0]
: $_[0]->meta->prototype_instance;
my (undef, %params) = @_;
my $self = $prototype->meta->clone_object($prototype, %params);
$self->BUILDALL(\%params);
return $self;
}
}
## ------------------------------------------------------------------
## make some classes now
{
package Foo;
use Moose;
extends 'ProtoMoose::Object';
has 'bar' => (is => 'rw');
}
{
package Bar;
use Moose;
extends 'Foo';
has 'baz' => (is => 'rw');
}
## ------------------------------------------------------------------
## ------------------------------------------------------------------
## Check that metaclasses are working/inheriting properly
foreach my $class (qw/ProtoMoose::Object Foo Bar/) {
isa_ok($class->meta,
'ProtoMoose::Meta::Class',
'... got the right metaclass for ' . $class . ' ->');
is($class->meta->instance_metaclass,
'ProtoMoose::Meta::Instance',
'... got the right instance meta for ' . $class);
is($class->meta->attribute_metaclass,
'ProtoMoose::Meta::Attribute',
'... got the right attribute meta for ' . $class);
}
## ------------------------------------------------------------------
# get the prototype for Foo
my $foo_prototype = Foo->meta->prototype_instance;
isa_ok($foo_prototype, 'Foo');
# set a value in the prototype
$foo_prototype->bar(100);
is($foo_prototype->bar, 100, '... got the value stored in the prototype');
# the "class" defers to the
# the prototype when asked
# about attributes
is(Foo->bar, 100, '... got the value stored in the prototype (through the Foo class)');
# now make an instance, which
# is basically a clone of the
# prototype
my $foo = Foo->new;
isa_ok($foo, 'Foo');
# the instance is *not* the prototype
isnt($foo, $foo_prototype, '... got a new instance of Foo');
# but it has the same values ...
is($foo->bar, 100, '... got the value stored in the instance (inherited from the prototype)');
# we can even change the values
# in the instance
$foo->bar(300);
is($foo->bar, 300, '... got the value stored in the instance (overwriting the one inherited from the prototype)');
# and not change the one in the prototype
is($foo_prototype->bar, 100, '... got the value stored in the prototype');
is(Foo->bar, 100, '... got the value stored in the prototype (through the Foo class)');
## subclasses
# now we can check that the subclass
# will seek out the correct prototypical
# value from it's "parent"
is(Bar->bar, 100, '... got the value stored in the Foo prototype (through the Bar class)');
# we can then also set it's local attrs
Bar->baz(50);
is(Bar->baz, 50, '... got the value stored in the prototype (through the Bar class)');
# now we clone the Bar prototype
my $bar = Bar->new;
isa_ok($bar, 'Bar');
isa_ok($bar, 'Foo');
# and we see that we got the right values
# in the instance/clone
is($bar->bar, 100, '... got the value stored in the instance (inherited from the Foo prototype)');
is($bar->baz, 50, '... got the value stored in the instance (inherited from the Bar prototype)');
# nowe we can change the value
$bar->bar(200);
is($bar->bar, 200, '... got the value stored in the instance (overriding the one inherited from the Foo prototype)');
# and all our original and
# prototypical values are still
# the same
is($foo->bar, 300, '... still got the original value stored in the instance (inherited from the prototype)');
is(Foo->bar, 100, '... still got the original value stored in the prototype (through the Foo class)');
is(Bar->bar, 100, '... still got the original value stored in the prototype (through the Bar class)');
done_testing;
|