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