Class: Stacks::ReportCache

Inherits:
Object
  • Object
show all
Extended by:
Cache
Defined in:
lib/stacks/report_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_conditionObject

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_nameObject

Returns the value of attribute report_name.



5
6
7
# File 'lib/stacks/report_cache.rb', line 5

def report_name
  @report_name
end

#ttlObject

Returns the value of attribute ttl.



5
6
7
# File 'lib/stacks/report_cache.rb', line 5

def ttl
  @ttl
end

#valuesObject

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

.reportsObject



7
8
9
# File 'lib/stacks/report_cache.rb', line 7

def self.reports
  @reports ||= {}
end

Instance Method Details

#backendObject



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_cacheObject



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

#timestampObject



44
45
46
# File 'lib/stacks/report_cache.rb', line 44

def timestamp
  get_item(Stacks::Items::Timestamp.new)
end

#value(value_name, &block) ⇒ Object



26
27
28
# File 'lib/stacks/report_cache.rb', line 26

def value(value_name, &block)
  @values[value_name] = Stacks::Items::Proc.new(value_name, block)
end