stuff
[gitmo/Moose.git] / lib / Moose.pm
CommitLineData
fcd84ca9 1
2package Moose;
3
4use strict;
5use warnings;
6
7our $VERSION = '0.01';
8
cc65ead0 9use Scalar::Util 'blessed', 'reftype';
fcd84ca9 10use Carp 'confess';
bc1e29b5 11use Sub::Name 'subname';
fcd84ca9 12
c0e30cf5 13use Moose::Meta::Class;
505c6fac 14use Moose::Meta::SafeMixin;
c0e30cf5 15use Moose::Meta::Attribute;
16
fcd84ca9 17use Moose::Object;
484ff7bf 18use Moose::Util::TypeConstraints ':no_export';
a15dff8d 19
505c6fac 20# bootstrap the mixin module
21Moose::Meta::SafeMixin::mixin(Moose::Meta::Class->meta, 'Moose::Meta::SafeMixin');
22
fcd84ca9 23sub import {
24 shift;
25 my $pkg = caller();
26
a15dff8d 27 Moose::Util::TypeConstraints->import($pkg);
28
fcd84ca9 29 my $meta;
30 if ($pkg->can('meta')) {
31 $meta = $pkg->meta();
32 (blessed($meta) && $meta->isa('Class::MOP::Class'))
33 || confess "Whoops, not møøsey enough";
34 }
35 else {
c0e30cf5 36 $meta = Moose::Meta::Class->initialize($pkg => (
37 ':attribute_metaclass' => 'Moose::Meta::Attribute'
e522431d 38 ));
39 $meta->add_method('meta' => sub {
40 # re-initialize so it inherits properly
41 Moose::Meta::Class->initialize($pkg => (
42 ':attribute_metaclass' => 'Moose::Meta::Attribute'
43 ));
44 })
fcd84ca9 45 }
ad1ac1bd 46
bc1e29b5 47 # NOTE:
48 # &alias_method will install the method, but it
49 # will not name it with
50
51 # handle superclasses
52 $meta->alias_method('extends' => subname 'Moose::extends' => sub { $meta->superclasses(@_) });
53
505c6fac 54 # handle mixins
55 $meta->alias_method('with' => subname 'Moose::with' => sub { $meta->mixin($_[0]) });
56
c0e30cf5 57 # handle attributes
29db16a9 58 $meta->alias_method('has' => subname 'Moose::has' => sub {
59 my ($name, %options) = @_;
60 if (exists $options{is}) {
cc65ead0 61 if ($options{is} eq 'ro') {
62 $options{reader} = $name;
63 }
64 elsif ($options{is} eq 'rw') {
65 $options{accessor} = $name;
66 }
29db16a9 67 }
cc65ead0 68 if (exists $options{isa}) {
69 if (reftype($options{isa}) && reftype($options{isa}) eq 'CODE') {
70 $options{type_constraint} = $options{isa};
71 }
72 else {
73 $options{type_constraint} = Moose::Util::TypeConstraints::subtype(
74 Object => Moose::Util::TypeConstraints::where { $_->isa($options{isa}) }
75 );
76 }
29db16a9 77 }
78 $meta->add_attribute($name, %options)
79 });
3c7278fb 80
c0e30cf5 81 # handle method modifers
bc1e29b5 82 $meta->alias_method('before' => subname 'Moose::before' => sub {
e5ebe4ce 83 my $code = pop @_;
84 $meta->add_before_method_modifier($_, $code) for @_;
85 });
bc1e29b5 86 $meta->alias_method('after' => subname 'Moose::after' => sub {
e5ebe4ce 87 my $code = pop @_;
88 $meta->add_after_method_modifier($_, $code) for @_;
89 });
bc1e29b5 90 $meta->alias_method('around' => subname 'Moose::around' => sub {
c0e30cf5 91 my $code = pop @_;
92 $meta->add_around_method_modifier($_, $code) for @_;
93 });
e5ebe4ce 94
c0e30cf5 95 # make sure they inherit from Moose::Object
fcd84ca9 96 $meta->superclasses('Moose::Object')
97 unless $meta->superclasses();
ad1ac1bd 98
c0e30cf5 99 # we recommend using these things
100 # so export them for them
ad1ac1bd 101 $meta->alias_method('confess' => \&confess);
102 $meta->alias_method('blessed' => \&blessed);
fcd84ca9 103}
104
1051;
106
107__END__
108
109=pod
110
111=head1 NAME
112
e522431d 113Moose - Moose, it's the new Camel
fcd84ca9 114
115=head1 SYNOPSIS
e522431d 116
117 package Point;
118 use Moose;
119
120 has 'x' => (isa => Int(), is => 'rw');
121 has 'y' => (isa => Int(), is => 'rw');
122
123 sub clear {
124 my $self = shift;
125 $self->x(0);
126 $self->y(0);
127 }
128
129 package Point3D;
130 use Moose;
131
132 extends 'Point';
09fdc1dc 133
e522431d 134 has 'z' => (isa => Int());
135
136 after 'clear' => sub {
137 my $self = shift;
138 $self->{z} = 0;
139 };
140
141=head1 CAVEAT
142
143This is a B<very> early release of this module, it still needs
144some fine tuning and B<lots> more documentation. I am adopting
145the I<release early and release often> approach with this module,
146so keep an eye on your favorite CPAN mirror!
147
fcd84ca9 148=head1 DESCRIPTION
149
e522431d 150Moose is an extension of the Perl 5 object system.
151
152=head2 Another object system!?!?
fcd84ca9 153
e522431d 154Yes, I know there has been an explosion recently of new ways to
155build object's in Perl 5, most of them based on inside-out objects,
156and other such things. Moose is different because it is not a new
157object system for Perl 5, but instead an extension of the existing
158object system.
3c7278fb 159
e522431d 160Moose is built on top of L<Class::MOP>, which is a metaclass system
161for Perl 5. This means that Moose not only makes building normal
505c6fac 162Perl 5 objects better, but it also provides the power of metaclass
163programming.
e522431d 164
165=head2 What does Moose stand for??
166
167Moose doesn't stand for one thing in particular, however, if you
168want, here are a few of my favorites, feel free to contribute
169more :)
170
171=over 4
172
173=item Makes Other Object Systems Envious
174
175=item Makes Object Orientation So Easy
176
505c6fac 177=item Makes Object Orientation Sound Easy
178
179=item Makes Object Orientation Spiffy- Er
180
181=item My Overcraft Overfilled (with) Some Eels
182
183=item Moose Often Ovulate Sorta Early
184
185=item Most Other Object Systems Emasculate
186
187=item Many Overloaded Object Systems Exists
188
189=item Moose Offers Often Super Extensions
190
e522431d 191=back
3c7278fb 192
fcd84ca9 193=head1 BUGS
194
195All complex software has bugs lurking in it, and this module is no
196exception. If you find a bug please either email me, or add the bug
197to cpan-RT.
198
fcd84ca9 199=head1 AUTHOR
200
201Stevan Little E<lt>stevan@iinteractive.comE<gt>
202
203=head1 COPYRIGHT AND LICENSE
204
205Copyright 2006 by Infinity Interactive, Inc.
206
207L<http://www.iinteractive.com>
208
209This library is free software; you can redistribute it and/or modify
210it under the same terms as Perl itself.
211
212=cut