Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit e0cf782b047d4da00a218268b973e1c31f41eb6b 1 parent e2c5e08
@rcaputo authored
View
43 docs/lib/Reflex/Role/Object.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()}) {
View
2  docs/lib/Reflex/Trait/Emitter.pm
@@ -74,7 +74,7 @@ has event => (
);
has setup => (
- isa => 'CodeRef',
+ isa => 'CodeRef|HashRef',
is => 'ro',
);
View
2  docs/lib/Reflex/Trait/Observer.pm
@@ -70,7 +70,7 @@ has role => (
);
has setup => (
- isa => 'CodeRef',
+ isa => 'CodeRef|HashRef',
is => 'ro',
);
Please sign in to comment.
Something went wrong with that request. Please try again.