Sha256: 0f60d544641f5660a3ffd22cf3eaecca0461251a1b0d2ea581245cc870e2c77b

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'yaml'
require 'erb'

module Hodor
  class Configuration

    def env
      Environment.instance
    end

    def target
      env.settings[:target]
    end

    def logger
      env.logger
    end

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

    def load

      @loaded = true

      yml_expand(@target_cluster, [@clusters])
    end

    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

    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

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hodor-1.0.2 lib/hodor/configuration.rb