Framework/Document.pm

filter: Statementstmt 62.0% 18 / 29 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 4 / 7 Podpod 100% 2 / 2 totaltotal 42.8% 24 / 56 CC 7 SCAR 30.3
CC
7
Cov
42%
load
42.0
__ANON__
6.0
save
2.0
CRAP 20.8
« Framework/VolSurface.pm ↑ Summary Framework/Spot/DatabaseAPI.pm »
1 package Quant::Framework::Document;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Date::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Quant::Framework::Document - Binds data with Chronicle
12  
13 =head1 DESCRIPTION
14  
15 Internal representation of persistend data. Do not B<create> the class directly outside
16 of Quant::Framework, although the usage of public fields outside of Quant::Framework
17 is allowed.
18  
19 The class is responsible for loading and storing data via Data::Chronicle. The
20 data itself is a hash, which content is provided by users of the class (i.e. by
21 CorporateActions).
22  
23 # create new (transient / not-yet-persisted) Document
24  
25 my $document = Quant::Framework::Document->new(
26 storage_accessor => $storage_accessor,
27 symbol => 'frxUSDJPY',
28 data => {},
29 for_date => Date::Utility->new,
30 );
31  
32 # persist document
33 $document->save('currency');
34  
35 # load document
36 my $document2 = Quant::Framework::Document::load(
37 $storage_accessor,
38 'currency',
39 'frxUSDJPY',
40 Date::Utility->new, # optional
41 )
42  
43 =cut
44  
45 =head1 ATTRIBUTES
46  
47 =head2 storage_accessor
48  
49 Chronicle assessor
50  
51 =head2 recorded_date
52  
53 The date of document creation (C<Date::Utility>)
54  
55 =head2 data
56  
57 Hashref of data. Should be defined by the class, which uses Document. Currently the fields
58 C<date> and C<symbol> are reserved.
59  
60 =head2 namespace
61  
62 The required namespace of document, e.g. 'corporate_actions'
63  
64 =head2 symbol
65  
66 The domain-specific name of document; e.g. "USAAPL" for corporate actions
67  
68 =cut
69  
70 2 has storage_accessor => (
71 is => 'ro',
72 required => 1,
73 );
74  
75 has recorded_date => (
76 is => 'ro',
77 isa => sub {
78 0 ▶ die("Quant::Framework::Document::recorded_date should be Date::Utility")
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "Date::Utility" 0 0 0
79 unless ref($_[0]) eq 'Date::Utility';
80 },
81 2 required => 1,
82 );
83  
84 2 has data => (
85 is => 'ro',
86 required => 1,
87 );
88  
89 2 has namespace => (
90 is => 'ro',
91 required => 1,
92 );
93  
94 2 has symbol => (
95 is => 'ro',
96 required => 1,
97 );
98  
99 =head1 SUBROUTINES
100  
101 =head2 load($storage_accessor, $namespace, $symbol, $for_date)
102  
103 my $recent_document = Quant::Framework::Document::load($storage_accessor, 'corporate_actions', "USAAPL");
104  
105 my $event_date = Date::Utility->new('02-02-2016')
106 my $historical_doc = Quant::Framework::Document::load($storage_accessor, 'corporate_actions', "USAAPL", $event_date);
107  
108 Loads the document. If the argument C<for_date> is ommitted, then loads the most recent document.
109  
110 If document is not found, returns C<undef>.
111  
112 =cut
113  
114 sub load {
115 0 ▶ my ($storage_accessor, $namespace, $symbol, $for_date) = @_;
Subroutine load: not called CC=6 SCAR=37.4
Pod documented
116  
117 0 ▶ my $data = $storage_accessor->chronicle_reader->get($namespace, $symbol)
Branch: 1 branch
BranchTrueFalseTotal
my $data = $storage_accessor->chronicle_reader->get($namespace, $symbol) or return 0 0 0
118 or return;
119  
120 0 ▶ if ($for_date && $for_date->datetime_iso8601 lt $data->{date}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for_date and $for_date->datetime_iso8601 lt $data->{"date"}) 0 0 0
Condition0%0/3
$for_date and $for_date->datetime_iso8601 lt $data->{"date"}
!ll&&!rl&&r
000
MC/DC0%0/2
$for_date and $for_date->datetime_iso8601 lt $data->{"date"}
$for_date $for_date->datetime_iso8601 lt $data->{"date"}
Truth table0%0/3
A and B
A= $for_dateB= $for_date->datetime_iso8601 lt $data->{"date"}
ABresult
0X0
100
111
121 0 ▶ $data = $storage_accessor->chronicle_reader->get_for($namespace, $symbol, $for_date->epoch)
Branch: 1 branch
BranchTrueFalseTotal
$data = $storage_accessor->chronicle_reader->get_for($namespace, $symbol, $for_date->epoch) or return 0 0 0
122 or return;
123 }
124  
125 return __PACKAGE__->new(
126 storage_accessor => $storage_accessor,
127 0 ▶ recorded_date => $for_date // Date::Utility->new($data->{date}),
Condition0%0/3
$for_date // "Date::Utility"->new($data->{"date"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$for_date // "Date::Utility"->new($data->{"date"})
$for_date "Date::Utility"->new($data->{"date"})
Truth table0%0/3
A // B
A= $for_dateB= "Date::Utility"->new($data->{"date"})
ABresult
1X1
011
000
128 symbol => $symbol,
129 data => $data,
130 namespace => $namespace,
131 );
132 }
133  
134 =head2 save
135  
136 $document->save;
137  
138 Stores (persists) the document in Chronicle database.
139  
140 =cut
141  
142 sub save {
143 0 ▶ my $self = shift;
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
144 # the most probably this is redundant, anc can be removed in future
145 0 $self->data->{date} = $self->recorded_date->datetime_iso8601;
146 0 $self->data->{symbol} = $self->symbol;
147 0 $self->storage_accessor->chronicle_writer->set($self->namespace, $self->symbol, $self->data, $self->recorded_date);
148 0 return;
149 }
150  
151 2 1;
« Framework/VolSurface.pm ↑ Summary Framework/Spot/DatabaseAPI.pm »