Lots of doc updates
[gitmo/Class-MOP.git] / examples / ArrayBasedStorage.pod
CommitLineData
43715282 1
62189f84 2package # hide the package from PAUSE
f892c0f0 3 ArrayBasedStorage::Instance;
0e76a376 4
5use strict;
6use warnings;
7
8use Carp 'confess';
9
10our $VERSION = '0.01';
11
12use base 'Class::MOP::Instance';
13
14sub new {
15 my ($class, $meta, @attrs) = @_;
16 my $self = $class->SUPER::new($meta, @attrs);
17 my $index = 0;
c23184fc 18 $self->{'%!slot_index_map'} = { map { $_ => $index++ } $self->get_all_slots };
0e76a376 19 return $self;
20}
21
22sub create_instance {
23 my $self = shift;
24 $self->bless_instance_structure([]);
25}
26
f7259199 27sub clone_instance {
28 my ($self, $instance) = shift;
29 $self->bless_instance_structure([ @$instance ]);
30}
31
0e76a376 32# operations on meta instance
33
c23184fc 34sub get_slot_index_map { (shift)->{'%!slot_index_map'} }
62189f84 35
0e76a376 36sub get_all_slots {
37 my $self = shift;
f7259199 38 return sort $self->SUPER::get_all_slots;
0e76a376 39}
40
41sub get_slot_value {
42 my ($self, $instance, $slot_name) = @_;
c23184fc 43 return $instance->[ $self->{'%!slot_index_map'}->{$slot_name} ];
0e76a376 44}
45
46sub set_slot_value {
47 my ($self, $instance, $slot_name, $value) = @_;
c23184fc 48 $instance->[ $self->{'%!slot_index_map'}->{$slot_name} ] = $value;
0e76a376 49}
50
0e76a376 51sub is_slot_initialized {
52 # NOTE:
53 # maybe use CLOS's *special-unbound-value*
54 # for this ?
55 confess "Cannot really tell this for sure";
56}
57
581;
59
60__END__
61
62=pod
63
64=head1 NAME
65
f892c0f0 66ArrayBasedStorage - An example of an Array based instance storage
0e76a376 67
68=head1 SYNOPSIS
69
f892c0f0 70 package Foo;
71
1becdfcc 72 use metaclass (
f892c0f0 73 ':instance_metaclass' => 'ArrayBasedStorage::Instance'
74 );
75
76 __PACKAGE__->meta->add_attribute('foo' => (
77 reader => 'get_foo',
78 writer => 'set_foo'
79 ));
80
81 sub new {
82 my $class = shift;
83 $class->meta->new_object(@_);
84 }
85
86 # now you can just use the class as normal
87
0e76a376 88=head1 DESCRIPTION
89
f892c0f0 90This is a proof of concept using the Instance sub-protocol
91which uses ARRAY refs to store the instance data.
92
1becdfcc 93This is very similar now to the InsideOutClass example, and
94in fact, they both share the exact same test suite, with
95the only difference being the Instance metaclass they use.
96
1a09d9cc 97=head1 AUTHORS
0e76a376 98
99Stevan Little E<lt>stevan@iinteractive.comE<gt>
100
1a09d9cc 101Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
102
0e76a376 103=head1 SEE ALSO
104
105=head1 COPYRIGHT AND LICENSE
106
2367814a 107Copyright 2006, 2007 by Infinity Interactive, Inc.
0e76a376 108
109L<http://www.iinteractive.com>
110
111This library is free software; you can redistribute it and/or modify
112it under the same terms as Perl itself.
113
114=cut