Sha256: 6c333e75f7f4766e8c929447270b0fb07cb61b8ba39fc37c4f7354bb57f53308

Contents?: true

Size: 1.73 KB

Versions: 17

Compression:

Stored size: 1.73 KB

Contents

require 'yaml'

module Ufo
  class Setting
    extend Memoist
    autoload :Profile, "ufo/setting/profile"

    def initialize(check_ufo_project=true)
      @check_ufo_project = check_ufo_project
    end

    # data contains the settings.yml config.  The order or precedence for settings
    # is the project ufo/settings.yml and then the ~/.ufo/settings.yml.
    def data
      if @check_ufo_project && !File.exist?(project_settings_path)
        Ufo.check_ufo_project!
      end

      # project based settings files
      project = load_file(project_settings_path)

      user_file = "#{ENV['HOME']}/.ufo/settings.yml"
      user = File.exist?(user_file) ? YAML.load_file(user_file) : {}

      default_file = File.expand_path("../default/settings.yml", __FILE__)
      default = load_file(default_file)

      all_envs = default.deep_merge(user.deep_merge(project))
      all_envs = merge_base(all_envs)
      data = all_envs[Ufo.env] || all_envs["base"] || {}
      data.deep_symbolize_keys
    end
    memoize :data

  private
    def load_file(path)
      return Hash.new({}) unless File.exist?(path)

      content = RenderMePretty.result(path)
      data = YAML.load(content)
      # If key is is accidentally set to nil it screws up the merge_base later.
      # So ensure that all keys with nil value are set to {}
      data.each do |env, _setting|
        data[env] ||= {}
      end
      data
    end

    # automatically add base settings to the rest of the environments
    def merge_base(all_envs)
      base = all_envs["base"] || {}
      all_envs.each do |env, settings|
        all_envs[env] = base.merge(settings) unless env == "base"
      end
      all_envs
    end

    def project_settings_path
      "#{Ufo.root}/.ufo/settings.yml"
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ufo-4.3.1 lib/ufo/setting.rb
ufo-4.3.0 lib/ufo/setting.rb
ufo-4.2.0 lib/ufo/setting.rb
ufo-4.1.10 lib/ufo/setting.rb
ufo-4.1.9 lib/ufo/setting.rb
ufo-4.1.8 lib/ufo/setting.rb
ufo-4.1.7 lib/ufo/setting.rb
ufo-4.1.6 lib/ufo/setting.rb
ufo-4.1.5 lib/ufo/setting.rb
ufo-4.1.4 lib/ufo/setting.rb
ufo-4.1.2 lib/ufo/setting.rb
ufo-4.1.1 lib/ufo/setting.rb
ufo-4.1.0 lib/ufo/setting.rb
ufo-4.0.3 lib/ufo/setting.rb
ufo-4.0.2 lib/ufo/setting.rb
ufo-4.0.1 lib/ufo/setting.rb
ufo-4.0.0 lib/ufo/setting.rb