our $VERSION = '0.01';
-with 'Moose::Autobox::Ref';
+with 'Moose::Autobox::Ref',
+ 'Moose::Autobox::List';
+## List interface
+
sub length {
my ($array) = @_;
CORE::scalar @$array;
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 {
sub unshift {
my ($array, @rest) = @_;
- CORE::unshift @{$array}, @rest;
+ CORE::unshift @$array, @rest;
$array;
}
sub exists {
CORE::shift @$array;
}
+##
+
+sub keys {
+ my ($array) = @_;
+ [ 0 .. $#{$array} ];
+}
+
+sub values {
+ my ($array) = @_;
+ [ @$array ];
+}
+
+sub kv {
+ my ($array) = @_;
+ [ CORE::map { [ $_, $array->[$_] ] } $array->keys ];
+}
+
1;
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
--- /dev/null
+
+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
--- /dev/null
+#!/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
--- /dev/null
+#!/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');
+