Class: Fontist::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fontist/config.rb

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
# File 'lib/fontist/config.rb', line 5

def initialize
  @custom_values = load_config_file
end

Instance Method Details

#custom_valuesObject



13
14
15
# File 'lib/fontist/config.rb', line 13

def custom_values
  @custom_values
end

#default_value(key) ⇒ Object



40
41
42
# File 'lib/fontist/config.rb', line 40

def default_value(key)
  default_values[key.to_sym]
end

#default_valuesObject



44
45
46
47
48
49
# File 'lib/fontist/config.rb', line 44

def default_values
  { fonts_path: Fontist.fontist_path.join("fonts"),
    open_timeout: 10,
    read_timeout: 10,
    google_fonts_key: nil }
end

#delete(key) ⇒ Object



34
35
36
37
38
# File 'lib/fontist/config.rb', line 34

def delete(key)
  @custom_values.delete(key.to_sym)

  persist
end

#fonts_path=(value) ⇒ Object



61
62
63
# File 'lib/fontist/config.rb', line 61

def fonts_path=(value)
  @custom_values[:fonts_path] = File.expand_path(value)
end

#loadObject



57
58
59
# File 'lib/fontist/config.rb', line 57

def load
  @custom_values = load_config_file
end

#persistObject



51
52
53
54
55
# File 'lib/fontist/config.rb', line 51

def persist
  values = @custom_values.transform_keys(&:to_s)
  FileUtils.mkdir_p(File.dirname(Fontist.config_path))
  File.write(Fontist.config_path, YAML.dump(values))
end

#set(key, value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fontist/config.rb', line 17

def set(key, value)
  attr = key.to_sym
  unless default_values.key?(attr)
    raise Errors::InvalidConfigAttributeError,
          "No such attribute '#{attr}' exists."
  end

  v = normalize_value(value)
  if respond_to?("#{attr}=")
    public_send("#{attr}=", v)
  else
    @custom_values[attr] = v
  end

  persist
end

#valuesObject



9
10
11
# File 'lib/fontist/config.rb', line 9

def values
  default_values.merge(@custom_values)
end