From: Stevan Little Date: Tue, 21 Mar 2006 16:17:13 +0000 (+0000) Subject: more-tweaks X-Git-Tag: 0_05~71 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7c13858b25dc93ccefeb881db1f1169c27d23bb6;p=gitmo%2FMoose.git more-tweaks --- diff --git a/lib/Moose.pm b/lib/Moose.pm index a8a9a56..0a8424e 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -17,6 +17,7 @@ use Class::MOP; use Moose::Meta::Class; use Moose::Meta::Attribute; use Moose::Meta::TypeConstraint; +use Moose::Meta::TypeCoercion; use Moose::Object; use Moose::Util::TypeConstraints; @@ -81,7 +82,7 @@ sub import { } else { # otherwise assume it is a constraint - my $constraint = Moose::Util::TypeConstraints::find_type_constraint($options{isa}); + my $constraint = find_type_constraint($options{isa}); # if the constraing it not found .... unless (defined $constraint) { # assume it is a foreign class, and make diff --git a/lib/Moose/Meta/TypeCoercion.pm b/lib/Moose/Meta/TypeCoercion.pm index 1e9f470..fea1b7d 100644 --- a/lib/Moose/Meta/TypeCoercion.pm +++ b/lib/Moose/Meta/TypeCoercion.pm @@ -44,7 +44,7 @@ sub compile_type_coercion { my $constraint = Moose::Util::TypeConstraints::find_type_constraint($constraint_name)->_compiled_type_constraint; (defined $constraint) || confess "Could not find the type constraint ($constraint_name)"; - push @coercions => [ $constraint, $action ]; + push @coercions => [ $constraint, $action ]; } $self->_compiled_type_coercion(sub { my $thing = shift; diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 8963ad2..00a4760 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -5,7 +5,6 @@ use strict; use warnings; use Carp 'confess'; -use Sub::Name 'subname'; use Scalar::Util 'blessed'; our $VERSION = '0.02'; @@ -16,9 +15,8 @@ use Moose::Meta::TypeCoercion; sub import { shift; my $pkg = shift || caller(); - return if $pkg eq ':no_export'; no strict 'refs'; - foreach my $export (qw(type subtype as where coerce from via)) { + foreach my $export (qw(type subtype as where coerce from via find_type_constraint)) { *{"${pkg}::${export}"} = \&{"${export}"}; } } @@ -27,12 +25,12 @@ sub import { my %TYPES; sub find_type_constraint { $TYPES{$_[0]} } - sub create_type_constraint { + sub _create_type_constraint { my ($name, $parent, $check) = @_; (!exists $TYPES{$name}) || confess "The type constraint '$name' has already been created" if defined $name; - $parent = find_type_constraint($parent) if defined $parent; + $parent = $TYPES{$parent} if defined $parent; my $constraint = Moose::Meta::TypeConstraint->new( name => $name || '__ANON__', parent => $parent, @@ -42,9 +40,9 @@ sub import { return $constraint; } - sub install_type_coercions { + sub _install_type_coercions { my ($type_name, $coercion_map) = @_; - my $type = find_type_constraint($type_name); + my $type = $TYPES{$type_name}; (!$type->has_coercion) || confess "The type coercion for '$type_name' has already been registered"; my $type_coercion = Moose::Meta::TypeCoercion->new( @@ -63,20 +61,21 @@ sub import { } } +# type constructors sub type ($$) { my ($name, $check) = @_; - create_type_constraint($name, undef, $check); + _create_type_constraint($name, undef, $check); } sub subtype ($$;$) { unshift @_ => undef if scalar @_ == 2; - create_type_constraint(@_); + _create_type_constraint(@_); } sub coerce ($@) { my ($type_name, @coercion_map) = @_; - install_type_coercions($type_name, \@coercion_map); + _install_type_coercions($type_name, \@coercion_map); } sub as ($) { $_[0] } @@ -165,9 +164,9 @@ Suggestions for improvement are welcome. =item B -=item B +=item B<_create_type_constraint ($type_name, $type_constraint)> -=item B +=item B<_install_type_coercions> =item B diff --git a/t/050_util_type_constraints.t b/t/050_util_type_constraints.t index 67c28f7..9216a7d 100644 --- a/t/050_util_type_constraints.t +++ b/t/050_util_type_constraints.t @@ -23,7 +23,7 @@ subtype NaturalLessThanTen => as Natural => where { $_ < 10 }; -Moose::Util::TypeConstraints::export_type_contstraints_as_functions(); +Moose::Util::TypeConstraints->export_type_contstraints_as_functions(); is(Num(5), 5, '... this is a Num'); ok(!defined(Num('Foo')), '... this is not a Num'); diff --git a/t/051_util_type_constraints_export.t b/t/051_util_type_constraints_export.t index 065fc12..54f3357 100644 --- a/t/051_util_type_constraints_export.t +++ b/t/051_util_type_constraints_export.t @@ -25,7 +25,7 @@ BEGIN { }; ::ok(!$@, '... successfully exported &subtype to Foo package'); - Moose::Util::TypeConstraints::export_type_contstraints_as_functions(); + Moose::Util::TypeConstraints->export_type_contstraints_as_functions(); ::ok(MyRef({}), '... Ref worked correctly'); ::ok(MyArrayRef([]), '... ArrayRef worked correctly'); diff --git a/t/052_util_std_type_constraints.t b/t/052_util_std_type_constraints.t index 790b999..526a43a 100644 --- a/t/052_util_std_type_constraints.t +++ b/t/052_util_std_type_constraints.t @@ -14,7 +14,7 @@ BEGIN { my $SCALAR_REF = \(my $var); -Moose::Util::TypeConstraints::export_type_contstraints_as_functions(); +Moose::Util::TypeConstraints->export_type_contstraints_as_functions(); ok(defined Any(0), '... Any accepts anything'); ok(defined Any(100), '... Any accepts anything'); diff --git a/t/053_util_find_type_constraint.t b/t/053_util_find_type_constraint.t index bd86bb0..0f843f8 100644 --- a/t/053_util_find_type_constraint.t +++ b/t/053_util_find_type_constraint.t @@ -7,7 +7,7 @@ use Test::More tests => 12; use Test::Exception; BEGIN { - use_ok('Moose::Util::TypeConstraints', (':no_export')); + use_ok('Moose::Util::TypeConstraints'); } foreach my $type_name (qw( @@ -23,7 +23,7 @@ foreach my $type_name (qw( RegexpRef Object )) { - is(Moose::Util::TypeConstraints::find_type_constraint($type_name)->name, + is(find_type_constraint($type_name)->name, $type_name, '... got the right name for ' . $type_name); } \ No newline at end of file diff --git a/t/054_util_type_coercion.t b/t/054_util_type_coercion.t index 7ac47be..6a1669c 100644 --- a/t/054_util_type_coercion.t +++ b/t/054_util_type_coercion.t @@ -30,7 +30,7 @@ coerce Header => from HashRef => via { HTTPHeader->new(hash => $_[0]) }; -Moose::Util::TypeConstraints::export_type_contstraints_as_functions(); +Moose::Util::TypeConstraints->export_type_contstraints_as_functions(); my $header = HTTPHeader->new(); isa_ok($header, 'HTTPHeader'); @@ -39,7 +39,7 @@ ok(Header($header), '... this passed the type test'); ok(!Header([]), '... this did not pass the type test'); ok(!Header({}), '... this did not pass the type test'); -my $coercion = Moose::Util::TypeConstraints::find_type_constraint('Header')->coercion; +my $coercion = find_type_constraint('Header')->coercion; isa_ok($coercion, 'Moose::Meta::TypeCoercion'); {