Sha256: db5fa4dde33b79ba08234011cc3bae436421ee82ce3c0cc0f66e5a307af4ec20

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

module BigBrother
  class Configuration
    GLOBAL_CONFIG_KEY = '_big_brother'

    def self.from_file(config_file)
      config = YAML.load_file(config_file)
      return {} unless _valid?(config)

      configured_clusters = config.fetch("clusters")
      defaults = config.delete(GLOBAL_CONFIG_KEY)

      configured_clusters.inject({}) do |clusters, cluster|
        cluster_details = _apply_defaults(defaults, cluster)
        cluster_name    = cluster.fetch("cluster_name")
        clusters.merge(cluster_name => BigBrother::ClusterFactory.create_cluster(cluster_name, _deeply_symbolize_keys(cluster_details)))
      end
    end

    def self._deeply_symbolize_keys(value)
      if value.is_a?(Hash)
        value.inject({}) do |symbolized_hash, (hash_key, hash_value)|
          symbolized_hash[hash_key.to_sym] = _deeply_symbolize_keys(hash_value)
          symbolized_hash
        end
      elsif value.is_a?(Array)
        value.map { |item| _deeply_symbolize_keys(item) }
      else
        value
      end
    end

    def self._apply_defaults(defaults_hash, settings_hash)
      return settings_hash unless defaults_hash
      defaults_hash.merge(settings_hash) do |key, oldval, newval|
        oldval.is_a?(Hash) && newval.is_a?(Hash) ? _apply_defaults(oldval, newval) : newval
      end
    end

    def self._valid?(config)
      schema_path = File.join(File.dirname(__FILE__), "../resources", "config_schema.yml")
      schema = YAML.load_file(schema_path)
      validator = Kwalify::Validator.new(schema)
      errors = validator.validate(config)
      valid = !(errors && !errors.empty?)

      unless valid
        errors.each { |err| BigBrother.logger.info("- [#{err.path}] #{err.message}") }
      end

      valid
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
big_brother-0.8.8.1 lib/big_brother/configuration.rb
big_brother-0.8.8 lib/big_brother/configuration.rb
big_brother-0.8.7 lib/big_brother/configuration.rb