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