Class: Settings::ValueStore

Inherits:
Object
  • Object
show all
Defined in:
lib/iron/settings/value_store.rb

Overview

Base class for our value stores. Derived classes manage loading and saving values in the value hash.

Direct Known Subclasses

DBStore, StaticStore

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ ValueStore

Returns a new instance of ValueStore.



7
8
9
10
11
12
13
# File 'lib/iron/settings/value_store.rb', line 7

def initialize(root, options = {})
  @root = root
  @options = options
  @loaded_on = nil
  @reload = options.delete(:reload) || false
  @values = {}
end

Instance Method Details

#get_value(key) ⇒ Object



74
75
76
# File 'lib/iron/settings/value_store.rb', line 74

def get_value(key)
  @values[key]
end

#has_value?(key) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/iron/settings/value_store.rb', line 70

def has_value?(key)
  @values.has_key?(key)
end

#loadObject



52
53
54
55
# File 'lib/iron/settings/value_store.rb', line 52

def load
  @loaded_on = Time.now
  @values = {}
end

#need_reload?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/iron/settings/value_store.rb', line 15

def need_reload?
  # Always reload at first chance, ie LOAD, duh
  return true if @loaded_on.nil?
  
  # Do the right thing
  case @reload
  when true then
    # Always reload each time #settings creates a new cursor
    true
    
  when false then
    # Never reload
    false
    
  when Proc then
    # Custom reload handler, reload on returning true
    @reload.call === true
    
  when Fixnum then
    # Reload after N seconds
    Time.now > @loaded_on + @reload.to_i
    
  when String then
    # Reload if file is modified
    mod_time = File.mtime(@reload) rescue nil
    mod_time.nil? || @loaded_on < mod_time
    
  else
    # Non-standard reload setting, must be handled in kids
    nil
  end
end

#read_only?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/iron/settings/value_store.rb', line 82

def read_only?
  false
end

#reload_if_neededObject



48
49
50
# File 'lib/iron/settings/value_store.rb', line 48

def reload_if_needed
  load if need_reload?
end

#saveObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/iron/settings/value_store.rb', line 57

def save
  # No saving for me, thanks
  return if read_only?
  
  # Update our timestamp on our cache reload file, if any
  if @reload.is_a?(String)
    FileUtils.touch(@reload)
  end
  
  # Remember when we were loaded for future use
  @loaded_on = Time.now
end

#set_value(key, value) ⇒ Object



78
79
80
# File 'lib/iron/settings/value_store.rb', line 78

def set_value(key, value)
  @values[key] = value
end