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

Returns a new instance of ConfigHash.



23
24
25
26
# File 'lib/config-hash.rb', line 23

def initialize(hash=nil)
  super()
  merge!(hash) if hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



106
107
108
109
110
111
112
113
114
# File 'lib/config-hash.rb', line 106

def method_missing(sym, *args, &block)
  if sym =~ /=$/
    self[$`] = args.first
  elsif args.empty?
    self[sym]
  else
    super
  end
end

Class Method Details

.load(path = "config.rb", var = "config") ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/config-hash.rb', line 14

def self.load(path="config.rb", var="config")
  path = File.expand_path(path)
  eval <<-"end", binding, path, 0
    #{var} ||= new
    #{IO.read(path, encoding: 'utf-8') if File.exists?(path)}
    #{var}
  end
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/config-hash.rb', line 39

def [](key)
  key = key.to_s
  if !key?(key) && key =~ SEPARATORS
    val = self
    key.split(SEPARATORS).each do |tag|
      if !val.instance_of?(self.class)
        return super(key)
      elsif val.key?(tag)
        val = val[tag]
      elsif tag == "*" && val.size == 1
        val = val[val.keys.first]
      else
        return super(key)
      end
    end
    val
  else
    super(key)
  end
end

#[]=(key, val) ⇒ Object Also known as: store



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/config-hash.rb', line 60

def []=(key, val)
  our = self.class
  key = key.to_s
  val = our.new(val) if val.instance_of?(Hash)
  if val.instance_of?(NormalHash)
    super(key, val)
  elsif key =~ SEPARATORS
    all = key.split(SEPARATORS)
    key = all.pop
    top = all.inject(self) do |top, tag|
      if top.key?(tag) && (try = top[tag]).instance_of?(our)
        top = try
      else
        top = top[tag] = our.new
      end
    end
    top[key] = val
  else
    super(key, val)
  end
end

#import(root, glob) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/config-hash.rb', line 28

def import(root, glob)
  root = File.expand_path(root)
  pref = root.size + 1
  Dir[File.join(root, glob)].each do |path|
    keys = File.dirname(path[pref..-1])
    data = ConfigHash.load(path)
    self[keys] = data
  end
  self
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/config-hash.rb', line 84

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

#merge!(other_hash) ⇒ Object Also known as: update

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/config-hash.rb', line 88

def merge!(other_hash)
  raise ArgumentError unless Hash === other_hash
  other_hash.each do |k, v|
    if block_given? && key?(k)
      self[k] = yield(k, self[k], v)
    else
      self[k] = v
    end
  end
  self
end

#to_hashObject



102
103
104
# File 'lib/config-hash.rb', line 102

def to_hash
  Hash[self]
end