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