Class: FIS::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/fis/configuration.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



10
11
12
13
14
15
16
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/fis/configuration.rb', line 8

def config
  @config
end

#file_full_pathObject (readonly)

Returns the value of attribute file_full_path.



8
9
10
# File 'lib/fis/configuration.rb', line 8

def file_full_path
  @file_full_path
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



8
9
10
# File 'lib/fis/configuration.rb', line 8

def file_name
  @file_name
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



8
9
10
# File 'lib/fis/configuration.rb', line 8

def file_path
  @file_path
end

Instance Method Details

#fetch(*keys) ⇒ Object



18
19
20
21
# File 'lib/fis/configuration.rb', line 18

def fetch(*keys)
  key = keys.pop
  keys.inject(@config, :fetch)[key]
end

#readObject



37
38
39
40
41
42
43
44
45
46
47
# 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

#rereadObject



49
50
51
# File 'lib/fis/configuration.rb', line 49

def reread
  @config = read
end

#set(*keys) ⇒ Object



23
24
25
26
27
28
# File 'lib/fis/configuration.rb', line 23

def set(*keys)
  key = keys.pop
  keys.inject(@config, :fetch)[key] = yield

  write!
end

#unset(*keys) ⇒ Object



30
31
32
33
34
35
# File 'lib/fis/configuration.rb', line 30

def unset(*keys)
  set(*keys) do
    key = keys.pop
    keys.inject(default, :fetch)[key]
  end
end

#write!Object



53
54
55
56
57
58
59
60
# 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