Class: Settingslogic

Inherits:
Hash
  • Object
show all
Defined in:
lib/settingslogic.rb

Overview

A simple settings solution using a YAML file. See README for more information.

Defined Under Namespace

Classes: MissingSetting

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_or_file = self.class.source, section = nil) ⇒ Settingslogic

Initializes a new settings object. You can initialize an object in any of the following ways:

Settings.new(:application) # will look for config/application.yml
Settings.new("application.yaml") # will look for application.yaml
Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
Settings.new(:config1 => 1, :config2 => 2)

Basically if you pass a symbol it will look for that file in the configs directory of your rails app, if you are using this in rails. If you pass a string it should be an absolute path to your settings file. Then you can pass a hash, and it just allows you to access the hash via methods.



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/settingslogic.rb', line 87

def initialize(hash_or_file = self.class.source, section = nil)
  case hash_or_file
  when Hash
    self.replace hash_or_file
  else
    hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash
    default_hash = hash[self.class.default_namespace] || {}      
    hash = hash[self.class.namespace] if self.class.namespace
    self.replace default_hash.deep_merge(hash)
  end
  @section = section || hash_or_file  # so end of error says "in application.yml"
  create_accessors!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object

Called for dynamically-defined keys, and also the first key deferenced at the top-level, if load! is not used. Otherwise, create_accessors! (called by new) will have created actual methods for each key.



108
109
110
111
112
113
114
115
# File 'lib/settingslogic.rb', line 108

def method_missing(key, *args, &block)
  begin
    value = fetch(key.to_s)
  rescue IndexError
    raise MissingSetting, "Missing setting '#{key}' in #{@section}"
  end
  value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
end

Class Method Details

.[](key) ⇒ Object



47
48
49
50
# File 'lib/settingslogic.rb', line 47

def [](key)
  # Setting.key.value or Setting[:key][:value] or Setting['key']['value']
  fetch(key.to_s,nil)
end

.[]=(key, val) ⇒ Object



52
53
54
55
# File 'lib/settingslogic.rb', line 52

def []=(key,val)
  # Setting[:key] = 'value' for dynamic settings
  store(key.to_s,val)
end

.default_namespace(value = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/settingslogic.rb', line 26

def default_namespace(value = nil)
  if value.nil?
    @default_namespace || 'defaults'
  else
    @default_namespace = value
  end
end

.key_by_path(key_path, separator = ".") ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/settingslogic.rb', line 34

def key_by_path(key_path, separator = ".")
  # Settings.get_nested_key('some.nested.setting')
  tmp = instance
  key_path.split(separator).each do |k|
    if tmp[k].respond_to?("[]") && !tmp[k].nil?
      tmp = tmp[k]
    else
      return nil
    end
  end
  tmp
end

.load!Object



57
58
59
60
# File 'lib/settingslogic.rb', line 57

def load!
  instance
  true
end

.namespace(value = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/settingslogic.rb', line 18

def namespace(value = nil)
  if value.nil?
    @namespace
  else
    @namespace = value
  end
end

.reload!Object



62
63
64
65
# File 'lib/settingslogic.rb', line 62

def reload!
  @instance = nil
  load!
end

.source(value = nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/settingslogic.rb', line 10

def source(value = nil)
  if value.nil?
    @source
  else
    @source = value
  end
end

Instance Method Details

#[](key) ⇒ Object



101
102
103
104
# File 'lib/settingslogic.rb', line 101

def [](key)
  # @settings.key.value or @settings[:key][:value] or @settings['key']['value']
  super(key.to_s)
end