Class: ESA::Context
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ESA::Context
show all
- Defined in:
- app/models/esa/context.rb
Overview
The Context provides a persisted filtered view on the objects related to a Chart.
Instance Method Summary
collapse
Instance Method Details
#amounts ⇒ Object
56
57
58
|
# File 'app/models/esa/context.rb', line 56
def amounts
self.apply(self.unscoped_amounts)
end
|
#apply(relation) ⇒ Object
60
61
62
63
64
|
# File 'app/models/esa/context.rb', line 60
def apply(relation)
self.effective_contexts.inject(relation) do |r,context|
context.inject_filters(r)
end
end
|
#can_be_persisted? ⇒ Boolean
181
182
183
|
# File 'app/models/esa/context.rb', line 181
def can_be_persisted?
self.type.present?
end
|
#change_total ⇒ Object
173
174
175
176
177
178
179
|
# File 'app/models/esa/context.rb', line 173
def change_total
if self.debits_total.present? and self.credits_total.present?
self.debits_total - self.credits_total
else
nil
end
end
|
#check_freshness(depth = 0, options = {}) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'app/models/esa/context.rb', line 82
def check_freshness(depth=0, options = {})
if self.is_update_needed? or not has_subcontext_namespaces?(options[:namespace])
self.update!(options)
else
self.update_freshness_timestamp!
end
if depth > 0 and self.last_transaction_time.present?
self.subcontexts.each do |sub|
if sub.freshness.nil? or sub.freshness <= self.last_transaction_time
sub.check_freshness(depth - 1)
end
end
end
end
|
#closing_context ⇒ Object
165
166
167
168
169
170
171
|
# File 'app/models/esa/context.rb', line 165
def closing_context
if self.effective_end_date.present?
Contexts::OpenCloseContext.new(chart: self.chart, parent: self, end_date: self.effective_end_date, namespace: 'closing')
else
Contexts::OpenCloseContext.new(chart: self.chart, parent: self, namespace: 'closing')
end
end
|
#effective_contexts ⇒ Object
140
141
142
|
# File 'app/models/esa/context.rb', line 140
def effective_contexts
self.parents_and_self
end
|
#effective_end_date ⇒ Object
152
153
154
|
# File 'app/models/esa/context.rb', line 152
def effective_end_date
self.effective_contexts.map(&:end_date).compact.min
end
|
#effective_path ⇒ Object
144
145
146
|
# File 'app/models/esa/context.rb', line 144
def effective_path
self.effective_contexts.map{|ctx| ctx.namespace || ""}
end
|
#effective_start_date ⇒ Object
148
149
150
|
# File 'app/models/esa/context.rb', line 148
def effective_start_date
self.effective_contexts.map(&:start_date).compact.max
end
|
#events ⇒ Object
44
45
46
|
# File 'app/models/esa/context.rb', line 44
def events
self.apply(self.unscoped_events)
end
|
#flags ⇒ Object
48
49
50
|
# File 'app/models/esa/context.rb', line 48
def flags
self.apply(self.unscoped_flags)
end
|
#has_subcontext_namespaces?(*namespaces) ⇒ Boolean
78
79
80
|
# File 'app/models/esa/context.rb', line 78
def has_subcontext_namespaces?(*namespaces)
(namespaces.flatten.compact - subcontext_namespaces).none?
end
|
#is_fresh?(time = Time.zone.now) ⇒ Boolean
74
75
76
|
# File 'app/models/esa/context.rb', line 74
def is_fresh?(time=Time.zone.now)
self.freshness.present? and (self.freshness + ESA.configuration.context_freshness_threshold) > time
end
|
#is_root? ⇒ Boolean
36
37
38
|
# File 'app/models/esa/context.rb', line 36
def is_root?
self.parent_id.nil?
end
|
#is_subcontext? ⇒ Boolean
40
41
42
|
# File 'app/models/esa/context.rb', line 40
def is_subcontext?
self.parent_id.present?
end
|
#is_update_needed? ⇒ Boolean
98
99
100
101
102
103
104
105
106
107
108
|
# File 'app/models/esa/context.rb', line 98
def is_update_needed?
if self.freshness.present?
if self.last_transaction_time.present?
self.freshness <= self.last_transaction_time
else
false
end
else
true
end
end
|
#last_transaction_time ⇒ Object
66
67
68
69
70
71
72
|
# File 'app/models/esa/context.rb', line 66
def last_transaction_time
if defined? @last_transaction_time
@last_transaction_time
else
@last_transaction_time = self.transactions.maximum(:created_at)
end
end
|
#next_freshness_timestamp ⇒ Object
128
129
130
131
132
133
134
|
# File 'app/models/esa/context.rb', line 128
def next_freshness_timestamp
if self.parent.present?
[self.freshness, self.parent.try(:freshness)].compact.max
else
Time.zone.now
end
end
|
#opening_context ⇒ Object
156
157
158
159
160
161
162
163
|
# File 'app/models/esa/context.rb', line 156
def opening_context
if self.effective_start_date.present?
end_date = self.effective_start_date - 1.day
Contexts::OpenCloseContext.new(chart: self.chart, parent: self, end_date: end_date, namespace: 'opening')
else
Contexts::EmptyContext.new(chart: self.chart, parent: self, namespace: 'opening')
end
end
|
#subcontext_namespaces ⇒ Object
136
137
138
|
# File 'app/models/esa/context.rb', line 136
def subcontext_namespaces
self.subcontexts.pluck(:namespace).compact.uniq
end
|
#transactions ⇒ Object
52
53
54
|
# File 'app/models/esa/context.rb', line 52
def transactions
self.apply(self.unscoped_transactions)
end
|
#update!(options = {}) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'app/models/esa/context.rb', line 110
def update!(options = {})
self.freshness = self.next_freshness_timestamp
ESA.configuration.context_checkers.each do |checker|
if checker.respond_to? :check
checker.check(self, options)
end
end
self.update_name
self.save if self.can_be_persisted?
end
|
#update_freshness_timestamp! ⇒ Object
123
124
125
126
|
# File 'app/models/esa/context.rb', line 123
def update_freshness_timestamp!
self.freshness = self.next_freshness_timestamp
self.save if self.can_be_persisted?
end
|