class Bizside::ItamaeConfSub

itamae_conf をテストするため、 singleton 以前のクラスを定義

TODO: HanaitaConf と類似していますが HanaitaConf は obsolete 予定なので 敢えて refactoring はやっていません。

Public Class Methods

conf_files() click to toggle source

base..derived の順

# File lib/bizside/itamae_conf.rb, line 12
def conf_files
  if ENV['ITAMAE_CONFS']
    ENV['ITAMAE_CONFS'].split
  else
    result = ['/etc/bizside/hanaita.yml']
    add_yml(result, 'itamae.yml')
    add_yml(result, 'database.yml')
    result
  end
end
new() click to toggle source
# File lib/bizside/itamae_conf.rb, line 40
def initialize
  for conf_file in self.class.conf_files do
    if File.exist?(conf_file)
      @_conf ||= {}
      hash = YAML.load(ERB.new(File.read(conf_file)).result)

      case conf_file
      # itamae.yml スペシャルロジック。ROLE必須。
      when /itamae.yml$/
        role = ENV['ROLE']
        next if role.nil? || role.empty?
        hash = hash[role]

      # database.yml スペシャルロジック。例:
      #
      # database.yml::  RAILS_ENV.adapter
      # ↓::             ↓
      # itamae_conf::   db.adapter
      when /database.yml$/
        partial = hash[Bizside.env]
        hash = {'db' => partial.dup} if partial
      end
      deep_merge!(@_conf, hash) if hash
    else
      $stderr.printf("WARN: %s does NOT exist.\n", conf_file)
    end
  end
end

Private Class Methods

add_yml(paths, basename) click to toggle source

shared/*.yml を優先して指定。存在しない時にのみ config/*.yml を指定。

# File lib/bizside/itamae_conf.rb, line 26
def add_yml(paths, basename)
  candidates = []
  candidates << File.join(ENV['DEPLOY_TO'], 'shared', basename).to_s unless ENV['DEPLOY_TO'].to_s.empty?
  candidates << File.join('config', basename).to_s

  candidates.each do |path|
    if File.exist?(path)
      paths << path
      break
    end
  end
end

Public Instance Methods

conf() click to toggle source
# File lib/bizside/itamae_conf.rb, line 69
def conf
  @_conf
end

Private Instance Methods

deep_merge(h1, h2, &block) click to toggle source

©Rails

# File lib/bizside/itamae_conf.rb, line 76
def deep_merge(h1, h2, &block)
  deep_merge!(h1.dup, h2, &block)
end
deep_merge!(h1, h2, &block) click to toggle source

©Rails

# File lib/bizside/itamae_conf.rb, line 81
def deep_merge!(h1, h2, &block)
  h2.each_pair do |current_key, other_value|
    this_value = h1[current_key]
    h1[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
                        deep_merge(this_value, other_value, &block)
                      else
                        if block_given? && key?(current_key)
                          block.call(current_key, this_value, other_value)
                        else
                          other_value
                        end
                      end
  end
  h1
end