From: Karen Etheridge Date: Sat, 22 Dec 2012 07:19:52 +0000 (-0800) Subject: prefer trait over metaclass as much as possible X-Git-Tag: v0.48~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=195fb408a61ad34e0e41887b4f9f8dc384dd5da5;p=gitmo%2FMooseX-Getopt.git prefer trait over metaclass as much as possible --- diff --git a/dist.ini b/dist.ini index 8ae73e6..206412a 100644 --- a/dist.ini +++ b/dist.ini @@ -34,6 +34,7 @@ Test::Requires = 0.05 Test::Trap = 0 Path::Class = 0 Test::NoWarnings = 1.04 +Test::Moose = 0 [InstallGuide] [MetaConfig] diff --git a/lib/MooseX/Getopt/Basic.pm b/lib/MooseX/Getopt/Basic.pm index c86de1f..0f8a7c5 100644 --- a/lib/MooseX/Getopt/Basic.pm +++ b/lib/MooseX/Getopt/Basic.pm @@ -11,8 +11,8 @@ use Carp (); use Getopt::Long 2.37 (); -has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); -has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); +has ARGV => (is => 'rw', isa => 'ArrayRef', traits => ['NoGetopt']); +has extra_argv => (is => 'rw', isa => 'ArrayRef', traits => ['NoGetopt']); sub process_argv { my ($class, @params) = @_; diff --git a/lib/MooseX/Getopt/Meta/Attribute.pm b/lib/MooseX/Getopt/Meta/Attribute.pm index fd89a3f..6a37acd 100644 --- a/lib/MooseX/Getopt/Meta/Attribute.pm +++ b/lib/MooseX/Getopt/Meta/Attribute.pm @@ -52,6 +52,17 @@ which L will create for you. This is certainly not the prettiest way to go about this, but for now it works for those who might need such a feature. +=head2 Use 'traits' instead of 'metaclass' + +You should rarely need to explicitly set the attribute metaclass. It is much +preferred to simply provide a trait (a role applied to the attribute +metaclass), which allows other code to futher modify the attribute by applying +additional roles. + +Therefore, you should first try to do this: + + has 'foo' => (traits => ['Getopt'], cmd_flag => 'f'); + =head2 Custom Metaclass alias This now takes advantage of the Moose 0.19 feature to support diff --git a/lib/MooseX/Getopt/Meta/Attribute/NoGetopt.pm b/lib/MooseX/Getopt/Meta/Attribute/NoGetopt.pm index f57aa99..c665950 100644 --- a/lib/MooseX/Getopt/Meta/Attribute/NoGetopt.pm +++ b/lib/MooseX/Getopt/Meta/Attribute/NoGetopt.pm @@ -38,6 +38,17 @@ metaclass. has 'foo' => (metaclass => 'MooseX::Getopt::Meta::Attribute::NoGetopt', ... ); +=head2 Use 'traits' instead of 'metaclass' + +You should rarely need to explicitly set the attribute metaclass. It is much +preferred to simply provide a trait (a role applied to the attribute +metaclass), which allows other code to futher modify the attribute by applying +additional roles. + +Therefore, you should first try to do this: + + has 'foo' => (traits => ['NoGetopt', ...], ...); + =head2 Custom Metaclass alias This now takes advantage of the Moose 0.19 feature to support diff --git a/t/001_basic.t b/t/001_basic.t index fd340f6..92f3fbc 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More tests => 70; +use Test::Moose; use Test::NoWarnings 1.04 ':early'; BEGIN { @@ -15,7 +16,7 @@ BEGIN { with 'MooseX::Getopt'; has 'data' => ( - metaclass => 'MooseX::Getopt::Meta::Attribute', + metaclass => 'Getopt', is => 'ro', isa => 'Str', default => 'file.dat', @@ -31,7 +32,7 @@ BEGIN { ); has 'horse' => ( - metaclass => 'MooseX::Getopt::Meta::Attribute', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'bray', @@ -69,7 +70,7 @@ BEGIN { ); has '_private_stuff_cmdline' => ( - metaclass => 'MooseX::Getopt::Meta::Attribute', + traits => ['Getopt'], is => 'ro', isa => 'Int', default => 832, @@ -81,7 +82,14 @@ BEGIN { foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) { my $attr = App->meta->get_attribute($attr_name); isa_ok($attr, 'Moose::Meta::Attribute'); - isa_ok($attr, 'MooseX::Getopt::Meta::Attribute'); + if ($attr_name eq 'data' or $attr_name eq 'cow') + { + isa_ok($attr, 'MooseX::Getopt::Meta::Attribute'); + } + else + { + does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait'); + } can_ok($attr, 'cmd_flag'); can_ok($attr, 'cmd_aliases'); } diff --git a/t/004_nogetop.t b/t/004_nogetop.t index b90b914..9592ed4 100644 --- a/t/004_nogetop.t +++ b/t/004_nogetop.t @@ -16,7 +16,7 @@ BEGIN { with 'MooseX::Getopt'; has 'data' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'file.dat', @@ -24,7 +24,7 @@ BEGIN { ); has 'cow' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'moo', @@ -32,7 +32,7 @@ BEGIN { ); has 'horse' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'bray', @@ -64,14 +64,14 @@ BEGIN { ); has 'private_stuff' => ( - metaclass => 'NoGetopt', + traits => ['NoGetopt'], is => 'ro', isa => 'Int', default => 713 ); has '_private_stuff_cmdline' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Int', default => 832, diff --git a/t/005_strict.t b/t/005_strict.t index 03eaf50..484f11e 100644 --- a/t/005_strict.t +++ b/t/005_strict.t @@ -17,7 +17,7 @@ BEGIN { with 'MooseX::Getopt::Strict'; has 'data' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'file.dat', @@ -25,7 +25,7 @@ BEGIN { ); has 'cow' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'moo', diff --git a/t/100_gld_default_bug.t b/t/100_gld_default_bug.t index 40ca140..85e10e3 100644 --- a/t/100_gld_default_bug.t +++ b/t/100_gld_default_bug.t @@ -17,7 +17,7 @@ use_ok('MooseX::Getopt'); with 'MooseX::Getopt'; has 'nproc' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Int', default => sub { 1 }, diff --git a/t/102_basic_basic.t b/t/102_basic_basic.t index cf7cc19..28e95fe 100644 --- a/t/102_basic_basic.t +++ b/t/102_basic_basic.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More tests => 70; +use Test::Moose; use Test::NoWarnings 1.04 ':early'; BEGIN { @@ -15,7 +16,7 @@ BEGIN { with 'MooseX::Getopt::Basic'; has 'data' => ( - metaclass => 'MooseX::Getopt::Meta::Attribute', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'file.dat', @@ -23,7 +24,7 @@ BEGIN { ); has 'cow' => ( - metaclass => 'Getopt', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'moo', @@ -31,7 +32,7 @@ BEGIN { ); has 'horse' => ( - metaclass => 'MooseX::Getopt::Meta::Attribute', + traits => ['Getopt'], is => 'ro', isa => 'Str', default => 'bray', @@ -69,7 +70,7 @@ BEGIN { ); has '_private_stuff_cmdline' => ( - metaclass => 'MooseX::Getopt::Meta::Attribute', + traits => ['Getopt'], is => 'ro', isa => 'Int', default => 832, @@ -81,7 +82,7 @@ BEGIN { foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) { my $attr = App->meta->get_attribute($attr_name); isa_ok($attr, 'Moose::Meta::Attribute'); - isa_ok($attr, 'MooseX::Getopt::Meta::Attribute'); + does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait'); can_ok($attr, 'cmd_flag'); can_ok($attr, 'cmd_aliases'); } diff --git a/t/111_gld_pass_through.t b/t/111_gld_pass_through.t index faecb98..bf80680 100644 --- a/t/111_gld_pass_through.t +++ b/t/111_gld_pass_through.t @@ -17,7 +17,6 @@ use_ok('MooseX::Getopt::GLD'); with 'MooseX::Getopt::GLD' => { getopt_conf => [ 'pass_through' ] }; has 'foo' => ( - metaclass => 'Getopt', is => 'ro', isa => 'Int', ); @@ -30,7 +29,6 @@ use_ok('MooseX::Getopt::GLD'); with 'MooseX::Getopt::GLD' => { getopt_conf => [ 'pass_through' ] };; has 'bar' => ( - metaclass => 'Getopt', is => 'ro', isa => 'Int', );