Sha256: eec44fab3b6a409f7f5fb6beec6c7e3de676b2267032ac28d6a8c084440c5f3e

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'yaml'
require 'active_support/core_ext/hash'

module Balancer
  class Setting
    extend Memoist

    def data
      settings_file = Balancer.profile || 'default'
      settings_file += ".yml"

      profile = yaml_file("#{Balancer.root}/.balancer/profiles/#{settings_file}")
      user = yaml_file("#{home}/.balancer/#{settings_file}")
      default_file = File.expand_path("../default/settings.yml", __FILE__)
      default = yaml_file(default_file)

      data = merge(default, user, profile)

      if ENV['DEBUG_SETTINGS']
        puts "settings data:"
        pp data
      end
      data
    end
    memoize :data

    def merge(*hashes)
      hashes.inject({}) do |result, hash|
        # note: important to compact for keys with nil value
        result.deep_merge(hash.compact)
      end
    end

    # Any empty file will result in "false".  Lets ensure that an empty file
    # loads an empty hash instead.
    def yaml_file(path)
      # puts "yaml_file #{path}"
      return {} unless File.exist?(path)
      YAML.load_file(path) || {}
    end

    def home
      # hack but fast
      ENV['TEST'] ? "spec/fixtures/home" : ENV['HOME']
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
balancer-0.2.0 lib/balancer/setting.rb