Class: ColorLS::Yaml

Inherits:
Object
  • Object
show all
Defined in:
lib/colorls/yaml.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Yaml

Returns a new instance of Yaml.



5
6
7
8
# File 'lib/colorls/yaml.rb', line 5

def initialize(filename)
  @filepath = File.join(File.dirname(__FILE__),"../yaml/#{filename}")
  @user_config_filepath = File.join(Dir.home, ".config/colorls/#{filename}")
end

Instance Method Details

#load(aliase: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/colorls/yaml.rb', line 10

def load(aliase: false)
  yaml = read_file(@filepath)
  if File.exist?(@user_config_filepath)
    user_config_yaml = read_file(@user_config_filepath)
    yaml = yaml.merge(user_config_yaml)
  end

  return yaml unless aliase

  yaml.to_a.map! { |k, v| v.include?('#') ? [k, v] : [k, v.to_sym] }.to_h
end

#read_file(filepath) ⇒ Object



22
23
24
# File 'lib/colorls/yaml.rb', line 22

def read_file(filepath)
  ::YAML.safe_load(File.read(filepath, encoding: Encoding::UTF_8)).transform_keys!(&:to_sym)
end