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