From: Stevan Little Date: Thu, 2 Nov 2006 20:33:39 +0000 (+0000) Subject: moving things around to get ready to support Class::MOP 0.36 X-Git-Tag: 0_16~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8ee73eeb7e76858f1dbe56f69101a2dc1e096559;p=gitmo%2FMoose.git moving things around to get ready to support Class::MOP 0.36 --- diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 878f8cd..b7c0558 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -9,6 +9,7 @@ use Carp 'confess'; our $VERSION = '0.08'; +use Moose::Meta::Method::Accessor; use Moose::Util::TypeConstraints (); use base 'Class::MOP::Attribute'; diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 39ff982..7e5d102 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -11,6 +11,8 @@ use Scalar::Util 'weaken', 'blessed', 'reftype'; our $VERSION = '0.08'; +use Moose::Meta::Method::Overriden; + use base 'Class::MOP::Class'; __PACKAGE__->meta->add_attribute('roles' => ( @@ -23,6 +25,7 @@ sub initialize { my $pkg = shift; $class->SUPER::initialize($pkg, ':attribute_metaclass' => 'Moose::Meta::Attribute', + ':method_metaclass' => 'Moose::Meta::Method', ':instance_metaclass' => 'Moose::Meta::Instance', @_); } @@ -313,15 +316,6 @@ sub _process_inherited_attribute { return $new_attr; } -package Moose::Meta::Method::Overriden; - -use strict; -use warnings; - -our $VERSION = '0.01'; - -use base 'Class::MOP::Method'; - 1; __END__ diff --git a/lib/Moose/Meta/Method.pm b/lib/Moose/Meta/Method.pm new file mode 100644 index 0000000..8c902f0 --- /dev/null +++ b/lib/Moose/Meta/Method.pm @@ -0,0 +1,16 @@ +package Moose::Meta::Method; + +use strict; +use warnings; + +our $VERSION = '0.01'; + +use base 'Class::MOP::Method'; + +1; + +__END__ + +=pod + +=cut \ No newline at end of file diff --git a/lib/Moose/Meta/Method/Accessor.pm b/lib/Moose/Meta/Method/Accessor.pm new file mode 100644 index 0000000..338669f --- /dev/null +++ b/lib/Moose/Meta/Method/Accessor.pm @@ -0,0 +1,18 @@ + +package Moose::Meta::Method::Accessor; + +use strict; +use warnings; + +our $VERSION = '0.01'; + +use base 'Moose::Meta::Method'; +# 'Class::MOP::Method::Accessor'; + +1; + +__END__ + +=pod + +=cut \ No newline at end of file diff --git a/lib/Moose/Meta/Method/Overriden.pm b/lib/Moose/Meta/Method/Overriden.pm new file mode 100644 index 0000000..7114976 --- /dev/null +++ b/lib/Moose/Meta/Method/Overriden.pm @@ -0,0 +1,16 @@ +package Moose::Meta::Method::Overriden; + +use strict; +use warnings; + +our $VERSION = '0.01'; + +use base 'Moose::Meta::Method'; + +1; + +__END__ + +=pod + +=cut \ No newline at end of file diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 6eec907..fbe9424 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -9,10 +9,10 @@ use Carp 'confess'; use Scalar::Util 'blessed'; use B 'svref_2object'; -use Moose::Meta::Class; - our $VERSION = '0.05'; +use Moose::Meta::Class; + use base 'Class::MOP::Module'; ## Attributes diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index e67e6f3..8d0f28a 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -11,6 +11,8 @@ use Scalar::Util 'blessed'; our $VERSION = '0.06'; +use Moose::Meta::TypeConstraint::Union; + __PACKAGE__->meta->add_attribute('name' => (reader => 'name' )); __PACKAGE__->meta->add_attribute('parent' => (reader => 'parent' )); __PACKAGE__->meta->add_attribute('constraint' => (reader => 'constraint')); @@ -128,116 +130,6 @@ sub union { ); } -package Moose::Meta::TypeConstraint::Union; - -use strict; -use warnings; -use metaclass; - -our $VERSION = '0.03'; - -__PACKAGE__->meta->add_attribute('type_constraints' => ( - accessor => 'type_constraints', - default => sub { [] } -)); - -sub new { - my $class = shift; - my $self = $class->meta->new_object(@_); - return $self; -} - -sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} } - -# NOTE: -# this should probably never be used -# but we include it here for completeness -sub constraint { - my $self = shift; - sub { $self->check($_[0]) }; -} - -# conform to the TypeConstraint API -sub parent { undef } -sub message { undef } -sub has_message { 0 } - -# FIXME: -# not sure what this should actually do here -sub coercion { undef } - -# this should probably be memoized -sub has_coercion { - my $self = shift; - foreach my $type (@{$self->type_constraints}) { - return 1 if $type->has_coercion - } - return 0; -} - -# NOTE: -# this feels too simple, and may not always DWIM -# correctly, especially in the presence of -# close subtype relationships, however it should -# work for a fair percentage of the use cases -sub coerce { - my $self = shift; - my $value = shift; - foreach my $type (@{$self->type_constraints}) { - if ($type->has_coercion) { - my $temp = $type->coerce($value); - return $temp if $self->check($temp); - } - } - return undef; -} - -sub _compiled_type_constraint { - my $self = shift; - return sub { - my $value = shift; - foreach my $type (@{$self->type_constraints}) { - return 1 if $type->check($value); - } - return undef; - } -} - -sub check { - my $self = shift; - my $value = shift; - $self->_compiled_type_constraint->($value); -} - -sub validate { - my $self = shift; - my $value = shift; - my $message; - foreach my $type (@{$self->type_constraints}) { - my $err = $type->validate($value); - return unless defined $err; - $message .= ($message ? ' and ' : '') . $err - if defined $err; - } - return ($message . ' in (' . $self->name . ')') ; -} - -sub is_a_type_of { - my ($self, $type_name) = @_; - foreach my $type (@{$self->type_constraints}) { - return 1 if $type->is_a_type_of($type_name); - } - return 0; -} - -sub is_subtype_of { - my ($self, $type_name) = @_; - foreach my $type (@{$self->type_constraints}) { - return 1 if $type->is_subtype_of($type_name); - } - return 0; -} - 1; __END__ diff --git a/lib/Moose/Meta/TypeConstraint/Union.pm b/lib/Moose/Meta/TypeConstraint/Union.pm new file mode 100644 index 0000000..9167111 --- /dev/null +++ b/lib/Moose/Meta/TypeConstraint/Union.pm @@ -0,0 +1,118 @@ + +package Moose::Meta::TypeConstraint::Union; + +use strict; +use warnings; +use metaclass; + +our $VERSION = '0.03'; + +__PACKAGE__->meta->add_attribute('type_constraints' => ( + accessor => 'type_constraints', + default => sub { [] } +)); + +sub new { + my $class = shift; + my $self = $class->meta->new_object(@_); + return $self; +} + +sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} } + +# NOTE: +# this should probably never be used +# but we include it here for completeness +sub constraint { + my $self = shift; + sub { $self->check($_[0]) }; +} + +# conform to the TypeConstraint API +sub parent { undef } +sub message { undef } +sub has_message { 0 } + +# FIXME: +# not sure what this should actually do here +sub coercion { undef } + +# this should probably be memoized +sub has_coercion { + my $self = shift; + foreach my $type (@{$self->type_constraints}) { + return 1 if $type->has_coercion + } + return 0; +} + +# NOTE: +# this feels too simple, and may not always DWIM +# correctly, especially in the presence of +# close subtype relationships, however it should +# work for a fair percentage of the use cases +sub coerce { + my $self = shift; + my $value = shift; + foreach my $type (@{$self->type_constraints}) { + if ($type->has_coercion) { + my $temp = $type->coerce($value); + return $temp if $self->check($temp); + } + } + return undef; +} + +sub _compiled_type_constraint { + my $self = shift; + return sub { + my $value = shift; + foreach my $type (@{$self->type_constraints}) { + return 1 if $type->check($value); + } + return undef; + } +} + +sub check { + my $self = shift; + my $value = shift; + $self->_compiled_type_constraint->($value); +} + +sub validate { + my $self = shift; + my $value = shift; + my $message; + foreach my $type (@{$self->type_constraints}) { + my $err = $type->validate($value); + return unless defined $err; + $message .= ($message ? ' and ' : '') . $err + if defined $err; + } + return ($message . ' in (' . $self->name . ')') ; +} + +sub is_a_type_of { + my ($self, $type_name) = @_; + foreach my $type (@{$self->type_constraints}) { + return 1 if $type->is_a_type_of($type_name); + } + return 0; +} + +sub is_subtype_of { + my ($self, $type_name) = @_; + foreach my $type (@{$self->type_constraints}) { + return 1 if $type->is_subtype_of($type_name); + } + return 0; +} + +1; + +__END__ + +=pod + +=cut