From: Paul Driver Date: Mon, 7 Apr 2008 14:35:52 +0000 (+0000) Subject: Forgot to add a couple of files inthe last commit. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=667db0bf109080936e84eabf992fbbdadb002da5;p=gitmo%2FMooseX-AttributeHelpers.git Forgot to add a couple of files inthe last commit. --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Number.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Number.pm new file mode 100644 index 0000000..9699619 --- /dev/null +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Number.pm @@ -0,0 +1,96 @@ + +package MooseX::AttributeHelpers::MethodProvider::Number; +use Moose::Role; + +our $VERSION = '0.02'; +our $AUTHORITY = 'cpan:STEVAN'; + +my %ops = ( + add => '+', + sub => '-', + mul => '*', + div => '/', + mod => '%', +); +foreach my $method (keys %ops) +{ + my $s = $ops{$method}; + __PACKAGE__->meta->alias_method($method, sub { + my ($attr, $reader, $writer) = @_; + return eval "sub { \$writer->(\$_[0], \$reader->(\$_[0]) $s \$_[1]) }"; + }); +} + +sub abs : method { + my ($attr, $reader, $writer) = @_; + return sub { $writer->($_[0], CORE::abs($reader->($_[0]))) }; +} + +sub set : method { + my ($attr, $reader, $writer) = @_; + return sub { $writer->($_[0], $_[1]) }; +} + +1; + +__END__ + +=pod + +=head1 NAME + +MooseX::AttributeHelpers::MethodProvider::Number + +=head1 DESCRIPTION + +This is a role which provides the method generators for +L. + +=head1 PROVIDED METHODS + +It is important to note that all those methods do in place modification of the +value stored in the attribute. All methods but 'set' are plain mathematical +operators, as in $current_value = $current_value I$argument, where I is +the operator listed next to the method name. + +=over 4 + +=item B: + + +=item B: - + +=item B: * + +=item B
: / + +=item B: % + +=item B: |$val|, or $val = abs($value). + +=item B: + +A way to set the value instead of 'setter' or 'is => "rw"'. This method is +provided for convenience. + +=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 + +Paul Driver Efrowith@cpan.orgE + +=head1 COPYRIGHT AND LICENSE + +Copyright 2007-2008 by Infinity Interactive, Inc. + +L + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut diff --git a/lib/MooseX/AttributeHelpers/Sugar.pm b/lib/MooseX/AttributeHelpers/Sugar.pm new file mode 100644 index 0000000..2809f17 --- /dev/null +++ b/lib/MooseX/AttributeHelpers/Sugar.pm @@ -0,0 +1,122 @@ + +package MooseX::AttributeHelpers::Sugar; +use Carp qw(confess); +use Exporter qw(import); +our @EXPORT = qw(define_attribute_helper); + +sub define_attribute_helper (%) { + my %info = @_; + my $class = caller(); + my $meta = $class->meta; + + $meta->add_method('helper_type', sub {$info{helper_type}}); + $meta->add_method('default_options', sub {$info{default_options}}); + $meta->add_method('auto_provide', sub {$info{auto_provide} || 0}); + + if(my $provider = $info{method_provider}) { + eval "require $provider"; + confess "Error loading method provider" if $@; + $meta->add_attribute('+method_provider', default => $provider); + } + + if (my $cons = $info{method_constructors}) { + $meta->add_attribute('+method_constructors', default => $cons) + } + + if (my $s = $info{shortcut}) { + $meta->create("Moose::Meta::Attribute::Custom::$s", + methods => {register_implementation => sub { $class }}, + ); + } +} + +1; + +__END__ + +=pod + +=head1 NAME + +MooseX::AttributeHelpers::Sugar - Convenience for defining AttributeHelper +metaclasses. + +=head1 SYNOPSIS + + package MooseX::AttributeHelpers::Counter; + use Moose; + use MooseX::AttributeHelpers::Sugar; + + extends 'MooseX::AttributeHelpers::Base'; + + define_attribute_helper ( + default_options => { + is => 'ro', + default => 0, + }, + + helper_type => 'Num', + method_provider => 'MooseX::AttributeHelpers::MethodProvider::Counter', + auto_provide => 1, + shortcut => 'Counter', + ); + + no Moose; + no MooseX::AttributeHelpers::Sugar; + + 1; + +=head1 DESCRIPTION + +This is just sugar to let you declaratively subclass +L. You still need to explicitly subclass, but +most of the boilerplate is taken care of for you by the sugar. One function is +exported. + +=over 4 + +=item B + +The following parameters are accepted, and are used to override methods in +the base class (see its documentation for details). + +=item B I + +=item B I + +=item B I + +=item B I + +=item B I + +=back + +=head SHORTCUT + +For ease of use of the generated metaclasses, if you pass in a "shortcut" +parameter to define_attribute_helper, a class at +Moose::Meta::Attribute::Custom::$shortcut will be generated for you, which +allows clients of your class to specify their metaclass by this shortcut +(without the long prefix). + +=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 + +Paul Driver E frodwith at cpan.org E + +=head1 COPYRIGHT AND LICENSE + +Copyright 2007, 2008 by Infinity Interactive, Inc. + +L + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut