Class: SettingsDB::Defaults

Inherits:
Object
  • Object
show all
Defined in:
lib/settingsdb/defaults.rb

Class Method Summary collapse

Class Method Details

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

:call-seq:

Defaults[:key] -> value (from :default namespace)
Defaults[:p1, :key] -> value (from :p1 namespace)

Returns the value for the index. If namespace is given returns the value for the index in that namespace. When no namespace is given, the :default namespace is used.



12
13
14
# File 'lib/settingsdb/defaults.rb', line 12

def self.[](namespace = :default, index)
  @@defaults[namespace][index] if @@defaults[namespace]
end

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

:call-seq:

Defaults[:key] = value -> 'value' (in :default namespace)
Defaults[:p1, :key] = value -> 'value' (in :p1 namespace)

Sets the value for the index. If a namespace is given then the index is set in that namespace else it is set in the default namespace.



24
25
26
27
28
29
30
31
# File 'lib/settingsdb/defaults.rb', line 24

def self.[]=(namespace = :default, index, value)
  if @@defaults[namespace] && @@defaults[namespace].has_key?(index)
    @@defaults[namespace][index] = value
  else
    @@defaults[namespace] = { index => value }
    value
  end
end

.configObject

:call-seq:

Defaults.config -> hash
Defaults.config {|config| ...}

If given a block yields self. If no block is given the hash of default values is returned.

Example

Defaults.config do |conf|
  conf[:key1] = value1
  conf[:ns1, :key1] = value2
end

If no block is given the default hash is returned and can be manipulated directly:

default_hash = Defaults.config
default_hash[:default][:key1] = value1
default_hash[:ns1] = { :key1 => value2 }


52
53
54
55
56
57
# File 'lib/settingsdb/defaults.rb', line 52

def self.config # :yields: config
  if block_given?
    yield self
  end
  self
end

.defaultsObject

:call-seq:

defaults -> hash

Returns a deep copy of the defaults hash, modifying this will have no impact on Defaults[] results.



85
86
87
88
# File 'lib/settingsdb/defaults.rb', line 85

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

.defaults!Object

:call-seq:

defaults! -> hash

Returns the actual reference to the internal defaults hash. USE THIS WITH CARE!



75
76
77
# File 'lib/settingsdb/defaults.rb', line 75

def self.defaults!
  @@defaults
end

.reset!Object

:call-seq:

reset! -> Defaults

Deletes all the default settings.



64
65
66
67
# File 'lib/settingsdb/defaults.rb', line 64

def self.reset!
  @@defaults = { :default => {} }.with_indifferent_access
  self
end