no-more-inline
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance.pm
CommitLineData
24869f62 1
2package Class::MOP::Instance;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'reftype', 'weaken';
9
10our $VERSION = '0.01';
11
12sub meta {
13 require Class::MOP::Class;
14 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
15}
16
17sub new {
052c2a1a 18 my ($class, $meta, @attrs) = @_;
c57c8b10 19 my @slots = map { $_->slots } @attrs;
24869f62 20 bless {
fc3ddd1d 21 # NOTE:
22 # I am not sure that it makes
23 # sense to pass in the meta
24 # The ideal would be to just
25 # pass in the class name, but
26 # that is placing too much of
27 # an assumption on bless(),
28 # which is *probably* a safe
29 # assumption,.. but you can
30 # never tell <:)
49c93440 31 meta => $meta,
052c2a1a 32 slots => \@slots,
24869f62 33 } => $class;
34}
35
49c93440 36sub create_instance {
37 my $self = shift;
38 $self->bless_instance_structure({});
2d711cc8 39}
40
49c93440 41sub bless_instance_structure {
42 my ($self, $instance_structure) = @_;
43 bless $instance_structure, $self->{meta}->name;
2d711cc8 44}
45
46# operations on meta instance
47
eb49acde 48sub get_all_slots {
49 my $self = shift;
49c93440 50 return @{$self->{slots}};
839ea973 51}
52
2d711cc8 53# operations on created instances
54
839ea973 55sub get_slot_value {
2bab2be6 56 my ($self, $instance, $slot_name) = @_;
57 return $instance->{$slot_name};
839ea973 58}
59
2bab2be6 60sub set_slot_value {
61 my ($self, $instance, $slot_name, $value) = @_;
62 $instance->{$slot_name} = $value;
63}
64
2d711cc8 65sub initialize_slot {
49c93440 66 my ($self, $instance, $slot_name) = @_;
67 $instance->{$slot_name} = undef;
2d711cc8 68}
69
c174112e 70sub initialize_all_slots {
71 my ($self, $instance) = @_;
72 foreach my $slot_name ($self->get_all_slots) {
73 $self->initialize_slot($instance, $slot_name);
74 }
75}
76
49c93440 77sub is_slot_initialized {
78 my ($self, $instance, $slot_name, $value) = @_;
2d711cc8 79 exists $instance->{$slot_name} ? 1 : 0;
2bab2be6 80}
839ea973 81
24869f62 821;
83
84__END__
85
86=pod
87
88=head1 NAME
89
90Class::MOP::Instance - Instance Meta Object
91
92=head1 SYNOPSIS
93
94=head1 DESCRIPTION
95
96=head1 METHODS
97
98=over 4
99
100=item B<new>
101
58287a97 102=item B<create_instance>
103
0e76a376 104=item B<bless_instance_structure>
105
49c93440 106=item B<get_all_slots>
839ea973 107
0e76a376 108=item B<initialize_all_slots>
109
839ea973 110=item B<get_slot_value>
111
49c93440 112=item B<set_slot_value>
58287a97 113
114=item B<initialize_slot>
115
49c93440 116=item B<is_slot_initialized>
58287a97 117
24869f62 118=back
119
120=head2 Introspection
121
122=over 4
123
124=item B<meta>
125
126This will return a B<Class::MOP::Class> instance which is related
127to this class.
128
129=back
130
131=head1 AUTHOR
132
133Stevan Little E<lt>stevan@iinteractive.comE<gt>
134
135=head1 COPYRIGHT AND LICENSE
136
137Copyright 2006 by Infinity Interactive, Inc.
138
139L<http://www.iinteractive.com>
140
141This library is free software; you can redistribute it and/or modify
142it under the same terms as Perl itself.
143
84ef30d1 144=cut