class FIS::Configuration

Handles the global configuration of the gem via ~/.flatiron-school/configuration.yml

Attributes

config[R]
file_full_path[R]
file_name[R]
file_path[R]

Public Class Methods

new() click to toggle source
# File lib/fis/configuration.rb, line 10
def initialize
  @file_path = File.join(Dir.home, '.flatiron-school')
  @file_name = 'configuration.yml'
  @file_full_path = File.join(@file_path, @file_name)

  @config = read
end

Public Instance Methods

fetch(*keys) click to toggle source
# File lib/fis/configuration.rb, line 18
def fetch(*keys)
  key = keys.pop
  keys.inject(@config, :fetch)[key]
end
read() click to toggle source
# File lib/fis/configuration.rb, line 37
def read
  return default unless File.exist?(@file_full_path)

  deep_merge(
    default,
    YAML.safe_load(
      File.open(@file_full_path).read,
      [Symbol]
    )
  )
end
reread() click to toggle source
# File lib/fis/configuration.rb, line 49
def reread
  @config = read
end
set(*keys) { || ... } click to toggle source
# File lib/fis/configuration.rb, line 23
def set(*keys)
  key = keys.pop
  keys.inject(@config, :fetch)[key] = yield

  write!
end
unset(*keys) click to toggle source
# File lib/fis/configuration.rb, line 30
def unset(*keys)
  set(*keys) do
    key = keys.pop
    keys.inject(default, :fetch)[key]
  end
end
write!() click to toggle source
# File lib/fis/configuration.rb, line 53
def write!
  Dir.mkdir(@file_path, 0o700) unless Dir.exist?(@file_path)

  File.open(@file_full_path, 'w') do |file|
    yaml = deep_merge(default, @config).to_yaml
    file.write(yaml)
  end
end

Private Instance Methods

deep_merge(this_hash, other_hash) click to toggle source
# File lib/fis/configuration.rb, line 78
def deep_merge(this_hash, other_hash)
  this_hash.merge(other_hash) do |_key, this_val, other_val|
    if this_val.is_a?(::Hash) && other_val.is_a?(::Hash)
      deep_merge(this_val, other_val)
    else
      other_val
    end
  end
end
default() click to toggle source
# File lib/fis/configuration.rb, line 64
def default
  {
    identity: {
      portal: {
        token: nil
      },
      github: {
        id: nil,
        username: nil
      }
    }
  }
end