Class: Fontist::Config

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/fontist/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Config

Returns a new instance of Config.



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

def initialize(**attrs)
  @custom_values = {}
  super
end

Class Method Details

.custom_valuesObject



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

def custom_values
  instance.custom_values
end

.default_value(key) ⇒ Object



48
49
50
# File 'lib/fontist/config.rb', line 48

def default_value(key)
  instance.default_value(key)
end

.delete(key) ⇒ Object



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

def delete(key)
  instance.delete(key)
end

.from_file(path) ⇒ Object



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

def from_file(path)
  return new unless File.exist?(path)

  content = File.read(path)
  from_yaml(content)
end

.instanceObject



28
29
30
# File 'lib/fontist/config.rb', line 28

def instance
  @instance ||= new.tap(&:load)
end

.set(key, value) ⇒ Object



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

def set(key, value)
  instance.set(key, value)
end

.valuesObject



32
33
34
# File 'lib/fontist/config.rb', line 32

def values
  instance.values
end

Instance Method Details

#custom_valuesObject



69
70
71
# File 'lib/fontist/config.rb', line 69

def custom_values
  @custom_values
end

#default_value(key) ⇒ Object



92
93
94
# File 'lib/fontist/config.rb', line 92

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

#default_valuesObject



96
97
98
99
100
101
# File 'lib/fontist/config.rb', line 96

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

#delete(key) ⇒ Object



87
88
89
90
# File 'lib/fontist/config.rb', line 87

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

#fonts_path=(value) ⇒ Object



117
118
119
120
# File 'lib/fontist/config.rb', line 117

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

#loadObject



113
114
115
# File 'lib/fontist/config.rb', line 113

def load
  @custom_values = load_config_file
end

#persistObject



103
104
105
106
107
108
109
110
111
# File 'lib/fontist/config.rb', line 103

def persist
  config_model = self.class.new
  @custom_values.each do |key, value|
    config_model.send("#{key}=", value) if config_model.respond_to?("#{key}=")
  end

  FileUtils.mkdir_p(File.dirname(Fontist.config_path))
  config_model.to_file(Fontist.config_path)
end

#set(key, value) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fontist/config.rb', line 73

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)
  @custom_values[attr] = v
  send("#{attr}=", v) if respond_to?("#{attr}=")

  persist
end

#to_file(path) ⇒ Object



122
123
124
# File 'lib/fontist/config.rb', line 122

def to_file(path)
  File.write(path, to_yaml)
end

#valuesObject



65
66
67
# File 'lib/fontist/config.rb', line 65

def values
  default_values.merge(@custom_values)
end