X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FSingleton%2FRole%2FMeta%2FMethod%2FConstructor.pm;h=f8f049e472bcd5580fa88f0095b2381b0fe6488f;hb=16a3d25b3eb827352f0e1ddb5727c9904e3d828e;hp=0521977dbddad007d7b73f71e65fc1dc983a2b23;hpb=2c50d2cd65874a9e70ef21d331e1151f71f10420;p=gitmo%2FMooseX-Singleton.git diff --git a/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm b/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm index 0521977..f8f049e 100644 --- a/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm +++ b/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm @@ -1,70 +1,73 @@ package MooseX::Singleton::Role::Meta::Method::Constructor; use Moose::Role; -our $VERSION = '0.24'; -$VERSION = eval $VERSION; -override _initialize_body => sub { - my $self = shift; - - # TODO: - # the %options should also include a both - # a call 'initializer' and call 'SUPER::' - # options, which should cover approx 90% - # of the possible use cases (even if it - # requires some adaption on the part of - # the author, after all, nothing is free) - my $source = 'sub {'; - $source .= "\n" . 'my $class = shift;'; - - $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 .= $self->_generate_params( '$params', '$class' ); - $source .= $self->_generate_instance( '$instance', '$class' ); - $source .= $self->_generate_slot_initializers; - - $source .= ";\n" . $self->_generate_triggers(); - $source .= ";\n" . $self->_generate_BUILDALL(); - - $source .= ";\n" . 'return ${$existing} = $instance'; - $source .= ";\n" . '}'; - warn $source if $self->options->{debug}; - - my $attrs = $self->_attributes; - - my @type_constraints - = map { $_->can('type_constraint') ? $_->type_constraint : undef } - @$attrs; - - my @type_constraint_bodies - = map { defined $_ ? $_->_compiled_type_constraint : undef; } - @type_constraints; - - my ( $code, $e ) = $self->_compile_code( - code => $source, - environment => { - '$meta' => \$self, - '$attrs' => \$attrs, - '@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 ) - if $e; - - $self->{'body'} = $code; -}; +if ( $Moose::VERSION < 1.9900 ) { + override _initialize_body => sub { + my $self = shift; + + # TODO: + # the %options should also include a both + # a call 'initializer' and call 'SUPER::' + # options, which should cover approx 90% + # of the possible use cases (even if it + # requires some adaption on the part of + # the author, after all, nothing is free) + my $source = 'sub {'; + $source .= "\n" . 'my $class = shift;'; + + $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 .= $self->_generate_params( '$params', '$class' ); + $source .= $self->_generate_instance( '$instance', '$class' ); + $source .= $self->_generate_slot_initializers; + + $source .= ";\n" . $self->_generate_triggers(); + $source .= ";\n" . $self->_generate_BUILDALL(); + + $source .= ";\n" . 'return ${$existing} = $instance'; + $source .= ";\n" . '}'; + warn $source if $self->options->{debug}; + + my $attrs = $self->_attributes; + + my @type_constraints + = map { $_->can('type_constraint') ? $_->type_constraint : undef } + @$attrs; + + 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, + environment => { + '$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 ) + if $e; + + $self->{'body'} = $code; + }; +} # Ideally we'd be setting this in the constructor, but the new() methods in # what the parent classes are not well-factored. @@ -93,14 +96,12 @@ no Moose::Role; 1; +# ABSTRACT: Constructor method role for MooseX::Singleton + __END__ =pod -=head1 NAME - -MooseX::Singleton::Role::Meta::Method::Constructor - Constructor method role for MooseX::Singleton - =head1 DESCRIPTION This role overrides the generated object C method so that it returns the