summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-06-01 01:55:08 -0500
committerJesse Luehrs <doy@tozt.net>2009-06-01 01:55:08 -0500
commitb25a41e690cfeea828e4a06d3c7cefabc3113898 (patch)
treec794c7e247aaa9d7609df339ad0d6b48bd0bdbf2
parentb74e5167fea789d09b7fe05c2f93b8ef07fe2eb9 (diff)
downloadbot-games-b25a41e690cfeea828e4a06d3c7cefabc3113898.tar.gz
bot-games-b25a41e690cfeea828e4a06d3c7cefabc3113898.zip
make the default formatters an attribute of the metaclass
-rw-r--r--lib/Bot/Games/OO/Game.pm3
-rw-r--r--lib/Bot/Games/Trait/Attribute/Formatted.pm38
-rw-r--r--lib/Bot/Games/Trait/Class/Formatted.pm27
3 files changed, 40 insertions, 28 deletions
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;