Class: Stacks::ReportCache
- Inherits:
-
Object
- Object
- Stacks::ReportCache
- Extended by:
- Cache
- Defined in:
- lib/stacks/report_cache.rb
Instance Attribute Summary collapse
-
#cache_condition ⇒ Object
Returns the value of attribute cache_condition.
-
#report_name ⇒ Object
Returns the value of attribute report_name.
-
#ttl ⇒ Object
Returns the value of attribute ttl.
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #backend ⇒ Object
- #fill_cache ⇒ Object
- #get_item(item) ⇒ Object
- #get_value(value_name) ⇒ Object
-
#initialize(report_name, ttl) ⇒ ReportCache
constructor
A new instance of ReportCache.
- #register_instance_variables(instance) ⇒ Object
- #set_cache_condition(&block) ⇒ Object
- #timestamp ⇒ Object
- #value(value_name, &block) ⇒ Object
Constructor Details
#initialize(report_name, ttl) ⇒ ReportCache
Returns a new instance of ReportCache.
16 17 18 19 20 |
# File 'lib/stacks/report_cache.rb', line 16 def initialize(report_name, ttl) @report_name = report_name @values = {} @ttl = ttl end |
Instance Attribute Details
#cache_condition ⇒ Object
Returns the value of attribute cache_condition.
5 6 7 |
# File 'lib/stacks/report_cache.rb', line 5 def cache_condition @cache_condition end |
#report_name ⇒ Object
Returns the value of attribute report_name.
5 6 7 |
# File 'lib/stacks/report_cache.rb', line 5 def report_name @report_name end |
#ttl ⇒ Object
Returns the value of attribute ttl.
5 6 7 |
# File 'lib/stacks/report_cache.rb', line 5 def ttl @ttl end |
#values ⇒ Object
Returns the value of attribute values.
5 6 7 |
# File 'lib/stacks/report_cache.rb', line 5 def values @values end |
Class Method Details
.get_report(report_name) ⇒ Object
11 12 13 14 |
# File 'lib/stacks/report_cache.rb', line 11 def self.get_report(report_name) raise "Invalid report name" unless @reports.keys.include?(report_name) reports[report_name] end |
.report(report_name, ttl, &block) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/stacks/report_cache.rb', line 57 def self.report(report_name, ttl, &block) report = new(report_name, ttl) report.instance_eval(&block) reports[report_name] = report report end |
.reports ⇒ Object
7 8 9 |
# File 'lib/stacks/report_cache.rb', line 7 def self.reports @reports ||= {} end |
Instance Method Details
#backend ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/stacks/report_cache.rb', line 70 def backend return @backend if @backend backend = Stacks::Backends::NamespacedBackend.new backend.namespace = @report_name @backend ||= backend end |
#fill_cache ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/stacks/report_cache.rb', line 48 def fill_cache if cache_condition return unless cache_condition.call end @values.each { |value_name, item| backend.fill(item, @ttl) } backend.fill(Stacks::Items::Timestamp.new, @ttl) end |
#get_item(item) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/stacks/report_cache.rb', line 30 def get_item(item) if cache_condition Stacks.deactivate = true unless cache_condition.call end value = self.class.get_value(item, backend, @ttl) Stacks.deactivate = false value end |
#get_value(value_name) ⇒ Object
40 41 42 |
# File 'lib/stacks/report_cache.rb', line 40 def get_value(value_name) get_item(@values[value_name]) end |
#register_instance_variables(instance) ⇒ Object
64 65 66 67 68 |
# File 'lib/stacks/report_cache.rb', line 64 def register_instance_variables(instance) values.each do |value_name, item| instance.instance_variable_set("@#{value_name}".to_sym, get_value(value_name)) end end |
#set_cache_condition(&block) ⇒ Object
22 23 24 |
# File 'lib/stacks/report_cache.rb', line 22 def set_cache_condition(&block) @cache_condition = block end |