Class: Inprovise::Config

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

Overview

Config class for Inprovise

Author

Martin Corino

License

Distributes under the same license as Ruby

Instance Method Summary collapse

Constructor Details

#initialize(other = nil) ⇒ Config

Returns a new instance of Config.



8
9
10
11
# File 'lib/inprovise/config.rb', line 8

def initialize(other=nil)
  @table = {}
  copy!(other) if other
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



106
107
108
109
110
111
112
# 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

Instance Method Details

#[](key) ⇒ Object



36
37
38
# File 'lib/inprovise/config.rb', line 36

def [](key)
  get(key)
end

#[]=(key, val) ⇒ Object



40
41
42
# File 'lib/inprovise/config.rb', line 40

def []=(key, val)
  set(key, val)
end

#copy!(other) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# 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

#dupObject



95
96
97
# File 'lib/inprovise/config.rb', line 95

def dup
  self.class.new(@table)
end

#each(&block) ⇒ Object



91
92
93
# File 'lib/inprovise/config.rb', line 91

def each(&block)
  @table.each { |k,v| block.call(k,v) }
end

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/inprovise/config.rb', line 52

def empty?
  @table.empty?
end

#get(key) ⇒ Object



18
19
20
21
22
23
24
25
# 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) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# 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

#set(key, val) ⇒ Object



27
28
29
30
31
32
33
34
# 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_hObject



99
100
101
102
103
104
# 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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# 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