X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Singleton.git;a=blobdiff_plain;f=lib%2FMooseX%2FSingleton%2FRole%2FMeta%2FMethod%2FConstructor.pm;h=63c2888774da92699c3522cbdb6dbad8c008efe6;hp=414b28a55aa81d146c29009a96b1a3bf1a2149d7;hb=0a71b1e610530e80cb99ffdec7fca186090474d0;hpb=387bf3e0e7da14d605b8e5f7ee4909f3d15c56ee diff --git a/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm b/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm index 414b28a..63c2888 100644 --- a/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm +++ b/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm @@ -1,8 +1,12 @@ package MooseX::Singleton::Role::Meta::Method::Constructor; use Moose::Role; +our $VERSION = '0.25'; +$VERSION = eval $VERSION; + override _initialize_body => sub { my $self = shift; + # TODO: # the %options should also include a both # a call 'initializer' and call 'SUPER::' @@ -13,14 +17,18 @@ override _initialize_body => sub { my $source = 'sub {'; $source .= "\n" . 'my $class = shift;'; - $source .= "\n" . 'my $existing = do { no strict "refs"; no warnings "once"; \${"$class\::singleton"}; };'; + $source .= "\n" + . 'my $existing = do { no strict "refs"; no warnings "once"; \${"$class\::singleton"}; };'; $source .= "\n" . 'return ${$existing} if ${$existing};'; $source .= "\n" . 'return $class->Moose::Object::new(@_)'; - $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';'; + $source + .= "\n" + . ' if $class ne \'' + . $self->associated_metaclass->name . '\';'; - $source .= $self->_generate_params('$params', '$class'); - $source .= $self->_generate_instance('$instance', '$class'); + $source .= $self->_generate_params( '$params', '$class' ); + $source .= $self->_generate_instance( '$instance', '$class' ); $source .= $self->_generate_slot_initializers; $source .= ";\n" . $self->_generate_triggers(); @@ -32,25 +40,30 @@ override _initialize_body => sub { my $attrs = $self->_attributes; - my @type_constraints = map { - $_->can('type_constraint') ? $_->type_constraint : undef - } @$attrs; + my @type_constraints + = map { $_->can('type_constraint') ? $_->type_constraint : undef } + @$attrs; + + my @type_constraint_bodies + = map { defined $_ ? $_->_compiled_type_constraint : undef; } + @type_constraints; - my @type_constraint_bodies = map { - defined $_ ? $_->_compiled_type_constraint : undef; - } @type_constraints; + my $defaults = [map { $_->default } @$attrs]; my ( $code, $e ) = $self->_compile_code( - code => $source, + code => $source, environment => { - '$meta' => \$self, - '$attrs' => \$attrs, - '@type_constraints' => \@type_constraints, + '$meta' => \$self, + '$attrs' => \$attrs, + '$defaults' => \$defaults, + '@type_constraints' => \@type_constraints, '@type_constraint_bodies' => \@type_constraint_bodies, }, ); - $self->throw_error("Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e", error => $e, data => $source ) + $self->throw_error( + "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e", + error => $e, data => $source ) if $e; $self->{'body'} = $code; @@ -79,7 +92,7 @@ override _expected_method_class => sub { return $super_value; }; -no Moose; +no Moose::Role; 1;