Sha256: 19a5680e2a271800b4e3640978f620343d2f3664f2fbe1aac55d61690b230f9f

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

module BigBrother
  class Configuration
    GLOBAL_CONFIG_KEY = '_big_brother'

    def self.from_file(config_file)
      config = YAML.load_file(config_file)
      defaults = config.delete(GLOBAL_CONFIG_KEY)

      config.inject({}) do |clusters, (cluster_name, cluster_values)|
        cluster_details = _apply_defaults(defaults, cluster_values)
        clusters.merge(cluster_name => Cluster.new(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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
big_brother-0.6.8 lib/big_brother/configuration.rb
big_brother-0.6.7 lib/big_brother/configuration.rb
big_brother-0.6.6 lib/big_brother/configuration.rb
big_brother-0.6.5 lib/big_brother/configuration.rb
big_brother-0.6.4 lib/big_brother/configuration.rb
big_brother-0.6.3 lib/big_brother/configuration.rb
big_brother-0.6.2 lib/big_brother/configuration.rb
big_brother-0.6.1 lib/big_brother/configuration.rb
big_brother-0.6.0 lib/big_brother/configuration.rb
big_brother-0.5.0 lib/big_brother/configuration.rb