Class: Nomo::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/nomo/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, name, options = {}) ⇒ Page

Returns a new instance of Page.



5
6
7
8
9
# File 'lib/nomo/page.rb', line 5

def initialize(record, name, options = {})
  @record  = record
  @name    = name
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/nomo/page.rb', line 3

def name
  @name
end

Instance Method Details

#cache_keyObject



29
30
31
# File 'lib/nomo/page.rb', line 29

def cache_key
  "#{key}/#{last_modified.to_f}"
end

#keyObject



33
34
35
36
37
38
# File 'lib/nomo/page.rb', line 33

def key
  model = @record.class.model_name.cache_key
  id    = @record.id

  "#{model}/#{id}/#{name}"
end

#last_modifiedObject



11
12
13
14
15
16
17
18
19
# File 'lib/nomo/page.rb', line 11

def last_modified
  if @last_modified.nil? && redis_last_modified = redis.get(key)
    @last_modified = Time.at(redis_last_modified.to_f).utc
  elsif @last_modified.nil?
    modify(publish: false)
  end

  @last_modified
end

#modify(options = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/nomo/page.rb', line 21

def modify(options = {})
  @last_modified = Time.now.utc

  Nomo.logger.debug("(nomo) #{key} modified by #{(options[:modified_by] || @record).class.model_name.cache_key}/#{@record.id}")
  redis.set(key, last_modified.to_f)
  Nomo.publish("updates", key) unless options[:publish].eql?(false) || !realtime?
end

#realtime?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/nomo/page.rb', line 40

def realtime?
  @options.has_key?(:realtime) ? @options[:realtime] : true
end