1 package Reaction::Class;
6 use Reaction::Types::Core;
9 sub exporter_for_package {
10 my ($self, $package) = @_;
11 my %exports_proto = $self->exports_for_package($package);
13 map { my $cr = $exports_proto{$_}; ($_, sub { Sub::Name::subname "${self}::$_" => $cr; }) }
17 my $exporter = Sub::Exporter::build_exporter({
28 my ($self, $pkg, $args) = @_;
29 my $exporter = $self->exporter_for_package($pkg, $args);
30 $exporter->($self, { into => $pkg }, @$args);
31 if (my @default_base = $self->default_base) {
33 @{"${pkg}::ISA"} = @default_base unless @{"${pkg}::ISA"};
37 sub default_base { ('Reaction::Object'); }
39 sub exports_for_package {
40 my ($self, $package) = @_;
42 set_or_lazy_build => sub {
44 my $build = "build_${name}";
45 return (required => 1, lazy => 1,
46 default => sub { shift->$build(); });
48 set_or_lazy_fail => sub {
50 my $message = "${name} must be provided before calling reader";
51 return (required => 1, lazy => 1,
52 default => sub { confess($message); });
54 trigger_adopt => sub {
57 my $adopt = "adopt_${type}";
58 return (trigger => sub { shift->$adopt(@args); });
60 register_inc_entry => sub {
66 #this needs to go away soon. its never used. pollution.
67 reflect_attributes_from => sub {
68 my ($from_class, @attrs) = @_;
70 #Should we use Class::Inspector to make sure class is loaded?
71 #unless( Class::Inspector->loaded($from_class) ){
72 # eval "require $from_class" || die("Failed to load: $from_class");
74 foreach my $attr_name (@attrs){
75 my $from_attr = $from_class->meta->get_attribute($attr_name);
76 confess("$from_attr does not exist in $from_class")
79 #$package->meta->add_attribute( $from_attr->name, %{$from_attr} );
80 $package->meta->add_attribute( bless { %{$from_attr} } =>
81 $package->meta->attribute_metaclass );
85 $self->do_class_sub($package, @_);
88 $package->can('with')->(@_);
91 $package->can('override')->(@_)
93 $self->make_package_sub($package),
94 implements => sub { confess "implements only valid within class block" },
95 $self->make_sugar_sub('is'),
96 $self->make_code_sugar_sub('which'),
97 $self->make_code_sugar_sub('as'),
98 run => sub (;&@) { @_ },
103 my ($self, $package, $class, @args) = @_;
104 my $error = "Invalid class declaration, should be: class Class (is Superclass)*, which { ... }";
105 confess $error if (@args % 1);
108 my $should_be_is = shift(@args);
109 confess $error unless $should_be_is eq 'is';
110 push(@supers, shift(@args));
112 confess $error unless $args[0] eq 'which' && ref($args[1]) eq 'CODE';
113 my $setup = $args[1];
115 #this eval is fucked, but I can't fix it
116 unless ($class->can('meta')) {
117 print STDERR "** MAKING CLASS $class useing Reaction::Class **\n";
118 eval "package ${class}; use Reaction::Class;";
119 if ($@) { confess "Couldn't make ${class} a Reaction class: $@"; }
122 Class::MOP::load_class($_) for @supers;
123 $class->meta->_fix_metaclass_incompatability(@supers);
124 $class->meta->superclasses(@supers);
126 $self->setup_and_cleanup($package, $setup);
129 #print STDERR "$package \n";
130 #print STDERR $package->meta->blessed, " \n";
131 $package->meta->make_immutable;
132 # (inline_accessor => 0, inline_destructor => 0,inline_constructor => 0,);
135 sub setup_and_cleanup {
136 my ($self, $package, $setup) = @_;
142 no warnings 'redefine';
143 local *{"${package}::implements"} =
144 Sub::Name::subname "${self}::implements" => sub {
146 shift if $_[0] eq 'as';
147 push(@methods, [ $name, shift ]);
149 foreach my $meth ($self->delayed_methods) {
150 $save_delayed{$meth} = $package->can($meth);
151 local *{"${package}::${meth}"} =
152 Sub::Name::subname "${self}::${meth}" => sub {
153 push(@apply_after, [ $meth => @_ ]);
156 # XXX - need additional fuckery to handle multi-class-per-file
157 $setup->(); # populate up the crap
159 my %exports = $self->exports_for_package($package);
162 foreach my $nuke (keys %exports) {
163 delete ${"${package}::"}{$nuke};
166 my $unimport_class = $self->next_import_package;
167 eval "package ${package}; no $unimport_class;";
168 confess "$unimport_class unimport from ${package} failed: $@" if $@;
169 foreach my $m (@methods) {
170 $package->meta->add_method(@$m);
172 foreach my $a (@apply_after) {
173 my $call = shift(@$a);
174 $save_delayed{$call}->(@$a);
178 sub delayed_methods {
179 return (qw/has with extends before after around override augment/);
182 sub make_package_sub {
183 my ($self, $package) = @_;
184 my ($last) = (split('::', $package))[-1];
185 return $last => sub {
186 $self->do_package_sub($package => @_);
193 return (@_ ? ($package => @_) : $package);
197 my ($self, $name) = @_;
198 return $name => sub {
199 return ($name => @_);
203 sub make_code_sugar_sub {
204 my ($self, $name) = @_;
205 return $name => sub (;&@) {
206 return ($name => @_);
216 $self->do_import($pkg, \@args);
217 goto &{$self->next_import};
221 return shift->next_import_package(@_)->can('import');
224 sub next_import_package { 'Moose' }
228 #---------#---------#---------#---------#---------#---------#---------#--------#
242 =item * L<Reaction::Manual>
246 =head1 Unstructured reminders
248 (will properly format and stuff later. no time right now)
250 C<use>ing C<Reaction::Class> will alias the current package name
253 package MyApp::Pretty::Picture
255 # Picture expands to 'MyApp::Pretty::Picture'
256 class Picture, which { ...
260 =head2 set_or_lazy_build $attrname
262 Will make your attributes lazy and required, if they are not set they
263 will default to the value returned by C<&build_$attrname>
265 has created_d => (isa => 'DateTime', set_or_lazy_build('created_d') );
266 sub build_created_d{ DateTime->now }
268 =head2 set_or_lazy_fail $attrname
270 Will make your attributes lazy and required, if they are not set
271 and their accessor is called an exception will be thrown
273 =head2 trigger_adopt $attrname
275 =head2 register_inc_entry
277 =head2 reflect_attributes_from $from_class, @attrs
279 Create attributes in the local class that mirror the specified C<@attrs>
282 =head2 class $name [, is $superclass ], which {
284 Sugary class declaration, will create a a package C<$name> with an
285 optional base class of $superclass. The class declaration, should be placed inside
286 the brackets using C<implements> to declare a method and C<has> to declare an
291 Alias to C<with> for the current package, see C<Moose::Role>
293 =head2 implements $method_name [is | which | as]
295 Only valid whithin a class block, allows you to declare a method for the class.
297 implements 'current_date' => as { DateTime->today };
305 =item * Matt S. Trout
307 =item * K. J. Cheetham
309 =item * Guillermo Roditi
311 =item * Jess Robinson (Documentation)
313 =item * Kaare Rasmussen (Documentation)
315 =item * Andres N. Kievsky (Documentation)
321 This library is free software, you can redistribute it and/or modify
322 it under the same terms as Perl itself.