Class: Rig::Oconfig

Inherits:
Object
  • Object
show all
Defined in:
lib/rig/config.rb

Direct Known Subclasses

Model::Account

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Oconfig

Returns a new instance of Oconfig.



78
79
80
81
# File 'lib/rig/config.rb', line 78

def initialize(data={})
  @data = {}
  update!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/rig/config.rb', line 106

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Class Method Details

.load_yaml_file(path) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rig/config.rb', line 69

def load_yaml_file(path)
  file = File.expand_path(path)
  raise "Configuration not found: #{path} (#{file})" unless File.exists?(file)
  yaml = YAML.load_file(file)
  yaml = yaml[yaml.keys.first] if yaml.keys.count == 1
  self.new(yaml)
end

Instance Method Details

#[](key) ⇒ Object



89
90
91
# File 'lib/rig/config.rb', line 89

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rig/config.rb', line 93

def []=(key, value)
  if value.kind_of?(Hash)
    @data[key.to_sym] = Oconfig.new(value)
  elsif value.kind_of?(Array)
    @data[key.to_sym] = []
    value.each do |v|
      @data[key.to_sym] << Oconfig.new(v)
    end
  else
    @data[key.to_sym] = value
  end
end

#to_hashObject



114
115
116
117
118
119
120
# File 'lib/rig/config.rb', line 114

def to_hash
  out = {}
  @data.each do |k, v|
    out[k] = v.kind_of?(Oconfig) ? v.to_hash : v
  end
  out
end

#to_yamlObject



123
124
125
# File 'lib/rig/config.rb', line 123

def to_yaml
  to_hash.to_yaml
end

#update!(data) ⇒ Object



83
84
85
86
87
# File 'lib/rig/config.rb', line 83

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end