class LazyNames::ConfigLoader

Constants

BasicConfig
HOME_PATH

Public Class Methods

call(namespace:, path: nil) click to toggle source
# File lib/lazy_names/config_loader.rb, line 13
def call(namespace:, path: nil)
  return read_from_path(namespace, path) if path

  config = read_from_project(namespace)
  config ||= read_from_home_dir(namespace)

  config
end

Private Class Methods

config_in_project_path?() click to toggle source
# File lib/lazy_names/config_loader.rb, line 56
def config_in_project_path?
  File.exist?(project_path)
end
find_definitions(path, namespace) click to toggle source
# File lib/lazy_names/config_loader.rb, line 49
def find_definitions(path, namespace)
  read_config(path)[namespace]['definitions'].to_h

rescue NoMethodError
  raise NamespaceNotFound
end
home_path() click to toggle source
# File lib/lazy_names/config_loader.rb, line 68
def home_path
  File.expand_path(HOME_PATH)
end
project_path() click to toggle source
# File lib/lazy_names/config_loader.rb, line 64
def project_path
  File.expand_path(Pathname.new(Dir.pwd).join('.lazy_names.yml'))
end
read_config(path) click to toggle source
# File lib/lazy_names/config_loader.rb, line 60
def read_config(path)
  YAML.safe_load(File.read(path))
end
read_from_home_dir(namespace) click to toggle source
# File lib/lazy_names/config_loader.rb, line 32
def read_from_home_dir(namespace)
  definitions = find_definitions(home_path, namespace)

  BasicConfig.new(home_path, definitions)
rescue Errno::ENOENT
  raise NoConfig, 'No config found in your home directory. ' \
    'Create ~/.lazy_names.yml'
end
read_from_path(namespace, path) click to toggle source
# File lib/lazy_names/config_loader.rb, line 24
def read_from_path(namespace, path)
  definitions = find_definitions(path, namespace)

  BasicConfig.new(path, definitions)
rescue Errno::ENOENT, Errno::ENOTDIR
  raise NoConfig, "No config found by given path: #{path}"
end
read_from_project(namespace) click to toggle source
# File lib/lazy_names/config_loader.rb, line 41
def read_from_project(namespace)
  return false unless config_in_project_path?

  definitions = find_definitions(project_path, namespace)

  BasicConfig.new(project_path, definitions)
end