class FitCommit::ConfigurationLoader

Constants

LOCAL_FILEPATH
SYSTEM_FILEPATH

Attributes

filepaths[RW]

sorted by increasing precedence

Public Class Methods

config_filepath() click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 42
def self.config_filepath
  File.join(git_top_level, "config", "fit_commit.yml")
end
default_configuration() click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 12
def self.default_configuration
  new(default_filepaths).configuration
end
default_filepaths() click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 24
def self.default_filepaths
  [
    gem_default_filepath,
    SYSTEM_FILEPATH,
    user_filepath,
    config_filepath,
    LOCAL_FILEPATH
  ]
end
gem_default_filepath() click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 34
def self.gem_default_filepath
  File.expand_path("../../../templates/config/fit_commit.default.yml", __FILE__)
end
git_top_level() click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 46
def self.git_top_level
  top_level = `git rev-parse --show-toplevel`.chomp.strip
  fail "Git repo not found! Please submit a bug report." if top_level == ""
  top_level
end
new(filepaths) click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 8
def initialize(filepaths)
  self.filepaths = filepaths
end
user_filepath() click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 38
def self.user_filepath
  File.join(ENV["HOME"], ".fit_commit.yml")
end

Public Instance Methods

configuration() click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 16
def configuration
  filepaths.each_with_object({}) do |filepath, config|
    config.merge!(read_config(filepath)) do |_key, oldval, newval|
      oldval.merge(newval)
    end
  end
end

Private Instance Methods

load_yaml(path) click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 64
def load_yaml(path)
  content = YAML.load_file(path) if File.readable?(path)
  content || {}
rescue => e
  raise e, "Error parsing config file: #{e.message}"
end
read_config(path) click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 57
def read_config(path)
  load_yaml(path).each_with_object({}) do |(key, value), config|
    translated_key = translate_config_key(key)
    config[translated_key] = value
  end
end
translate_config_key(config_key) click to toggle source
# File lib/fit_commit/configuration_loader.rb, line 71
def translate_config_key(config_key)
  return config_key unless config_key.include?("/")
  "FitCommit::" + config_key.gsub("/", "::")
end