Tests and implementation for Undef/Defined types
[gitmo/Mouse.git] / lib / Mouse / Class.pm
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2package Mouse::Class;
3use strict;
4use warnings;
5
6use MRO::Compat;
7
8do {
9 my %METACLASS_CACHE;
10 sub initialize {
11 my $class = shift;
12 my $name = shift;
13 $METACLASS_CACHE{$name} = $class->new(name => $name)
14 if !exists($METACLASS_CACHE{$name});
15 return $METACLASS_CACHE{$name};
16 }
17};
18
19sub new {
20 my $class = shift;
21 my %args = @_;
22
23 $args{attributes} = {};
24 $args{superclasses} = do {
25 no strict 'refs';
26 \@{ $args{name} . '::ISA' };
27 };
28
29 bless \%args, $class;
30}
31
32sub name { $_[0]->{name} }
33
34sub superclasses {
35 my $self = shift;
36
37 if (@_) {
38 Mouse::load_class($_) for @_;
39 @{ $self->{superclasses} } = @_;
40 }
41
42 @{ $self->{superclasses} };
43}
44
45sub add_attribute {
46 my $self = shift;
47 my $attr = shift;
48
49 $self->{'attributes'}{$attr->name} = $attr;
50}
51
52sub attributes { values %{ $_[0]->{'attributes'} } }
53sub get_attribute_map { $_[0]->{attributes} }
54sub get_attribute { $_[0]->{attributes}->{$_[1]} }
55
56sub linearized_isa { @{ mro::get_linear_isa($_[0]->name) } }
57
581;
59
60__END__
61
62=head1 NAME
63
64Mouse::Class - hook into the Mouse MOP
65
66=head1 METHODS
67
68=head2 initialize ClassName -> Mouse::Class
69
70Finds or creates a Mouse::Class instance for the given ClassName. Only one
71instance should exist for a given class.
72
73=head2 new %args -> Mouse::Class
74
75Creates a new Mouse::Class. Don't call this directly.
76
77=head2 name -> ClassName
78
79Returns the name of the owner class.
80
81=head2 superclasses -> [ClassName]
82
83Gets (or sets) the list of superclasses of the owner class.
84
85=head2 add_attribute Mouse::Attribute
86
87Begins keeping track of the existing L<Mouse::Attribute> for the owner class.
88
89=head2 attributes -> [Mouse::Attribute]
90
91Returns a list of L<Mouse::Attribute> objects.
92
93=head2 get_attribute_map -> { name => Mouse::Attribute }
94
95Returns a mapping of attribute names to their corresponding
96L<Mouse::Attribute> objects.
97
98=head2 get_attribute Name -> Mouse::Attribute | undef
99
100Returns the L<Mouse::Attribute> with the given name.
101
102=head2 linearized_isa -> [ClassNames]
103
104Returns the list of classes in method dispatch order, with duplicates removed.
105
106=cut
107