X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FObject.pm;h=4d1f5d467e3f15278f5a04abfb163b21f4ae32d7;hb=07b0f1a5599bf1a0ed99933200ca27290e9a468a;hp=e754eb46b5653c8a6cba412e697cf27936480532;hpb=f742dfef187b2d0f19826bcc31fdd9c64fd060c1;p=gitmo%2FMoose.git diff --git a/lib/Moose/Object.pm b/lib/Moose/Object.pm index e754eb4..4d1f5d4 100644 --- a/lib/Moose/Object.pm +++ b/lib/Moose/Object.pm @@ -4,55 +4,86 @@ package Moose::Object; use strict; use warnings; -use Moose::Meta::Class; -use metaclass 'Moose::Meta::Class'; +use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class'; +use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class'; use Carp 'confess'; -our $VERSION = '0.06'; +our $VERSION = '0.51'; +our $AUTHORITY = 'cpan:STEVAN'; sub new { my $class = shift; - my %params; + my $params = $class->BUILDARGS(@_); + my $self = $class->meta->new_object(%$params); + $self->BUILDALL($params); + return $self; +} + +sub BUILDARGS { + my $class = shift; + if (scalar @_ == 1) { - (ref($_[0]) eq 'HASH') - || confess "Single parameters to new() must be a HASH ref"; - %params = %{$_[0]}; - } - else { - %params = @_; + if (defined $_[0]) { + no warnings 'uninitialized'; + (ref($_[0]) eq 'HASH') + || confess "Single parameters to new() must be a HASH ref"; + return {%{$_[0]}}; + } else { + return {}; # FIXME this is compat behavior, but is it correct? + } + } else { + return {@_}; } - my $self = $class->meta->new_object(%params); - $self->BUILDALL(\%params); - return $self; } sub BUILDALL { - my ($self, $params) = @_; - foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) { - $method->{code}->($self, $params); - } + # NOTE: we ask Perl if we even + # need to do this first, to avoid + # extra meta level calls + return unless $_[0]->can('BUILD'); + my ($self, $params) = @_; + foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) { + $method->{code}->body->($self, $params); + } } sub DEMOLISHALL { - my $self = shift; - foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) { - $method->{code}->($self); - } + my $self = shift; + foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) { + $method->{code}->body->($self); + } } -sub DESTROY { goto &DEMOLISHALL } +sub DESTROY { + # NOTE: we ask Perl if we even + # need to do this first, to avoid + # extra meta level calls + return unless $_[0]->can('DEMOLISH'); + # if we have an exception here ... + if ($@) { + # localize the $@ ... + local $@; + # run DEMOLISHALL ourselves, ... + $_[0]->DEMOLISHALL; + # and return ... + return; + } + # otherwise it is normal destruction + $_[0]->DEMOLISHALL; +} # new does() methods will be created # as approiate see Moose::Meta::Role sub does { my ($self, $role_name) = @_; (defined $role_name) - || confess "You much supply a role name to does()"; + || confess "You must supply a role name to does()"; my $meta = $self->meta; foreach my $class ($meta->class_precedence_list) { + my $m = $meta->initialize($class); return 1 - if $meta->initialize($class)->does_role($role_name); + if $m->can('does_role') && $m->does_role($role_name); } return 0; } @@ -69,7 +100,7 @@ sub does { sub dump { my $self = shift; require Data::Dumper; - $Data::Dumper::Maxdepth = shift if @_; + local $Data::Dumper::Maxdepth = shift if @_; Data::Dumper::Dumper $self; } @@ -104,7 +135,12 @@ This will return the metaclass associated with the given class. =item B -This will create a new instance and call C. +This will call C, create a new instance and call C. + +=item B + +This method processes an argument list into a hash reference. It is used by +C. =item B @@ -143,11 +179,11 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006 by Infinity Interactive, Inc. +Copyright 2006-2008 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut