Class: Hodor::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hodor/configuration.rb

Instance Method Summary collapse

Constructor Details

#initialize(yml_file) ⇒ Configuration

Returns a new instance of Configuration.



19
20
21
22
# File 'lib/hodor/configuration.rb', line 19

def initialize(yml_file)
  @yml_file = yml_file
  @kvp = {}
end

Instance Method Details

#envObject



7
8
9
# File 'lib/hodor/configuration.rb', line 7

def env
  Environment.instance
end

#loadObject



24
25
26
27
28
29
# File 'lib/hodor/configuration.rb', line 24

def load

  @loaded = true

  yml_expand(@target_cluster, [@clusters])
end

#loggerObject



15
16
17
# File 'lib/hodor/configuration.rb', line 15

def logger
  env.logger
end

#render_flattenedObject



73
74
75
76
# File 'lib/hodor/configuration.rb', line 73

def render_flattened
  flat_vals = yml_flatten('', egress_to)
  flat_vals.join("\n")
end

#targetObject



11
12
13
# File 'lib/hodor/configuration.rb', line 11

def target
  env.settings[:target]
end

#yml_expand(val, parents) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hodor/configuration.rb', line 31

def yml_expand(val, parents)
  if val.is_a? String
    val.gsub(/\$\{.+?\}/) { |match|
      cv = match.split(/\${|}/)
      expr = cv[1]
      ups = expr.split('^')
      parent_index = parents.length - ups.length
      parent = parents[parent_index]
      parent_key = ups[-1]
      parent_key = parent_key[1..-1] if parent_key.start_with?(':')
      if parent.has_key?(parent_key)
        parent[parent_key]
      elsif parent.has_key?(parent_key.to_sym)
        parent[parent_key.to_sym]
      else
        parent_key
      end
    }
  elsif val.is_a? Hash
    more_parents = parents << val
    val.each_pair { |k, v|
      exp_val = yml_expand(v, more_parents)
      val[k] = exp_val
    }
  else
    val
  end
end

#yml_flatten(parent_key, val) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hodor/configuration.rb', line 60

def yml_flatten(parent_key, val)
  flat_vals = []
  if val.is_a? Hash
    val.each_pair { |k, v|
      flat_vals += yml_flatten("#{parent_key}.#{k}", v)
    }
  else
    parent_key = parent_key[1..-1] if parent_key.start_with?('.')
    flat_vals = ["#{parent_key} = #{val}"]
  end
  flat_vals
end