types are no string, you can export if you want
[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                     if (reftype($options{isa}) && reftype($options{isa}) eq 'CODE') {
81                                 $options{type_constraint} = $options{isa};
82                         }
83                         else {
84                 $options{type_constraint} = Moose::Util::TypeConstraints::find_type_constraint($options{isa});
85                         }
86                 }
87                 $meta->add_attribute($name, %options) 
88         });
89
90         # handle method modifers
91         $meta->alias_method('before' => subname 'Moose::before' => sub { 
92                 my $code = pop @_;
93                 $meta->add_before_method_modifier($_, $code) for @_; 
94         });
95         $meta->alias_method('after'  => subname 'Moose::after' => sub { 
96                 my $code = pop @_;
97                 $meta->add_after_method_modifier($_, $code) for @_;
98         });     
99         $meta->alias_method('around' => subname 'Moose::around' => sub { 
100                 my $code = pop @_;
101                 $meta->add_around_method_modifier($_, $code) for @_;    
102         });     
103
104         # make sure they inherit from Moose::Object
105         $meta->superclasses('Moose::Object')
106        unless $meta->superclasses();
107
108         # we recommend using these things 
109         # so export them for them
110         $meta->alias_method('confess' => \&Carp::confess);                      
111         $meta->alias_method('blessed' => \&Scalar::Util::blessed);                              
112 }
113
114 1;
115
116 __END__
117
118 =pod
119
120 =head1 NAME
121
122 Moose - Moose, it's the new Camel
123
124 =head1 SYNOPSIS
125
126   package Point;
127   use Moose;
128         
129   has 'x' => (isa => 'Int', is => 'rw');
130   has 'y' => (isa => 'Int', is => 'rw');
131   
132   sub clear {
133       my $self = shift;
134       $self->x(0);
135       $self->y(0);    
136   }
137   
138   package Point3D;
139   use Moose;
140   
141   extends 'Point';
142   
143   has 'z' => (isa => 'Int');
144   
145   after 'clear' => sub {
146       my $self = shift;
147       $self->{z} = 0;
148   };
149   
150 =head1 CAVEAT
151
152 This is a B<very> early release of this module, it still needs 
153 some fine tuning and B<lots> more documentation. I am adopting 
154 the I<release early and release often> approach with this module, 
155 so keep an eye on your favorite CPAN mirror!
156
157 =head1 DESCRIPTION
158
159 Moose is an extension of the Perl 5 object system. 
160
161 =head2 Another object system!?!?
162
163 Yes, I know there has been an explosion recently of new ways to 
164 build object's in Perl 5, most of them based on inside-out objects, 
165 and other such things. Moose is different because it is not a new 
166 object system for Perl 5, but instead an extension of the existing 
167 object system.
168
169 Moose is built on top of L<Class::MOP>, which is a metaclass system 
170 for Perl 5. This means that Moose not only makes building normal 
171 Perl 5 objects better, but it also provides the power of metaclass 
172 programming.
173
174 =head2 What does Moose stand for??
175
176 Moose doesn't stand for one thing in particular, however, if you 
177 want, here are a few of my favorites, feel free to contribute 
178 more :)
179
180 =over 4
181
182 =item Make Other Object Systems Envious
183
184 =item Makes Object Orientation So Easy
185
186 =item Makes Object Orientation Spiffy- Er  (sorry ingy)
187
188 =item Most Other Object Systems Emasculate
189
190 =item My Overcraft Overfilled (with) Some Eels
191
192 =item Moose Often Ovulate Sorta Early
193
194 =item Many Overloaded Object Systems Exists 
195
196 =item Moose Offers Often Super Extensions
197
198 =back
199
200 =head1 ACKNOWLEDGEMENTS
201
202 =over 4
203
204 =item I blame Sam Vilain for giving me my first hit of meta-model crack.
205
206 =item I blame Audrey Tang for encouraging that meta-crack habit in #perl6.
207
208 =item Without the love and encouragement of Yuval "nothingmuch" Kogman, 
209 this module would not be possible (and it wouldn't have a name).
210
211 =item The basis of the TypeContraints module was Rob Kinyon's idea 
212 originally, I just ran with it.
213
214 =back
215
216 =head1 BUGS
217
218 All complex software has bugs lurking in it, and this module is no 
219 exception. If you find a bug please either email me, or add the bug
220 to cpan-RT.
221
222 =head1 AUTHOR
223
224 Stevan Little E<lt>stevan@iinteractive.comE<gt>
225
226 =head1 COPYRIGHT AND LICENSE
227
228 Copyright 2006 by Infinity Interactive, Inc.
229
230 L<http://www.iinteractive.com>
231
232 This library is free software; you can redistribute it and/or modify
233 it under the same terms as Perl itself. 
234
235 =cut