Bump to 0.07
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
c3398f5b 1#!/usr/bin/env perl
306290e8 2package Mouse::Meta::Class;
c3398f5b 3use strict;
4use warnings;
5
72b88a88 6use Scalar::Util 'blessed';
7a59f4e8 7use Carp 'confess';
72b88a88 8
c3398f5b 9use MRO::Compat;
10
11do {
12 my %METACLASS_CACHE;
72b88a88 13
14 # because Mouse doesn't introspect existing classes, we're forced to
15 # only pay attention to other Mouse classes
16 sub _metaclass_cache {
17 my $class = shift;
18 my $name = shift;
19 return $METACLASS_CACHE{$name};
20 }
21
c3398f5b 22 sub initialize {
23 my $class = shift;
24 my $name = shift;
25 $METACLASS_CACHE{$name} = $class->new(name => $name)
26 if !exists($METACLASS_CACHE{$name});
27 return $METACLASS_CACHE{$name};
28 }
29};
30
31sub new {
32 my $class = shift;
6cbacbf6 33 my %args = @_;
c3398f5b 34
35 $args{attributes} = {};
36 $args{superclasses} = do {
37 no strict 'refs';
38 \@{ $args{name} . '::ISA' };
39 };
40
41 bless \%args, $class;
42}
43
44sub name { $_[0]->{name} }
45
46sub superclasses {
47 my $self = shift;
48
49 if (@_) {
50 Mouse::load_class($_) for @_;
51 @{ $self->{superclasses} } = @_;
52 }
53
54 @{ $self->{superclasses} };
55}
56
d16fe7d7 57sub add_method {
58 my $self = shift;
59 my $name = shift;
60 my $code = shift;
61
62 my $pkg = $self->name;
63
64 no strict 'refs';
65 *{ $pkg . '::' . $name } = $code;
66}
67
c3398f5b 68sub add_attribute {
69 my $self = shift;
70 my $attr = shift;
71
72 $self->{'attributes'}{$attr->name} = $attr;
73}
74
72b88a88 75sub compute_all_applicable_attributes {
76 my $self = shift;
77 my (@attr, %seen);
78
79 for my $class ($self->linearized_isa) {
80 my $meta = $self->_metaclass_cache($class)
81 or next;
82
83 for my $name (keys %{ $meta->get_attribute_map }) {
84 next if $seen{$name}++;
85 push @attr, $meta->get_attribute($name);
86 }
87 }
88
89 return @attr;
90}
91
c3398f5b 92sub get_attribute_map { $_[0]->{attributes} }
66eea168 93sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
c3398f5b 94sub get_attribute { $_[0]->{attributes}->{$_[1]} }
95
96sub linearized_isa { @{ mro::get_linear_isa($_[0]->name) } }
97
7a59f4e8 98sub clone_object {
99 my $class = shift;
100 my $instance = shift;
101
102 (blessed($instance) && $instance->isa($class->name))
1a0f0802 103 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
7a59f4e8 104
105 $class->clone_instance($instance, @_);
106}
107
108sub clone_instance {
109 my ($class, $instance, %params) = @_;
110
111 (blessed($instance))
e42bee44 112 || confess "You can only clone instances, ($instance) is not a blessed instance";
7a59f4e8 113
114 my $clone = bless { %$instance }, ref $instance;
115
116 foreach my $attr ($class->compute_all_applicable_attributes()) {
117 if ( defined( my $init_arg = $attr->init_arg ) ) {
118 if (exists $params{$init_arg}) {
119 $clone->{ $attr->name } = $params{$init_arg};
120 }
121 }
122 }
123
124 return $clone;
125
126}
127
60e2164a 128sub make_immutable {}
7a59f4e8 129
c3398f5b 1301;
131
132__END__
133
134=head1 NAME
135
306290e8 136Mouse::Meta::Class - hook into the Mouse MOP
c3398f5b 137
138=head1 METHODS
139
306290e8 140=head2 initialize ClassName -> Mouse::Meta::Class
c3398f5b 141
306290e8 142Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
143one instance should exist for a given class.
c3398f5b 144
306290e8 145=head2 new %args -> Mouse::Meta::Class
c3398f5b 146
306290e8 147Creates a new Mouse::Meta::Class. Don't call this directly.
c3398f5b 148
149=head2 name -> ClassName
150
151Returns the name of the owner class.
152
153=head2 superclasses -> [ClassName]
154
155Gets (or sets) the list of superclasses of the owner class.
156
306290e8 157=head2 add_attribute Mouse::Meta::Attribute
c3398f5b 158
306290e8 159Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
160class.
c3398f5b 161
72b88a88 162=head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
163
164Returns the list of all L<Mouse::Meta::Attribute> instances associated with
165this class and its superclasses.
166
306290e8 167=head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
c3398f5b 168
169Returns a mapping of attribute names to their corresponding
306290e8 170L<Mouse::Meta::Attribute> objects.
c3398f5b 171
66eea168 172=head2 has_attribute Name -> Boool
173
174Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
175
306290e8 176=head2 get_attribute Name -> Mouse::Meta::Attribute | undef
c3398f5b 177
306290e8 178Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 179
180=head2 linearized_isa -> [ClassNames]
181
182Returns the list of classes in method dispatch order, with duplicates removed.
183
f7b11a21 184=head2 clone_object Instance -> Instance
185
186Clones the given C<Instance> which must be an instance governed by this
187metaclass.
188
189=head2 clone_instance Instance, Parameters -> Instance
190
191Clones the given C<Instance> and sets any additional parameters.
192
c3398f5b 193=cut
194