From: Jesse Luehrs Date: Thu, 11 Nov 2010 15:12:02 +0000 (-0600) Subject: a bit more refactoring X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aa28eb106aaeffc3d29983be5f6b63b9fc254c16;p=gitmo%2FClass-MOP.git a bit more refactoring --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 891e611..cfa11fe 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -650,33 +650,52 @@ sub _inline_slot_initializer { my $self = shift; my ($attr, $idx) = @_; - my $default = $self->_inline_default_value($attr, $idx); - if (defined(my $init_arg = $attr->init_arg)) { my @source = ( 'if (exists $params->{\'' . $init_arg . '\'}) {', - $attr->_inline_set_value( - '$instance', '$params->{\'' . $init_arg . '\'}' - ), + $self->_inline_init_attr_from_constructor($attr, $idx), '}', ); - if (defined $default) { + if (my @default = $self->_inline_init_attr_from_default($attr, $idx)) { push @source, ( 'else {', - $attr->_inline_set_value('$instance', $default), + @default, '}', ); } return @source; } - elsif (defined $default) { - return $attr->_inline_set_value('$instance', $default); + elsif (my @default = $self->_inline_init_attr_from_default($attr, $idx)) { + return ( + '{', + @default, + '}', + ); } else { return (); } } +sub _inline_init_attr_from_constructor { + my $self = shift; + my ($attr, $idx) = @_; + + return $attr->_inline_set_value( + '$instance', '$params->{\'' . $attr->init_arg . '\'}' + ), +} + +sub _inline_init_attr_from_default { + my $self = shift; + my ($attr, $idx) = @_; + + my $default = $self->_inline_default_value($attr, $idx); + return unless $default; + + return $attr->_inline_set_value('$instance', $default); +} + sub _inline_default_value { my $self = shift; my ($attr, $index) = @_; diff --git a/t/010_self_introspection.t b/t/010_self_introspection.t index 04efa2d..ff2e2c5 100644 --- a/t/010_self_introspection.t +++ b/t/010_self_introspection.t @@ -66,6 +66,7 @@ my @class_mop_class_methods = qw( _inline_new_object _inline_default_value _inline_preserve_weak_metaclasses _inline_slot_initializer _inline_extra_init _inline_fallback_constructor _inline_generate_instance _inline_params _inline_slot_initializers + _inline_init_attr_from_constructor _inline_init_attr_from_default _generate_fallback_constructor construct_instance _construct_instance construct_class_instance _construct_class_instance