File Coverage

blib/lib/OpenERP/OOM/Roles/Attribute.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             OpenERP::OOM::Roles::Attribute - Meta attribute for implementing dirty attribute tracking
4              
5             =head1 DESCRIPTION
6              
7             This code was largely taken from a version of MooseX::TrackDirty before it
8             was updated to work with Moose 2.0. Then it was cut down to suit our purposes
9             being uses in the Moose::Exporter.
10              
11             =head1 LICENSE AND COPYRIGHT
12              
13             Copyright (C) 2011 OpusVL
14              
15             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
16              
17             =cut
18              
19             package OpenERP::OOM::Roles::Attribute;
20 3     3   543579 use namespace::autoclean;
  3         6572  
  3         23  
21 3     3   577 use Moose::Role;
  3         4364  
  3         21  
22              
23             has track_dirty => (is => 'rw', isa => 'Bool', default => 1);
24             has dirty => (is => 'ro', isa => 'Str', predicate => 'has_dirty');
25              
26             has track_attribute_helpers_dirty =>
27             (is => 'rw', isa => 'Bool', default => 1);
28              
29              
30             # wrap our internal clearer
31             after clear_value => sub {
32             my ($self, $instance) = @_;
33              
34             $instance->_mark_clean($self->name) if $self->track_dirty;
35             };
36              
37             after install_accessors => sub {
38             my ($self, $inline) = @_;
39              
40             ### in install_accessors, installing if: $self->track_dirty
41             return unless $self->track_dirty;
42              
43             my $class = $self->associated_class;
44             my $name = $self->name;
45              
46             ### is_dirty: $self->dirty || ''
47             $class->add_method($self->dirty, sub { shift->_is_dirty($name) })
48             if $self->has_dirty;
49              
50             $class->add_after_method_modifier(
51             $self->clearer => sub { shift->_mark_clean($name) }
52             ) if $self->has_clearer;
53              
54             # if we're set, we're dirty (cach both writer/accessor)
55             $class->add_after_method_modifier(
56             $self->writer => sub { shift->_mark_dirty($name) }
57             ) if $self->has_writer;
58             $class->add_after_method_modifier(
59             $self->accessor =>
60             sub { $_[0]->_mark_dirty($name) if exists $_[1] }
61             ) if $self->has_accessor;
62              
63             return;
64             };
65              
66             before _process_options => sub {
67             my ($self, $name, $options) = @_;
68              
69             ### before _process_options: $name
70             $options->{dirty} = $name.'_is_dirty'
71             unless exists $options->{dirty} || !$options->{lazy_build};
72              
73             return;
74             };
75              
76             1;
77