Permalink
Browse files

Allow setup attribute option to contain constructor parameters.

Reflex's setup may either build the object or contain constructor
parameters as a hashref.  In the latter case, Reflex will build the
object itself.
  • Loading branch information...
1 parent e2c5e08 commit e0cf782b047d4da00a218268b973e1c31f41eb6b @rcaputo committed Sep 21, 2009
Showing with 36 additions and 11 deletions.
  1. +34 −9 docs/lib/Reflex/Role/Object.pm
  2. +1 −1 docs/lib/Reflex/Trait/Emitter.pm
  3. +1 −1 docs/lib/Reflex/Trait/Observer.pm
@@ -179,20 +179,45 @@ sub BUILD {
my ($self, $args) = @_;
foreach my $setup (
- (
- grep { $_->does('Reflex::Trait::Emitter') }
- $self->meta()->get_all_attributes()
- ),
- (
- grep { $_->does('Reflex::Trait::Observer') }
- $self->meta()->get_all_attributes()
- )
+ grep {
+ $_->does('Reflex::Trait::Emitter') || $_->does('Reflex::Trait::Observer')
+ }
+ $self->meta()->get_all_attributes()
) {
my $callback = $setup->setup();
- if (defined $callback) {
+ next unless defined $callback;
+
+ # TODO - Better way to detect CodeRef?
+ if (ref($callback) eq "CODE") {
my $member = $setup->name();
$self->$member( $callback->() ); # TODO - Proper parameters!
+ next;
}
+
+ # TODO - Better way to detect HashRef?
+ if (ref($callback) eq "HASH") {
+ my $member = $setup->name();
+
+ my @types = (
+ grep { $_ ne "Undef" }
+ split /\s*\|\s*/,
+ $setup->type_constraint()
+ );
+
+ croak "Hashref 'setup' can't determine the class from 'isa'" if (
+ @types < 1
+ );
+
+ croak "Hashref 'setup' can't set up more than one class from 'isa'" if (
+ @types > 1
+ );
+
+ my $type = $types[0];
+ $self->$member( $type->new($callback) );
+ next;
+ }
+
+ croak "Unknown 'setup' value: $callback";
}
foreach my $observer (@{$self->observers()}) {
@@ -74,7 +74,7 @@ has event => (
);
has setup => (
- isa => 'CodeRef',
+ isa => 'CodeRef|HashRef',
is => 'ro',
);
@@ -70,7 +70,7 @@ has role => (
);
has setup => (
- isa => 'CodeRef',
+ isa => 'CodeRef|HashRef',
is => 'ro',
);

0 comments on commit e0cf782

Please sign in to comment.