class Greener::ConfigStore

Read configs from a user-specified greener.yml or fallback to defaults

Attributes

checkers[R]
files[R]
formatters[R]

Public Class Methods

new(path, default_path = nil) click to toggle source
# File lib/greener/config_store.rb, line 22
def initialize(path, default_path = nil)
  @path = path
  default_path ||= default_absolute_path
  @default_path = default_path

  @checkers = {}
  @files = []
  @formatters = []
end

Public Instance Methods

files_matching_glob(glob) click to toggle source
# File lib/greener/config_store.rb, line 51
def files_matching_glob(glob)
  Dir.glob(glob).select { |e| File.file? e }
end
load_yml_file(path) click to toggle source

Stub-able methods

# File lib/greener/config_store.rb, line 47
def load_yml_file(path)
  YAML.load_file(path)
end
resolve() click to toggle source
# File lib/greener/config_store.rb, line 32
def resolve
  if @path
    fail Error::Standard, "No config file found at specified path: #{@path}" unless File.exist? @path
    config = load_yml_file @path
  end

  config ||= {}
  defaults = load_yml_file @default_path
  @all = merge_hashes(defaults, config)

  validate
  self
end

Private Instance Methods

deep_merge(first, second) click to toggle source
# File lib/greener/config_store.rb, line 66
def deep_merge(first, second)
  merger = proc { |_key, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2, &merger) : v2 }
  first.merge(second, &merger)
end
default_absolute_path() click to toggle source
# File lib/greener/config_store.rb, line 120
def default_absolute_path
  File.expand_path("../../../config/defaults.yml", __FILE__)
end
merge_hashes(default, opt) click to toggle source

Deep merge, with a few post-merge checks

# File lib/greener/config_store.rb, line 58
def merge_hashes(default, opt)
  result = deep_merge(default, opt)
  # Change nils to empty hashes/arrays so this class isn't littered with #nil? checks
  result["AllCheckers"]["Exclude"] = [] if result["AllCheckers"]["Exclude"].nil?

  result
end
set_checkers() click to toggle source
# File lib/greener/config_store.rb, line 98
def set_checkers
  @all.each do |k, v|
    next unless %w( Style/ Lint/ ).any? { |prefix| k.start_with?(prefix) }
    checker_klass = checker_from_string(k)
    @checkers[checker_klass] = v
    @all.delete(k)
  end
end
set_files() click to toggle source
# File lib/greener/config_store.rb, line 107
def set_files
  includes = []
  excludes = []

  @all["AllCheckers"]["Include"].each { |glob| includes += files_matching_glob(glob) }
  @all["AllCheckers"].delete "Include"

  @all["AllCheckers"]["Exclude"].each { |glob| excludes += files_matching_glob(glob) }
  @all["AllCheckers"].delete "Exclude"

  @files = includes.uniq - excludes.uniq
end
set_formatters() click to toggle source
# File lib/greener/config_store.rb, line 83
def set_formatters
  formatters = @all["AllCheckers"]["Formatters"].uniq.compact
  # Ensure "Summary" formatter is in last position
  if formatters.include?("Summary")
    formatters << formatters.delete("Summary")
  else
    formatters << "Summary"
  end
  formatters.each do |f_string|
    @formatters << formatter_from_string(f_string)
  end

  @all["AllCheckers"].delete "Formatters"
end
validate() click to toggle source
# File lib/greener/config_store.rb, line 71
def validate
  set_formatters
  set_checkers
  set_files

  @all.delete("AllCheckers") if @all["AllCheckers"] && @all["AllCheckers"].empty?

  @all.each do |k, _v|
    fail Error::Standard, "Unknown option in config file: #{k}" # TODO: print warning instead of fail
  end
end