our $AUTHORITY = 'cpan:STEVAN';
use MooseX::AttributeHelpers::Meta::Method::Provided;
+use MooseX::AttributeHelpers::Meta::Method::Curried;
use MooseX::AttributeHelpers::Counter;
use MooseX::AttributeHelpers::Number;
get => 'get_mapping',
set => 'set_mapping',
},
+ curries => {
+ set => [ set_quantity => 'quantity' ]
+ }
);
# ...
my $obj = MyClass->new;
- $obj->set_mapping(4, 'foo');
- $obj->set_mapping(5, 'bar');
- $obj->set_mapping(6, 'baz');
+ $obj->set_quantity(10); # quantity => 10
+ $obj->set_mapping(4, 'foo'); # 4 => 'foo'
+ $obj->set_mapping(5, 'bar'); # 5 => 'bar'
+ $obj->set_mapping(6, 'baz'); # 6 => 'baz'
# prints 'bar'
print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
As seen in the L</SYNOPSIS>, you specify the extension via the
C<metaclass> parameter. Available meta classes are:
+=head1 PARAMETERS
+
+=head2 provides
+
+This points to a hashref that uses C<provider> for the keys and
+C<['method', @args]> for the values. The method will be added to
+the object itself and do what you want.
+
+=head2 curries
+
+This points to a hashref that uses C<provider> for the keys and
+C<['method', @args]> for the values. The method will be added to
+the object itself (always using C<@args> as the beginning arguments).
+
+=head1 METHOD PROVIDERS
+
=over
=item L<Number|MooseX::AttributeHelpers::Number>
default => sub {{}}
);
+has 'curries' => (
+ is => 'ro',
+ isa => 'HashRef',
+ default => sub {{}}
+);
+
# these next two are the possible methods
# you can use in the 'provides' map.
}
}
+sub _curry {
+ my $self = shift;
+ my $code = shift;
+
+ #warn "_curry: "; use DDS; warn Dump($self);
+ my @args = @_;
+ return sub { my $self = shift; $code->($self, @args, @_) };
+}
+
after 'install_accessors' => sub {
my $attr = shift;
my $class = $attr->associated_class;
# before we install them, lets
# make sure they are valid
$attr->check_provides_values;
+# $attr->check_curries_values;
my $method_constructors = $attr->method_constructors;
my $class_name = $class->name;
+ foreach my $key (keys %{$attr->curries}) {
+
+ my ($curried_name, @curried_args) = @{ $attr->curries->{$key} };
+
+ if ($class->has_method($curried_name)) {
+ confess "The method ($curried_name) already exists in class (" . $class->name . ")";
+ }
+
+ my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
+ $attr->_curry($method_constructors->{$key}->(
+ $attr,
+ $attr_reader,
+ $attr_writer,
+ ), @curried_args),
+ package_name => $class_name,
+ name => $curried_name,
+ );
+
+#use DDS; warn Dump($method);
+
+ $attr->associate_method($method);
+ $class->add_method($curried_name => $method);
+ }
+
foreach my $key (keys %{$attr->provides}) {
my $method_name = $attr->provides->{$key};
--- /dev/null
+
+package MooseX::AttributeHelpers::Meta::Method::Curried;
+use Moose;
+
+extends 'Moose::Meta::Method';
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+MooseX::AttributeHelpers::Meta::Method::Curried
+
+=head1 DESCRIPTION
+
+This is an extension of Moose::Meta::Method to mark I<curried> methods.
+
+=head1 METHODS
+
+=over 4
+
+=item B<meta>
+
+=back
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Stevan Little E<lt>stevan@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007-2008 by Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
use strict;
use warnings;
-use Test::More tests => 51;
+use Test::More tests => 52;
use Test::Exception;
BEGIN {
has 'options' => (
metaclass => 'Collection::Array',
is => 'ro',
- isa => 'ArrayRef[Int]',
+ isa => 'ArrayRef[Str]',
default => sub { [] },
provides => {
'push' => 'add_options',
'count' => 'num_options',
'empty' => 'has_options',
'clear' => 'clear_options',
+ },
+ curries => {
+ 'push' => ['add_options_with_speed', 'funrolls', 'funbuns'],
+ 'unshift' => ['prepend_prerequisites_along_with', 'first', 'second']
}
);
}
$stuff->clear_options;
is_deeply( $stuff->options, [], "... clear options" );
-## check some errors
+lives_ok {
+ $stuff->add_options('tree');
+} '... set the options okay';
-dies_ok {
- $stuff->add_options([]);
-} '... could not add an array ref where an int is expected';
+lives_ok {
+ $stuff->add_options_with_speed('compatible', 'safe');
+} '... add options with speed okay';
-dies_ok {
- $stuff->insert_options(undef);
-} '... could not add an undef where an int is expected';
+is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/]);
-dies_ok {
- $stuff->set_option(5, {});
-} '... could not add a hash ref where an int is expected';
+lives_ok {
+ $stuff->prepend_prerequisites_along_with();
+} '... add prerequisite options okay';
+
+## check some errors
+
+#dies_ok {
+# $stuff->insert_options(undef);
+#} '... could not add an undef where a string is expected';
+#
+#dies_ok {
+# $stuff->set_option(5, {});
+#} '... could not add a hash ref where a string is expected';
dies_ok {
- Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
+ Stuff->new(options => [ undef, 10, undef, 20 ]);
} '... bad constructor params';
## test the meta
'clear' => 'clear_options',
}, '... got the right provies mapping');
-is($options->type_constraint->type_parameter, 'Int', '... got the right container type');
+is($options->type_constraint->type_parameter, 'Str', '... got the right container type');
use strict;
use warnings;
-use Test::More tests => 35;
+use Test::More tests => 37;
use Test::Exception;
BEGIN {
'count' => 'num_options',
'clear' => 'clear_options',
'delete' => 'delete_option',
+ },
+ curries => {
+ 'set' => [
+ 'set_with_defaults' =>
+ size => 'medium', quantity => 1
+ ],
}
);
}
is_deeply($stuff->options, { }, "... cleared options" );
lives_ok {
+ $stuff->set_with_defaults(foo => 'bar');
+} '... options added okay with defaults';
+
+is_deeply($stuff->options, {size => 'medium', quantity => 1, foo => 'bar'});
+
+lives_ok {
Stuff->new(options => { foo => 'BAR' });
} '... good constructor params';
use strict;
use warnings;
-use Test::More tests => 20;
+use Test::More tests => 26;
BEGIN {
use_ok('MooseX::AttributeHelpers');
div => 'div',
mod => 'mod',
abs => 'abs',
+ },
+ curries => {
+ 'add' => ['inc', 1],
+ 'sub' => ['dec', 1],
+ 'mod' => ['odd', 2],
+ 'div' => ['cut_in_half', 2]
}
);
}
isa_ok($real, 'Real');
can_ok($real, $_) for qw[
- set add sub mul div mod abs
+ set add sub mul div mod abs inc dec odd cut_in_half
];
is $real->integer, 5, 'Default to five';
is $real->integer, 1, 'abs 1';
+$real->set(12);
+
+$real->inc;
+
+is $real->integer, 13, 'inc 12';
+
+$real->dec;
+
+is $real->integer, 12, 'dec 13';
+
## test the meta
my $attr = $real->meta->get_attribute('integer');
use strict;
use warnings;
-use Test::More tests => 21;
+use Test::More tests => 24;
use Test::Exception;
BEGIN {
'find' => 'find_option',
'elements' => 'options',
'join' => 'join_options',
+ },
+ curries => {
+ 'grep' => ['less_than_five', sub { $_ < 5 }],
+ 'map' => ['up_by_one', sub { $_ + 1 }],
+ 'join' => ['dashify', '-']
}
);
}
is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
+# test the currying
+is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
+
+is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
+
+is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
+
## test the meta
my $options = $stuff->meta->get_attribute('_options');
use strict;
use warnings;
-use Test::More tests => 17;
+use Test::More tests => 21;
BEGIN {
use_ok('MooseX::AttributeHelpers');
chop => 'chop_string',
chomp => 'chomp_string',
clear => 'clear_string',
+ },
+ curries => {
+ append => ['exclaim', '!'],
+ replace => ['capitalize_last', qr/(.)$/, sub { uc $1 }],
+ match => ['invalid_number', qr/\D/]
}
);
}
$page->replace_string(qr/([ao])/, sub { uc($1) });
is($page->string, 'bArcfo', "substitution");
+$page->exclaim;
+is($page->string, 'bArcfo!', 'exclaim!');
+
+$page->string('Moosex');
+$page->capitalize_last;
+is($page->string, 'MooseX', 'capitalize last');
+
+$page->string('1234');
+ok(!$page->invalid_number, 'string "isn\'t an invalid number');
+
+$page->string('one two three four');
+ok($page->invalid_number, 'string an invalid number');
+
$page->clear_string;
is($page->string, '', "clear");