From: Stevan Little Date: Fri, 2 Jun 2006 12:23:41 +0000 (+0000) Subject: foo X-Git-Tag: 0_02~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e6bb88b0e1fff9794352dde571e623ae0f4e3a95;p=gitmo%2FMoose-Autobox.git foo --- diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index 37cc777..1b31e90 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -3,8 +3,11 @@ use Moose::Role 'with'; our $VERSION = '0.01'; -with 'Moose::Autobox::Ref'; +with 'Moose::Autobox::Ref', + 'Moose::Autobox::List'; +## List interface + sub length { my ($array) = @_; CORE::scalar @$array; @@ -27,18 +30,20 @@ sub join { sub reverse { my ($array) = @_; - [ CORE::reverse @{$array} ]; + [ CORE::reverse @$array ]; } sub sort { my ($array, $sub) = @_; $sub ||= sub { $a cmp $b }; [ CORE::sort { $sub->($a, $b) } @$array ]; -} +} + +## Array Interface sub pop { my ($array) = @_; - CORE::pop @{$array}; + CORE::pop @$array; } sub push { @@ -49,7 +54,7 @@ sub push { sub unshift { my ($array, @rest) = @_; - CORE::unshift @{$array}, @rest; + CORE::unshift @$array, @rest; $array; } sub exists { @@ -67,4 +72,21 @@ sub shift { CORE::shift @$array; } +## + +sub keys { + my ($array) = @_; + [ 0 .. $#{$array} ]; +} + +sub values { + my ($array) = @_; + [ @$array ]; +} + +sub kv { + my ($array) = @_; + [ CORE::map { [ $_, $array->[$_] ] } $array->keys ]; +} + 1; diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 2cc99a3..52238c7 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -9,14 +9,20 @@ sub exists { my ($hash, $key) = @_; CORE::exists $hash->{$key}; } + sub keys { my ($hash) = @_; - [ CORE::keys %{$hash} ]; + [ CORE::keys %$hash ]; } sub values { my ($hash) = @_; - [ CORE::values %{$hash} ]; + [ CORE::values %$hash ]; +} + +sub kv { + my ($hash) = @_; + [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ]; } 1; \ No newline at end of file diff --git a/lib/Moose/Autobox/List.pm b/lib/Moose/Autobox/List.pm new file mode 100644 index 0000000..e7fac25 --- /dev/null +++ b/lib/Moose/Autobox/List.pm @@ -0,0 +1,11 @@ + +package Moose::Autobox::List; +use Moose::Role 'with', 'requires'; + +our $VERSION = '0.01'; + +with 'Moose::Autobox::Value'; + +requires qw/length grep map join reverse sort/; + +1; \ No newline at end of file diff --git a/t/001_example.t b/t/001_example.t deleted file mode 100644 index e69de29..0000000 diff --git a/t/002_example.t b/t/002_example.t new file mode 100644 index 0000000..83d7337 --- /dev/null +++ b/t/002_example.t @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More no_plan => 1; + +BEGIN { + use_ok('Moose::Autobox'); +} + +ok(SCALAR->does('Moose::Autobox::Scalar'), '... SCALAR does Moose::Autobox::Scalar'); + ok(SCALAR->does('Moose::Autobox::Ref'), '... SCALAR does Moose::Autobox::Ref'); + ok(SCALAR->does('Moose::Autobox::Value'), '... SCALAR does Moose::Autobox::Value'); + ok(SCALAR->does('Moose::Autobox::Defined'), '... SCALAR does Moose::Autobox::Defined'); + ok(SCALAR->does('Moose::Autobox::Item'), '... SCALAR does Moose::Autobox::Item'); + +ok(ARRAY->does('Moose::Autobox::Array'), '... ARRAY does Moose::Autobox::Array'); + ok(ARRAY->does('Moose::Autobox::List'), '... ARRAY does Moose::Autobox::List'); + ok(ARRAY->does('Moose::Autobox::Ref'), '... ARRAY does Moose::Autobox::Ref'); + ok(ARRAY->does('Moose::Autobox::Defined'), '... ARRAY does Moose::Autobox::Defined'); + ok(ARRAY->does('Moose::Autobox::Item'), '... ARRAY does Moose::Autobox::Item'); + +ok(HASH->does('Moose::Autobox::Hash'), '... HASH does Moose::Autobox::Hash'); + ok(HASH->does('Moose::Autobox::Ref'), '... HASH does Moose::Autobox::Ref'); + ok(HASH->does('Moose::Autobox::Defined'), '... HASH does Moose::Autobox::Defined'); + ok(HASH->does('Moose::Autobox::Item'), '... HASH does Moose::Autobox::Item'); + +ok(CODE->does('Moose::Autobox::Code'), '... CODE does Moose::Autobox::Code'); + ok(CODE->does('Moose::Autobox::Ref'), '... CODE does Moose::Autobox::Ref'); + ok(CODE->does('Moose::Autobox::Defined'), '... CODE does Moose::Autobox::Defined'); + ok(CODE->does('Moose::Autobox::Item'), '... CODE does Moose::Autobox::Item'); \ No newline at end of file diff --git a/t/003_p6_example.t b/t/003_p6_example.t new file mode 100644 index 0000000..4be1352 --- /dev/null +++ b/t/003_p6_example.t @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More no_plan => 1; + +BEGIN { + use_ok('Moose::Autobox'); +} + +use autobox; + +SCALAR->meta->add_method('bytes' => sub { + $_[0]; +}); + +SCALAR->meta->add_method('byte' => SCALAR->meta->get_method('bytes')); + +SCALAR->meta->add_method('kilobytes' => sub { + $_[0] * 1024; +}); + +SCALAR->meta->add_method('kilobyte' => SCALAR->meta->get_method('kilobytes')); + +SCALAR->meta->add_method('megabytes' => sub { + $_[0] * 1024->kilobytes; +}); + +SCALAR->meta->add_method('metabyte' => SCALAR->meta->get_method('megabytes')); + +SCALAR->meta->add_method('gigabytes' => sub { + $_[0] * 1024->megabytes; +}); + +SCALAR->meta->add_method('gigabyte' => SCALAR->meta->get_method('gigabytes')); + +SCALAR->meta->add_method('terabytes' => sub { + $_[0] * 1024->gigabytes; +}); + +SCALAR->meta->add_method('terabyte' => SCALAR->meta->get_method('terabytes')); + +is(5->bytes, 5, '... got 5 bytes'); +is(5->kilobytes, 5120, '... got 5 kilobytes'); +is(2->megabytes, 2097152, '... got 2 megabytes'); +is(1->gigabyte, 1073741824, '... got 1 gigabyte'); +is(2->terabytes, 2199023255552, '... got 2 terabyte'); +