From: stevan Date: Wed, 9 Aug 2006 00:43:56 +0000 (+0000) Subject: JavaAccessors forPeopleWhoLikeJava X-Git-Tag: 0_01~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=08e608bd50d9d5d861e3319662c5d71dd7077cd4;p=gitmo%2FMoose-Policy.git JavaAccessors forPeopleWhoLikeJava --- diff --git a/lib/Moose/Policy/JavaAccessors.pm b/lib/Moose/Policy/JavaAccessors.pm new file mode 100644 index 0000000..fea56dd --- /dev/null +++ b/lib/Moose/Policy/JavaAccessors.pm @@ -0,0 +1,36 @@ + +package Moose::Policy::JavaAccessors; + +use constant attribute_metaclass => 'Moose::Policy::JavaAccessors::Attribute'; + +package Moose::Policy::JavaAccessors::Attribute; +use Moose; + +extends 'Moose::Meta::Attribute'; + +before '_process_options' => sub { + my ($class, $name, $options) = @_; + # NOTE: + # If is has been specified, and + # we don't have a reader or writer + # Of couse this is an odd case, but + # we better test for it anyway. + if (exists $options->{is} && !(exists $options->{reader} || exists $options->{writer})) { + if ($options->{is} eq 'ro') { + $options->{reader} = 'get' . ucfirst($name); + } + elsif ($options->{is} eq 'rw') { + $options->{reader} = 'get' . ucfirst($name); + $options->{writer} = 'set' . ucfirst($name); + } + delete $options->{is}; + } +}; + +1; + +__END__ + +=pod + +=cut diff --git a/t/011_JavaAccessors_test.t b/t/011_JavaAccessors_test.t new file mode 100644 index 0000000..93540c9 --- /dev/null +++ b/t/011_JavaAccessors_test.t @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More 'no_plan'; + +BEGIN { + use_ok('Moose::Policy'); +} + +{ + package Foo; + + use Moose::Policy 'Moose::Policy::JavaAccessors'; + use Moose; + + has 'bar' => (is => 'rw', default => 'Foo::bar'); + has 'baz' => (is => 'ro', default => 'Foo::baz'); +} + +isa_ok(Foo->meta, 'Moose::Meta::Class'); +is(Foo->meta->attribute_metaclass, 'Moose::Policy::JavaAccessors::Attribute', '... got our custom attr metaclass'); + +isa_ok(Foo->meta->get_attribute('bar'), 'Moose::Policy::JavaAccessors::Attribute'); + +my $foo = Foo->new; +isa_ok($foo, 'Foo'); + +can_ok($foo, 'getBar'); +can_ok($foo, 'setBar'); + +can_ok($foo, 'getBaz'); +ok(! $foo->can('setBaz'), 'without setter'); + +is($foo->getBar, 'Foo::bar', '... got the right default value'); +is($foo->getBaz, 'Foo::baz', '... got the right default value'); +