From: Stevan Little Date: Thu, 17 Aug 2006 16:58:14 +0000 (+0000) Subject: more examples X-Git-Tag: 0_03~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=244bd352801e30561098541bd2a7313234374810;p=gitmo%2FMoose-Autobox.git more examples --- diff --git a/examples/units/bytes.t b/examples/units/bytes.t index 7ca0d88..bea2992 100644 --- a/examples/units/bytes.t +++ b/examples/units/bytes.t @@ -26,11 +26,7 @@ use Moose::Autobox; } } -{ - package Moose::Autobox::SCALAR; - use Moose 'with'; - with 'Units::Bytes'; -} +Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes'); $\ = "\n"; diff --git a/examples/units/time.t b/examples/units/time.t index f04edd1..10f228c 100644 --- a/examples/units/time.t +++ b/examples/units/time.t @@ -51,11 +51,7 @@ use Moose::Autobox::Undef; } -{ - package Moose::Autobox::SCALAR; - use Moose 'with'; - with 'Units::Time'; -} +Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Time'); $\ = "\n"; diff --git a/lib/Moose/Autobox.pm b/lib/Moose/Autobox.pm index 775f6a4..7899bb7 100644 --- a/lib/Moose/Autobox.pm +++ b/lib/Moose/Autobox.pm @@ -17,38 +17,49 @@ sub import { UNDEF => 'Moose::Autobox::Undef', ); } - -package Moose::Autobox::SCALAR; -# NOTE: -# this doesnt make sense, but -# I need to prevent Moose from -# assiging to @ISA -use base 'UNIVERSAL'; -use Moose; -with 'Moose::Autobox::Scalar'; - -*does = \&Moose::Object::does; - -package Moose::Autobox::ARRAY; -use base 'UNIVERSAL'; -use Moose; -with 'Moose::Autobox::Array'; - -*does = \&Moose::Object::does; - -package Moose::Autobox::HASH; -use base 'UNIVERSAL'; -use Moose; -with 'Moose::Autobox::Hash'; -*does = \&Moose::Object::does; - -package Moose::Autobox::CODE; -use base 'UNIVERSAL'; -use Moose; -with 'Moose::Autobox::Code'; +sub mixin_additional_role { + my ($class, $type, $role) = @_; + ($type =~ /SCALAR|ARRAY|HASH|CODE/) + || confess "Can only add additional roles to SCALAR, ARRAY, HASH or CODE"; + ('Moose::Autobox::' . $type)->meta->_apply_all_roles($role); +} -*does = \&Moose::Object::does; +{ + + package Moose::Autobox::SCALAR; + # NOTE: + # this doesnt make sense, but + # I need to prevent Moose from + # assiging to @ISA + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Scalar'; + + *does = \&Moose::Object::does; + + package Moose::Autobox::ARRAY; + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Array'; + + *does = \&Moose::Object::does; + + package Moose::Autobox::HASH; + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Hash'; + + *does = \&Moose::Object::does; + + package Moose::Autobox::CODE; + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Code'; + + *does = \&Moose::Object::does; + +} 1; @@ -107,26 +118,19 @@ possible. This may or may not be possible, depending on how well L works out. At this point, I have high hopes for things but only time (and more tests and code) will tell. -=head1 ROLES - -This is a rough diagram of the roles involved to get our 4 -autoboxed types (SCALAR, ARRAY, HASH & CODE). - - +------------------------+-------------------------------+ - | Identity | Behavioral | - +------------------------+-------------------------------+ - | Item | | - | Undef | | - | Defined | | - | Scalar* <-|- String, Number <--+ | - | Ref | |-- Value | - | Array* <-|- List <------------+ | - | Hash* | | - | Code* | | - | | | - +------------------------+-------------------------------+ - - * indicates actual autoboxed types +=head1 METHODS + +=over 4 + +=item B + +This will mixin an additonal C<$role> into a certain C<$type>. The +types can be SCALAR, ARRAY, HASH or CODE. + +This can be used to add additional methods to the types, see the +F directory for some examples. + +=back =head1 TODO @@ -138,14 +142,6 @@ autoboxed types (SCALAR, ARRAY, HASH & CODE). =back -=head1 NOTES - - - String, Number & List are currently the only 'Value's. - - - Indexed is pretty much an interface, we probably will - need more of these (see Smalltalk Collection Trait - Refactoring) - =head1 BUGS All complex software has bugs lurking in it, and this module is no diff --git a/t/003_p6_example.t b/t/003_p6_example.t index 054f966..bead570 100644 --- a/t/003_p6_example.t +++ b/t/003_p6_example.t @@ -37,11 +37,7 @@ This comes from one of the examples in the Pugs distro. } } -{ - package Moose::Autobox::SCALAR; - use Moose 'with'; - with 'Units::Bytes'; -} +Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes'); sub testing_bytes { ::dies_ok { 10->bytes } '... cannot do the autoboxing lexically';