From: Dave Rolsky Date: Wed, 22 Sep 2010 22:15:00 +0000 (-0500) Subject: Calculate _native_type from role name, rather than hardcoding it X-Git-Tag: 1.15~117 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=157152453b33fc25c31161cd889dff1ab26a39af;p=gitmo%2FMoose.git Calculate _native_type from role name, rather than hardcoding it --- diff --git a/lib/Moose/Meta/Attribute/Native/Trait.pm b/lib/Moose/Meta/Attribute/Native/Trait.pm index 28337ab..93aab7e 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait.pm @@ -123,12 +123,26 @@ sub _root_types { sub _native_accessor_class_for { my ( $self, $suffix ) = @_; - # XXX - bridge code - return unless $self->can('_native_type'); - return 'Moose::Meta::Method::Accessor::Native::' . $self->_native_type . '::' . $suffix; } +sub _build_native_type { + my $self = shift; + + for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) { + return $1 if $role_name =~ /::Native::Trait::(\w+)$/; + } + + die "Cannot calculate native type for " . ref $self; +} + +has '_native_type' => ( + is => 'ro', + isa => 'Str', + lazy => 1, + builder => '_build_native_type', +); + has 'method_constructors' => ( is => 'ro', isa => 'HashRef', diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm index 987f8d5..095f365 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm @@ -35,8 +35,6 @@ with 'Moose::Meta::Attribute::Native::Trait'; sub _helper_type { 'ArrayRef' } -sub _native_type { 'Array' } - no Moose::Role; 1; diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm b/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm index 6ef82ac..ed9dee6 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm @@ -15,8 +15,6 @@ with 'Moose::Meta::Attribute::Native::Trait'; sub _default_is { 'rw' } sub _helper_type { 'Bool' } -sub _native_type { 'Bool' } - no Moose::Role; 1; diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Code.pm b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm index 07ac6e8..30b6d2f 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Code.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm @@ -12,8 +12,6 @@ with 'Moose::Meta::Attribute::Native::Trait'; sub _helper_type { 'CodeRef' } -sub _native_type { 'Code' } - no Moose::Role; 1; diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm b/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm index c1912c0..53ba46e 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm @@ -17,7 +17,6 @@ with 'Moose::Meta::Attribute::Native::Trait' => sub _default_default { 0 } sub _default_is { 'ro' } sub _helper_type { 'Num' } -sub _native_type { 'Counter' } sub _root_types { 'Num', 'Int' } no Moose::Role; diff --git a/lib/Moose/Meta/Attribute/Native/Trait/String.pm b/lib/Moose/Meta/Attribute/Native/Trait/String.pm index 341a3b6..19febd8 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/String.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/String.pm @@ -22,8 +22,6 @@ sub _default_default { q{} } sub _default_is { 'rw' } sub _helper_type { 'Str' } -sub _native_type { 'String' } - no Moose::Role; 1;