Class: Iqvoc::InstanceConfiguration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/iqvoc/instance_configuration.rb

Overview

provides the interface to configuration settings

Defined Under Namespace

Classes: UnregisteredSetting

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstanceConfiguration

Returns a new instance of InstanceConfiguration.



33
34
35
36
37
38
# File 'lib/iqvoc/instance_configuration.rb', line 33

def initialize
  @defaults = {} # default settings
  @records = {} # customized (non-default) settings
  @settings = {} # current settings, using defaults as fallback
  # NB: cannot cache immediately because defaults need to be registered first
end

Instance Attribute Details

#defaultsObject (readonly)

XXX: dangerous (mutable object)



25
26
27
# File 'lib/iqvoc/instance_configuration.rb', line 25

def defaults
  @defaults
end

Class Method Details

.validate_value(value) ⇒ Object

checks whether value type is supported



112
113
114
115
116
117
118
119
# File 'lib/iqvoc/instance_configuration.rb', line 112

def self.validate_value(value) # TODO: compare type to default's? (cf. controller)
  if value == nil
    raise TypeError, "nil values not supported"
  end
  unless [TrueClass, FalseClass, String, Fixnum, Float, Array].include?(value.class)
    raise TypeError, "complex values not supported"
  end
end

Instance Method Details

#[](key) ⇒ Object

retrieve individual setting, using default value as fallback



71
72
73
74
# File 'lib/iqvoc/instance_configuration.rb', line 71

def [](key)
  initialize_cache # relying on ActiveRecord query cache -- XXX: inefficient (caching doesn't include processing/indexing)
  return @settings[key]
end

#[]=(key, value) ⇒ Object

store individual customized setting



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/iqvoc/instance_configuration.rb', line 77

def []=(key, value)
  raise UnregisteredSetting unless @defaults.include?(key)
  self.class.validate_value(value)

  json = JSON.dump(value)
  if setting = ConfigurationSetting.find_by_key(key)
    setting.update_attributes(:value => json)
  else
    ConfigurationSetting.create(:key => key, :value => json)
  end

  # update cache
  @records[key] = value
  @settings[key] = value

  return value
end

#deregister_setting(key) ⇒ Object

remove a default setting returns nil if setting does not exist NB: does not delete configuration settings from the database



61
62
63
64
65
66
67
68
# File 'lib/iqvoc/instance_configuration.rb', line 61

def deregister_setting(key)
  res = @defaults.delete(key)

  # update cache
  @settings.delete(key)

  return res
end

#initialize_cache(force = false) ⇒ Object

populate settings caches (subsequent updates will happen automatically via the respective setters)



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/iqvoc/instance_configuration.rb', line 97

def initialize_cache(force=false)
  # cache customized settings
  db_settings = ConfigurationSetting.all rescue [] # database table might not exist yet (pre-migration)
  db_settings.each do |setting|
    @records[setting.key] = JSON.load(setting.value)
  end

  # cache current settings
  @defaults.each_with_object({}) do |(key, default_value), hsh|
    value = @records[key]
    @settings[key] = value.nil? ? default_value : value
  end
end

#register_setting(key, default_value) ⇒ Object

create or update a default setting



49
50
51
52
53
54
55
56
# File 'lib/iqvoc/instance_configuration.rb', line 49

def register_setting(key, default_value)
  self.class.validate_value(default_value)

  @defaults[key] = default_value

  # update cache
  @settings[key] = @records[key] || default_value
end

#register_settings(settings = {}) ⇒ Object

convenience wrapper for ‘register_setting` batch operations accepts a hash of key / default value pairs



42
43
44
45
46
# File 'lib/iqvoc/instance_configuration.rb', line 42

def register_settings(settings = {})
  settings.each do |key, default_value|
    register_setting(key, default_value)
  end
end