Module: SettingsDB::Settings::ClassMethods

Defined in:
lib/settingsdb/settings.rb

Instance Method Summary collapse

Instance Method Details

#[](namespace = :default, index) ⇒ Object

:call-seq:

Model[:key] -> value
Model[:namespace, :key] -> value

Returns the value for the given key. If a namespace is given then the key lookup is restricted to that namespace. Unqualified keys are looked up in the :default namespace.



28
29
30
31
32
# File 'lib/settingsdb/settings.rb', line 28

def [](namespace = :default, index)
  obj = get_setting(namespace, index)
  return obj.read_attribute(setting_value_field) if obj
  SettingsDB::Defaults[namespace, index]
end

#[]=(namespace = :default, index, value) ⇒ Object

:call-seq:

Model[:key] = value -> value
Model[:namespace, :key] = value -> value

Sets the value for :key to value and commits the new value to the database. If :namespace is given, the key is set in that namespace only, if :namespace is not given then :default is used.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/settingsdb/settings.rb', line 44

def []=(namespace = :default, index, value)
  obj = get_setting(namespace, index)
  if obj
    obj.instance_eval do
      write_attribute(setting_value_field, value)
    end
    obj.save
    write_cache(obj)
  else
    obj = write_cache(self.create(setting_name_field => index, setting_namespace_field => namespace, setting_value_field => value))
  end
end

#cacheObject

:call-seq:

cache -> hash

Returns a deep copy of the internal setting cache



76
77
78
79
# File 'lib/settingsdb/settings.rb', line 76

def cache
  # Do a deep copy
  Marshal.load(Marshal.dump(@@cache))
end

#cache!Object

:call-seq:

cache! -> hash

Returns a reference to the internal setting cache. USE WITH CAUTION!



87
88
89
# File 'lib/settingsdb/settings.rb', line 87

def cache!
  @@cache
end

#delete_from_cache(setting) ⇒ Object

:nodoc:



57
58
59
# File 'lib/settingsdb/settings.rb', line 57

def delete_from_cache(setting) # :nodoc:
  write_cache(setting,true)
end

#reset_cache!Object

:call-seq:

reset_cache! -> true

Resets the internal setting cache



66
67
68
69
# File 'lib/settingsdb/settings.rb', line 66

def reset_cache!
  @@cache = { :default => {} }.with_indifferent_access
  true
end