use lib 't/lib';
do {
- # copied from MouseX::AttributeHelpers;
+ # copied from MooseX::AttributeHelpers;
package MouseX::AttributeHelpers::Trait::Base;
use Mouse::Role;
use Mouse::Util::TypeConstraints;
# extend the parents stuff to make sure
# certain bits are now required ...
#has 'default' => (required => 1);
- has 'type_constraint' => (required => 1);
+ has 'type_constraint' => (is => 'rw', required => 1);
## Methods called prior to instantiation
sub helper_type { 'Num' }
- has 'method_constructors' => (
- is => 'ro',
- isa => 'HashRef',
- lazy => 1,
- default => sub {
- return +{
- set => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$writer($_[1]) };
- },
- get => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$reader() };
- },
- add => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$writer($_[0]->$reader() + $_[1]) };
- },
- sub => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$writer($_[0]->$reader() - $_[1]) };
+ has 'method_constructors' => (\r
+ is => 'ro',\r
+ isa => 'HashRef',\r
+ lazy => 1,\r
+ default => sub {\r
+ return +{\r
+ set => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $writer->( $_[0], $_[1] ) };\r
},
- mul => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$writer($_[0]->$reader() * $_[1]) };
+ get => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $reader->( $_[0] ) };\r
},
- div => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$writer($_[0]->$reader() / $_[1]) };
- },
- mod => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$writer($_[0]->$reader() % $_[1]) };
- },
- abs => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $_[0]->$writer(abs($_[0]->$reader()) ) };
- },
- }
- }
- );
+ add => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $writer->( $_[0], $reader->( $_[0] ) + $_[1] ) };\r
+ },\r
+ sub => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $writer->( $_[0], $reader->( $_[0] ) - $_[1] ) };\r
+ },\r
+ mul => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $writer->( $_[0], $reader->( $_[0] ) * $_[1] ) };\r
+ },\r
+ div => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $writer->( $_[0], $reader->( $_[0] ) / $_[1] ) };\r
+ },\r
+ mod => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $writer->( $_[0], $reader->( $_[0] ) % $_[1] ) };\r
+ },\r
+ abs => sub {\r
+ my ( $attr, $reader, $writer ) = @_;\r
+ return sub { $writer->( $_[0], abs( $reader->( $_[0] ) ) ) };\r
+ },\r
+ };\r
+ }\r
+ );\r
+\r
package MouseX::AttributeHelpers::Number;
use Mouse;
has 'ii' => (
isa => 'Num',
+ predicate => 'has_ii',
+
provides => {
sub => 'ii_minus',
abs => 'ii_abs',
$k = MyClassWithTraits->new(ii => 10);
$k->ii_minus(100);
is $k->get_ii, -90;
-is $k->ii_abs, 90;
+$k->ii_abs;
+is $k->get_ii, 90;
$k->set_ii(10);
is $k->get_ii, 10;
-is $k->ii_abs, 10;
+$k->ii_abs;
+is $k->get_ii, 10;