summaryrefslogtreecommitdiffstats
path: root/lib/Bread/Board/Declare/Meta/Role/Attribute.pm
blob: a73828c5a532cd955162cb83402ae69caba2dee9 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Bread::Board::Declare::Meta::Role::Attribute;
use Moose::Role;
# ABSTRACT: base attribute metarole for Bread::Board::Declare

use Moose::Util 'does_role', 'find_meta';

use Bread::Board::Declare::Meta::Role::Attribute::Container;
use Bread::Board::Declare::Meta::Role::Attribute::Service;

=attr service

Whether or not to create a service for this attribute. Defaults to true.

=cut

has service => (
    is      => 'ro',
    isa     => 'Bool',
    default => 1,
);

# this is kinda gross, but it's the only way to hook in at the right place
# at the moment, it seems
around interpolate_class => sub {
    my $orig = shift;
    my $class = shift;
    my ($options) = @_;

    # we only want to do this on the final recursive call
    return $class->$orig(@_)
        if $options->{metaclass};

    return $class->$orig(@_)
        if exists $options->{service} && !$options->{service};

    my ($new_class, @traits) = $class->$orig(@_);

    return wantarray ? ($new_class, @traits) : $new_class
        if does_role($new_class, 'Bread::Board::Declare::Meta::Role::Attribute::Service')
        || does_role($new_class, 'Bread::Board::Declare::Meta::Role::Attribute::Container');

    my $parent = @traits
        ? (find_meta($new_class)->superclasses)[0]
        : $new_class;
    push @{ $options->{traits} }, 'Bread::Board::Declare::Meta::Role::Attribute::Service';

    return $parent->interpolate_class($options);
};

no Moose::Role;

1;