Search code examples
perlmoose

Moose attributes: separating data and behaviour


I have a class built with Moose that's essentially a data container for an article list. All the attributes - like name, number, price, quantity - are data. "Well, what else?", I can hear you say. So what else?

An evil conspiration of unfortunate circumstances now forces external functionality into that package: Tax calculation of the data in this class has to be performed by an external component. This external component is tightly coupled to an entire application including database and dependencies that ruin the component's testability, dragging it into the everything-coupled-together stew. (Even thinking about refactoring the tax component out of the stew is completely out of the question.)

So my idea is to have the class accept a coderef wrapping the tax calculation component. The class would then remain independent of the tax calculation implementation (and its possible nightmare of dependencies), and at the same time it would allow integration with the application environment.

has 'tax_calculator', is => 'ro', isa => 'CodeRef';

But then, I'd have added a non-data component to my class. Why is that a problem? Because I'm (ab)using $self->meta->get_attribute_list to assemble a data export for my class:

my %data; # need a plain hash, no objects
my @attrs = $self->meta->get_attribute_list;
$data{ $_ } = $self->$_ for @attrs;
return %data;

Now the coderef is part of the attribute list. I could filter it out, of course. But I'm unsure any of what I'm doing here is a sound way to proceed. So how would you handle this problem, perceived as the need to separate data attributes and behaviour attributes?


Solution

  • A possible half thought out solution: use inheritance. Create your class as you do today but with a calculate_tax method that dies if called (i.e. a virtual function). Then create subclass that overrides that method to call into the external system. You can test the base class and use the child class.

    Alternate solution: use a role to add the calculate_tax method. You can create two roles: Calculate::Simple::Tax and Calculate::Real::Tax. When testing you add the simple role, in production you add the real role.

    I whipped up this example, but I don't use Moose, so I may be crazy with respect to how to apply the role to the class. There may be some more Moosey way of doing this:

    #!/usr/bin/perl
    
    use warnings;
    
    {
        package Simple::Tax;
        use Moose::Role;
    
        requires 'price';
    
        sub calculate_tax {
            my $self = shift;
            return int($self->price * 0.05);
        }
    }
    
    
    {
        package A;
        use Moose;
        use Moose::Util qw( apply_all_roles );
    
        has price => ( is => "rw", isa => 'Int' ); #price in pennies
    
        sub new_with_simple_tax {
            my $class = shift;
            my $obj = $class->new(@_);
            apply_all_roles( $obj, "Simple::Tax" );
        }
    }
    
    my $o = A->new_with_simple_tax(price => 100);
    print $o->calculate_tax, " cents\n";
    

    It appears as if the right way to do it in Moose is to use two roles. The first is applied to the class and contains the production code. The second is applied to an object you want to use in testing. It subverts the first method using an around method and never calls the original method:

    #!/usr/bin/perl
    
    use warnings;
    
    {
        package Complex::Tax;
        use Moose::Role;
    
        requires 'price';
    
        sub calculate_tax {
            my $self = shift;
            print "complex was called\n";
            #pretend this is more complex
            return int($self->price * 0.15);
        }
    }
    
    {
        package Simple::Tax;
        use Moose::Role;
    
        requires 'price';
    
        around calculate_tax => sub {
            my ($orig_method, $self) = @_;
            return int($self->price * 0.05);
        }
    }
    
    
    {
        package A;
        use Moose;
    
        has price => ( is => "rw", isa => 'Int' ); #price in pennies
    
        with "Complex::Tax";
    }
    
    my $prod = A->new(price => 100);
    print $prod->calculate_tax, " cents\n";
    
    use Moose::Util qw/ apply_all_roles /;
    my $test = A->new(price => 100);
    apply_all_roles($test, 'Simple::Tax');
    print $test->calculate_tax, " cents\n";