class Inprovise::Config
- Author
-
Martin Corino
- License
-
Distributes under the same license as Ruby
Public Class Methods
new(other=nil)
click to toggle source
# File lib/inprovise/config.rb, line 8 def initialize(other=nil) @table = {} copy!(other) if other end
Public Instance Methods
[](key)
click to toggle source
# File lib/inprovise/config.rb, line 36 def [](key) get(key) end
[]=(key, val)
click to toggle source
# File lib/inprovise/config.rb, line 40 def []=(key, val) set(key, val) end
copy!(other)
click to toggle source
# File lib/inprovise/config.rb, line 68 def copy!(other) other.to_h.each do |k,v| case self[k] when self.class self[k].copy!(v) else self[k] = v.is_a?(Hash) ? v : (v.dup rescue v) end end self end
dup()
click to toggle source
# File lib/inprovise/config.rb, line 95 def dup self.class.new(@table) end
each(&block)
click to toggle source
# File lib/inprovise/config.rb, line 91 def each(&block) @table.each { |k,v| block.call(k,v) } end
empty?()
click to toggle source
# File lib/inprovise/config.rb, line 52 def empty? @table.empty? end
get(key)
click to toggle source
# File lib/inprovise/config.rb, line 18 def get(key) if Symbol === key @table[key] else vk = (path = key.to_s.split('.')).pop path.reduce(@table) { |t,k| t[k.to_sym] ||= self.class.new }[vk.to_sym] end end
has_key?(key)
click to toggle source
# File lib/inprovise/config.rb, line 44 def has_key?(key) if Symbol === key @table.has_key?(key) else !(key.to_s.split('.').reduce(@table) { |t,k| t && t.has_key?(k.to_sym) && self.class === t[k.to_sym] ? t[k.to_sym] : nil }).nil? end end
merge!(other)
click to toggle source
# File lib/inprovise/config.rb, line 56 def merge!(other) other.to_h.each do |k,v| case self[k] when self.class self[k].merge!(v) else self[k] = v end end self end
method_missing(method, *args)
click to toggle source
# File lib/inprovise/config.rb, line 106 def method_missing(method, *args) if /(.*)=$/ =~ method.to_s self[$1.to_sym] = (args.size > 1 ? args : args.shift) else self[method] end end
set(key, val)
click to toggle source
# File lib/inprovise/config.rb, line 27 def set(key, val) if Symbol === key @table[key] = _v_(val) else vk = (path = key.to_s.split('.')).pop path.reduce(@table) { |t,k| t[k.to_sym] ||= self.class.new }[vk.to_sym] = _v_(val) end end
to_h()
click to toggle source
# File lib/inprovise/config.rb, line 99 def to_h @table.reduce({}) do |hsh, (k,v)| hsh[k] = self.class === v ? v.to_h : v hsh end end
update!(other)
click to toggle source
# File lib/inprovise/config.rb, line 80 def update!(other) other.to_h.each do |k,v| if self.has_key?(k) self[k].update!(v) if self.class === self[k] else self[k] = v.is_a?(Hash) ? v : (v.dup rescue v) end end self end
Private Instance Methods
_v_(val)
click to toggle source
# File lib/inprovise/config.rb, line 13 def _v_(val) Hash === val ? self.class.new.merge!(val) : val end