class DirFriend::Config

Constants

CONFIG_FILE
CONFIG_PATH

Attributes

enable[RW]

Public Class Methods

read(theme) click to toggle source
# File lib/dir_friend/config.rb, line 7
def read(theme)
  new.read(theme)
end

Public Instance Methods

read(theme) click to toggle source
# File lib/dir_friend/config.rb, line 14
def read(theme)
  if theme
    use_passed_theme(theme)
  else
    use_default_theme
  end
end
themes() click to toggle source
# File lib/dir_friend/config.rb, line 22
def themes
  @themes ||= YAML.load_file(CONFIG_PATH).to_keysym_hash
rescue Errno::ENOENT
  create
  {}
rescue Psych::SyntaxError => e
  abort "Syntax errors found in your '#{CONFIG_FILE}': #{e}."
end

Private Instance Methods

create() click to toggle source
# File lib/dir_friend/config.rb, line 32
def create
  dir = File.dirname(CONFIG_PATH)
  Dir.mkdir(dir) unless Dir.exist?(dir)
  FileUtils.copy(template, CONFIG_PATH)
  puts "'#{CONFIG_FILE}' created in #{dir}."
  puts "You can set a theme for dot from pre-defined or created by you!"
rescue => e
  abort "Something go wrong: #{e}."
end
template() click to toggle source
# File lib/dir_friend/config.rb, line 42
def template
  File.join(__dir__, 'template.yaml')
end
use_default_theme() click to toggle source
# File lib/dir_friend/config.rb, line 52
def use_default_theme
  case defo = themes.delete(:default)
  when Symbol, String
    themes[defo.intern] || {}
  when Hash
    defo
  else
    {}
  end
end
use_passed_theme(theme) click to toggle source
# File lib/dir_friend/config.rb, line 46
def use_passed_theme(theme)
  themes[theme.intern].tap do |tm|
    abort "Theme: '#{theme}' not found in your #{CONFIG_FILE}." unless tm
  end
end