From b25a41e690cfeea828e4a06d3c7cefabc3113898 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 1 Jun 2009 01:55:08 -0500 Subject: make the default formatters an attribute of the metaclass --- lib/Bot/Games/OO/Game.pm | 3 ++- lib/Bot/Games/Trait/Attribute/Formatted.pm | 38 +++++++++--------------------- lib/Bot/Games/Trait/Class/Formatted.pm | 27 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 lib/Bot/Games/Trait/Class/Formatted.pm (limited to 'lib/Bot') diff --git a/lib/Bot/Games/OO/Game.pm b/lib/Bot/Games/OO/Game.pm index 3921a0d..0bc294a 100644 --- a/lib/Bot/Games/OO/Game.pm +++ b/lib/Bot/Games/OO/Game.pm @@ -50,7 +50,8 @@ sub init_meta { ['Bot::Games::Trait::Attribute::Command', 'Bot::Games::Trait::Attribute::Formatted'], metaclass_roles => - ['Bot::Games::Trait::Class::Command'], + ['Bot::Games::Trait::Class::Command', + 'Bot::Games::Trait::Class::Formatted'], ); return $options{for_class}->meta; } diff --git a/lib/Bot/Games/Trait/Attribute/Formatted.pm b/lib/Bot/Games/Trait/Attribute/Formatted.pm index b94e887..303c1bc 100644 --- a/lib/Bot/Games/Trait/Attribute/Formatted.pm +++ b/lib/Bot/Games/Trait/Attribute/Formatted.pm @@ -1,21 +1,6 @@ package Bot::Games::Trait::Attribute::Formatted; use Moose::Role; -my %default_formatters = ( - 'ArrayRef' => sub { - my $arrayref = shift; - return join ', ', @$arrayref; - }, - 'Bool' => sub { - my $bool = shift; - return $bool ? 'true' : 'false'; - }, - 'Object' => sub { - my $obj = shift; - return "$obj"; - }, -); - # when the attribute is being constructed, the accessor methods haven't been # generated yet, so we need to store the formatter here, and then apply it # after the accessor methods exist @@ -32,18 +17,17 @@ before _process_options => sub { if exists($options->{formatter}) && !$options->{command}; }; -after _process_options => sub { - my $class = shift; - my ($name, $options) = @_; - return if exists $options->{formatter}; - return unless $options->{command}; - if (exists $options->{type_constraint}) { - my $tc = $options->{type_constraint}; - for my $tc_type (keys %default_formatters) { - if ($tc->is_a_type_of($tc_type)) { - $options->{formatter} = $default_formatters{$tc_type}; - return; - } +after attach_to_class => sub { + my $self = shift; + my ($meta) = @_; + return if $self->has_formatter; + return unless $self->command; + return unless $self->has_type_constraint; + my $tc = $self->type_constraint; + for my $tc_name ($meta->formattable_tcs) { + if ($tc->is_a_type_of($tc_name)) { + $self->formatter($meta->formatter_for($tc_name)); + return; } } }; diff --git a/lib/Bot/Games/Trait/Class/Formatted.pm b/lib/Bot/Games/Trait/Class/Formatted.pm new file mode 100644 index 0000000..403bba6 --- /dev/null +++ b/lib/Bot/Games/Trait/Class/Formatted.pm @@ -0,0 +1,27 @@ +package Bot::Games::Trait::Class::Formatted; +use Moose::Role; +use MooseX::AttributeHelpers; + +has default_formatters => ( + metaclass => 'Collection::ImmutableHash', + is => 'ro', + isa => 'HashRef[CodeRef]', + builder => '_build_default_formatters', + provides => { + get => 'formatter_for', + exists => 'has_formatter', + keys => 'formattable_tcs', + }, +); + +sub _build_default_formatters { + { + 'ArrayRef' => sub { join ', ', @{ shift() } }, + 'Bool' => sub { return shift() ? 'true' : 'false' }, + 'Object' => sub { shift() . "" }, + } +} + +no Moose::Role; + +1; -- cgit v1.2.3-54-g00ecf