File Coverage

blib/lib/DBIx/Class/AuditAny/DataPoint.pm
Criterion Covered Total %
statement 16 16 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 24 24 100.0


line stmt bran cond sub pod time code
1             package DBIx::Class::AuditAny::DataPoint;
2 14     14   7619 use strict;
  14         45  
  14         467  
3 14     14   81 use warnings;
  14         44  
  14         446  
4              
5             # ABSTRACT: Object class for AuditAny datapoint configs
6              
7             =head1 NAME
8              
9             DBIx::Class::AuditAny::DataPoint - Object class for AuditAny datapoint configs
10              
11             =head1 DESCRIPTION
12              
13             This class defines the *config* of a datapoint, not the *value*
14             of the datapoint. It is used to get the value, but the value itself
15             is not stored within this object. Datapoint values are stored within
16             the Context objects whose life-cycle is limited to individual tracked
17             database operations, and -only- after being called from the Collector.
18              
19             Datapoints are just only optional sugar for abstracting and simplifying
20             useful collection of data as key/values. It just provides a nice way to
21             organize data that has already been retrieved. They are *never* automatically
22             calculated; only made available to the Collector. Note that this means
23             datapoint values are only retrieved -after- the database operation is completed,
24             and thus only have access to the data that has already been collected (or is
25             otherwise available to) the given Context object. I thought long and hard
26             about this design... At one point I was going to expand the paradigm to
27             provide 'pre' vs 'post' hooks, but ultimately decided that this would be
28             overkill and over-complicate things. To accomplish custom collection of
29             data at the 'pre' stage (i.e. *before* the tracked database operation is
30             executed) a custom Context object should be written. This could then of
31             course be paired with a custom datapoint config to access this extra data
32             in the custom Context, but that is incidental (i.e. just organizing the data
33             after the actual work to collect it)
34              
35             =head1 ATTRIBUTES
36              
37             =cut
38              
39 14     14   89 use Moo;
  14         34  
  14         89  
40 14     14   22646 use MooX::Types::MooseLike::Base 0.19 qw(:all);
  14         414  
  14         10282  
41              
42             =head2 AuditObj
43              
44             Required. Reference to the Auditor object (L<DBIx::Class::AuditAny> instance).
45              
46             =cut
47             has 'AuditObj', is => 'ro', isa => InstanceOf['DBIx::Class::AuditAny'], required => 1;
48              
49             =head2 name
50              
51             The unique name of the DataPoint (i.e. 'key')
52              
53             =cut
54             has 'name', is => 'ro', required => 1, isa => sub {
55             $_[0] =~ /^[a-z0-9\_\-]+$/ or die "'$_[0]' is invalid - " .
56             "only lowercase letters, numbers, underscore(_) and dash(-) allowed";
57             };
58              
59             =head2 context
60              
61             The name of the -context- which determines at what point the value
62             can be computed and collected, and into which Context -object- it
63             will be cached (although, since Context objects overlay in a hierarchy,
64             lower-level contexts can automatically access the datapoint values of the
65             higher-level contexts (i.e. 'set' datapoints are implied in 'change'
66             context but not 'column' datapoints. This is just standard belongs_to vs
67             has_many logic based on the way contexts are interrelated, regardless of
68             how or if they are actually stored)
69              
70             =cut
71             has 'context', is => 'ro', required => 1,
72             isa => Enum[qw(base source set change column)];
73              
74             =head2 method
75              
76             method is what is called to get the value of the datapoint. It is a
77             CodeRef and is supplied the Context object (ChangeSet, Change, Column, etc)
78             as the first argument. As a convenience, it can also be a Str in which
79             case it is an existing method name within the Context object
80              
81             =cut
82             has 'method', is => 'ro', isa => AnyOf[CodeRef,Str], required => 1;
83              
84             =head2 user_defined
85              
86             Informational flag set to identify if this datapoint has been
87             defined custom, on-the-fly, or is a built-in
88              
89             =cut
90             has 'user_defined', is => 'ro', isa => Bool, default => sub{0};
91              
92             =head2 original_name
93              
94             Optional extra attr to keep track of a separate 'original' name. Auto
95             set when 'rename_datapoints' are specified (see top DBIx::Class::AuditAny class)
96              
97             =cut
98             has 'original_name', is => 'ro', isa => Str, lazy => 1,
99             default => sub { (shift)->name };
100              
101             =head2 column_info
102              
103             defines the schema needed to store this datapoint within
104             a DBIC Result/table. Only used in collectors like Collector::AutoDBIC
105              
106             =cut
107             has 'column_info', is => 'ro', isa => HashRef, lazy => 1,
108             default => sub { my $self = shift; $self->_get_column_info->($self) };
109            
110             has '_get_column_info', is => 'ro', isa => CodeRef, lazy => 1,
111             default => sub {{ data_type => "varchar" }};
112             # --
113              
114             =head1 METHODS
115              
116             =head2 get_value
117              
118             Returns the value of the datapoint via the C<'method'> function/CodeRef
119              
120             =cut
121             sub get_value {
122 1231     1231 1 1932 my $self = shift;
123 1231         1726 my $Context = shift;
124 1231         2364 my $method = $self->method;
125 1231 100       14233 return ref($method) ? $method->($self,$Context,@_) : $Context->$method(@_);
126             }
127              
128             1;
129              
130             __END__
131              
132             =head1 SEE ALSO
133              
134             =over
135              
136             =item *
137              
138             L<DBIx::Class::AuditAny>
139              
140             =item *
141              
142             L<DBIx::Class>
143              
144             =back
145              
146             =head1 SUPPORT
147            
148             IRC:
149            
150             Join #rapidapp on irc.perl.org.
151              
152             =head1 AUTHOR
153              
154             Henry Van Styn <vanstyn@cpan.org>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2012-2015 by IntelliTree Solutions llc.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut