up
[gitmo/Moose.git] / lib / Moose.pm
CommitLineData
fcd84ca9 1
5569c072 2use lib '/Users/stevan/Projects/CPAN/Class-MOP/Class-MOP/lib';
3
fcd84ca9 4package Moose;
5
6use strict;
7use warnings;
8
b6aed8b0 9our $VERSION = '0.02';
fcd84ca9 10
cc65ead0 11use Scalar::Util 'blessed', 'reftype';
fcd84ca9 12use Carp 'confess';
bc1e29b5 13use Sub::Name 'subname';
fcd84ca9 14
7f18097c 15use UNIVERSAL::require;
16
ef1d5f4b 17use Class::MOP;
18
c0e30cf5 19use Moose::Meta::Class;
20use Moose::Meta::Attribute;
21
fcd84ca9 22use Moose::Object;
484ff7bf 23use Moose::Util::TypeConstraints ':no_export';
a15dff8d 24
fcd84ca9 25sub import {
26 shift;
27 my $pkg = caller();
28
fc5609d2 29 # we should never export to main
30 return if $pkg eq 'main';
31
a15dff8d 32 Moose::Util::TypeConstraints->import($pkg);
182134e8 33
34 # make a subtype for each Moose class
35 Moose::Util::TypeConstraints::subtype($pkg
36 => Moose::Util::TypeConstraints::as Object
37 => Moose::Util::TypeConstraints::where { $_->isa($pkg) }
38 );
5569c072 39
fcd84ca9 40 my $meta;
41 if ($pkg->can('meta')) {
42 $meta = $pkg->meta();
43 (blessed($meta) && $meta->isa('Class::MOP::Class'))
44 || confess "Whoops, not møøsey enough";
45 }
46 else {
c0e30cf5 47 $meta = Moose::Meta::Class->initialize($pkg => (
48 ':attribute_metaclass' => 'Moose::Meta::Attribute'
e522431d 49 ));
50 $meta->add_method('meta' => sub {
51 # re-initialize so it inherits properly
52 Moose::Meta::Class->initialize($pkg => (
53 ':attribute_metaclass' => 'Moose::Meta::Attribute'
54 ));
55 })
fcd84ca9 56 }
ad1ac1bd 57
bc1e29b5 58 # NOTE:
59 # &alias_method will install the method, but it
60 # will not name it with
61
62 # handle superclasses
7f18097c 63 $meta->alias_method('extends' => subname 'Moose::extends' => sub {
64 $_->require for @_;
65 $meta->superclasses(@_)
5e030bec 66 });
505c6fac 67
c0e30cf5 68 # handle attributes
29db16a9 69 $meta->alias_method('has' => subname 'Moose::has' => sub {
70 my ($name, %options) = @_;
71 if (exists $options{is}) {
cc65ead0 72 if ($options{is} eq 'ro') {
73 $options{reader} = $name;
74 }
75 elsif ($options{is} eq 'rw') {
76 $options{accessor} = $name;
77 }
29db16a9 78 }
cc65ead0 79 if (exists $options{isa}) {
e90c03d0 80 # allow for anon-subtypes here ...
182134e8 81 if (reftype($options{isa}) && reftype($options{isa}) eq 'CODE') {
cc65ead0 82 $options{type_constraint} = $options{isa};
83 }
84 else {
e90c03d0 85 # otherwise assume it is a constraint
86 my $constraint = Moose::Util::TypeConstraints::find_type_constraint($options{isa});
87 # if the constraing it not found ....
88 unless (defined $constraint) {
89 # assume it is a foreign class, and make
90 # an anon constraint for it
91 $constraint = Moose::Util::TypeConstraints::subtype(
92 Object => Moose::Util::TypeConstraints::where { $_->isa($constraint) }
93 );
94 }
95 $options{type_constraint} = $constraint;
cc65ead0 96 }
29db16a9 97 }
4b598ea3 98 if (exists $options{coerce} && $options{coerce} && $options{isa}) {
99 my $coercion = Moose::Util::TypeConstraints::find_type_coercion($options{isa});
100 (defined $coercion)
101 || confess "Cannot find coercion for type " . $options{isa};
102 $options{coerce} = $coercion;
103 }
29db16a9 104 $meta->add_attribute($name, %options)
105 });
3c7278fb 106
c0e30cf5 107 # handle method modifers
bc1e29b5 108 $meta->alias_method('before' => subname 'Moose::before' => sub {
e5ebe4ce 109 my $code = pop @_;
110 $meta->add_before_method_modifier($_, $code) for @_;
111 });
bc1e29b5 112 $meta->alias_method('after' => subname 'Moose::after' => sub {
e5ebe4ce 113 my $code = pop @_;
fc5609d2 114 $meta->add_after_method_modifier($_, $code) for @_;
e5ebe4ce 115 });
bc1e29b5 116 $meta->alias_method('around' => subname 'Moose::around' => sub {
c0e30cf5 117 my $code = pop @_;
fc5609d2 118 $meta->add_around_method_modifier($_, $code) for @_;
c0e30cf5 119 });
5569c072 120
c0e30cf5 121 # make sure they inherit from Moose::Object
5569c072 122 $meta->superclasses('Moose::Object')
123 unless $meta->superclasses();
ad1ac1bd 124
c0e30cf5 125 # we recommend using these things
126 # so export them for them
5569c072 127 $meta->alias_method('confess' => \&Carp::confess);
128 $meta->alias_method('blessed' => \&Scalar::Util::blessed);
fcd84ca9 129}
130
1311;
132
133__END__
134
135=pod
136
137=head1 NAME
138
e522431d 139Moose - Moose, it's the new Camel
fcd84ca9 140
141=head1 SYNOPSIS
e522431d 142
143 package Point;
144 use Moose;
145
182134e8 146 has 'x' => (isa => 'Int', is => 'rw');
147 has 'y' => (isa => 'Int', is => 'rw');
e522431d 148
149 sub clear {
150 my $self = shift;
151 $self->x(0);
152 $self->y(0);
153 }
154
155 package Point3D;
156 use Moose;
157
158 extends 'Point';
09fdc1dc 159
182134e8 160 has 'z' => (isa => 'Int');
e522431d 161
162 after 'clear' => sub {
163 my $self = shift;
164 $self->{z} = 0;
165 };
166
167=head1 CAVEAT
168
169This is a B<very> early release of this module, it still needs
170some fine tuning and B<lots> more documentation. I am adopting
171the I<release early and release often> approach with this module,
172so keep an eye on your favorite CPAN mirror!
173
fcd84ca9 174=head1 DESCRIPTION
175
e522431d 176Moose is an extension of the Perl 5 object system.
177
178=head2 Another object system!?!?
fcd84ca9 179
e522431d 180Yes, I know there has been an explosion recently of new ways to
181build object's in Perl 5, most of them based on inside-out objects,
182and other such things. Moose is different because it is not a new
183object system for Perl 5, but instead an extension of the existing
184object system.
3c7278fb 185
e522431d 186Moose is built on top of L<Class::MOP>, which is a metaclass system
187for Perl 5. This means that Moose not only makes building normal
505c6fac 188Perl 5 objects better, but it also provides the power of metaclass
189programming.
e522431d 190
191=head2 What does Moose stand for??
192
193Moose doesn't stand for one thing in particular, however, if you
194want, here are a few of my favorites, feel free to contribute
195more :)
196
197=over 4
198
5569c072 199=item Make Other Object Systems Envious
e522431d 200
201=item Makes Object Orientation So Easy
202
5569c072 203=item Makes Object Orientation Spiffy- Er (sorry ingy)
505c6fac 204
5569c072 205=item Most Other Object Systems Emasculate
505c6fac 206
207=item My Overcraft Overfilled (with) Some Eels
208
209=item Moose Often Ovulate Sorta Early
210
505c6fac 211=item Many Overloaded Object Systems Exists
212
213=item Moose Offers Often Super Extensions
214
e522431d 215=back
3c7278fb 216
5569c072 217=head1 ACKNOWLEDGEMENTS
218
219=over 4
220
221=item I blame Sam Vilain for giving me my first hit of meta-model crack.
222
223=item I blame Audrey Tang for encouraging that meta-crack habit in #perl6.
224
225=item Without the love and encouragement of Yuval "nothingmuch" Kogman,
226this module would not be possible (and it wouldn't have a name).
227
228=item The basis of the TypeContraints module was Rob Kinyon's idea
229originally, I just ran with it.
230
231=back
232
e90c03d0 233=head1 SEE ALSO
234
235=over 4
236
237=item L<http://forum2.org/moose/>
238
239=back
240
fcd84ca9 241=head1 BUGS
242
243All complex software has bugs lurking in it, and this module is no
244exception. If you find a bug please either email me, or add the bug
245to cpan-RT.
246
fcd84ca9 247=head1 AUTHOR
248
249Stevan Little E<lt>stevan@iinteractive.comE<gt>
250
251=head1 COPYRIGHT AND LICENSE
252
253Copyright 2006 by Infinity Interactive, Inc.
254
255L<http://www.iinteractive.com>
256
257This library is free software; you can redistribute it and/or modify
258it under the same terms as Perl itself.
259
260=cut