summaryrefslogtreecommitdiffstats
path: root/lib/Bot/Games/Trait/Attribute/Formatted.pm
blob: dd40a19ef9012da0f4cc96f5e752c2e01f6a1d17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package Bot::Games::Trait::Attribute::Formatted;
use Moose::Role;

# 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
has formatter => (
    is        => 'rw',
    isa       => 'CodeRef',
    predicate => 'has_formatter',
);

after install_accessors => sub {
    my $self = shift;
    if ($self->has_formatter) {
        my $formatter = $self->formatter;
        my $method_meta = $self->get_read_method_ref;
        $method_meta->formatter($formatter)
            if $method_meta->can('formatter');
    }
};

no Moose::Role;

1;