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