Class: ConfigHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/config-hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#+@, #-@

Constructor Details

#initialize(hash = nil) ⇒ ConfigHash



30
31
32
33
# File 'lib/config-hash.rb', line 30

def initialize(hash=nil)
  super()
  update(hash) if hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &code) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/config-hash.rb', line 102

def method_missing(name, *args, &code)
  case
    when name =~ /=$/ then self[$`] = args.first
    when args.empty?  then self[name]
    else super
  end
end

Class Method Details

.[](hash = nil) ⇒ Object



15
16
17
# File 'lib/config-hash.rb', line 15

def self.[](hash=nil)
  new(hash)
end

.load(path = "config.rb", list = nil, name: "config") ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/config-hash.rb', line 19

def self.load(path="config.rb", list=nil, name: "config")
  path = File.expand_path(path)
  data = eval "  \#{name} ||= new\n  \#{IO.read(path, encoding: 'utf-8') if File.readable?(path)}\n  \#{name}\nend\ndata.load(*list) if list && !list.empty?\ndata\n", binding, path, 0

Instance Method Details

#[](key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/config-hash.rb', line 54

def [](key)
  our = self.class
  key = key.to_s

  if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
    val = ary.inject(self) do |obj, sub|
      if not our === obj  then return super(key)
      elsif obj.key?(sub) then obj[sub]
      elsif sub == "*"    then obj[obj.keys.first]
      else                     return super(key)
      end
    end
  else
    super(key)
  end
end

#[]=(key, val) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/config-hash.rb', line 71

def []=(key, val)
  our = self.class
  key = key.to_s
  val = our.new(val) if val.instance_of?(Hash)

  if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
    key = ary.pop
    obj = ary.inject(self) do |obj, sub|
      obj.key?(sub) && our === (try = obj[sub]) ? try : (obj[sub] = our.new)
    end
    obj[key] = val
  else
    super(key, val)
  end
end

#key?(key) ⇒ Boolean



50
51
52
# File 'lib/config-hash.rb', line 50

def key?(key)
  super(key.to_s)
end

#load(*list) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/config-hash.rb', line 35

def load(*list)
  [list].each do |root, glob|
    root = File.expand_path(root)
    pref = root.size + 1
    full = File.join([root, glob].compact)
    list = Dir[full].sort {|a,b| [a.count('/'), a] <=> [b.count('/'), b]}
    list.each do |path|
      info = File.dirname(path[pref...] || '')
      data = ConfigHash.load(path)
      info == '.' ? update(data) : (self[info] = data)
    end
  end
  self
end

#to_hashObject



98
99
100
# File 'lib/config-hash.rb', line 98

def to_hash
  Hash[self]
end

#update(hash, nuke = false) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
90
91
92
# File 'lib/config-hash.rb', line 87

def update(hash, nuke=false)
  raise ArgumentError unless Hash === hash
  clear if nuke
  hash.each {|key, val| self[key] = val}
  self
end

#update!(hash) ⇒ Object



94
95
96
# File 'lib/config-hash.rb', line 94

def update!(hash)
  update(hash, true)
end