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.



138
139
140
141
# File 'lib/fontist/config.rb', line 138

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

Class Method Details

.custom_valuesObject



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

def custom_values
  instance.custom_values
end

.default_value(key) ⇒ Object



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

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

.delete(key) ⇒ Object



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

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

.fonts_install_locationSymbol

Gets fonts installation location Priority: ENV > config file > default (“fontist”)

Returns:

  • (Symbol)

    :fontist, :user, or :system



73
74
75
76
77
78
79
# File 'lib/fontist/config.rb', line 73

def fonts_install_location
  value = ENV["FONTIST_INSTALL_LOCATION"] ||
    instance.custom_values[:fonts_install_location] ||
    "fontist"

  parse_location_value(value)
end

.from_file(path) ⇒ Object



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

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

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

.instanceObject



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

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

.resetObject



38
39
40
# File 'lib/fontist/config.rb', line 38

def reset
  @instance = nil
end

.set(key, value) ⇒ Object



50
51
52
# File 'lib/fontist/config.rb', line 50

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

.set_fonts_install_location(location) ⇒ Object

Sets fonts installation location in config file

Parameters:

  • location (String, Symbol)

    “fontist”, “user”, “system”, or symbols



84
85
86
87
# File 'lib/fontist/config.rb', line 84

def set_fonts_install_location(location)
  normalized = normalize_location_value(location)
  instance.set(:fonts_install_location, normalized)
end

.system_fonts_pathString?

Gets system fonts path Priority: ENV > config file > nil (use default in InstallLocation)

Returns:

  • (String, nil)

    System fonts path or nil for default



102
103
104
105
# File 'lib/fontist/config.rb', line 102

def system_fonts_path
  ENV["FONTIST_SYSTEM_FONTS_PATH"] ||
    instance.custom_values[:system_fonts_path]
end

.user_fonts_pathString?

Gets user fonts path Priority: ENV > config file > nil (use default in InstallLocation)

Returns:

  • (String, nil)

    User fonts path or nil for default



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

def user_fonts_path
  ENV["FONTIST_USER_FONTS_PATH"] ||
    instance.custom_values[:user_fonts_path]
end

.valuesObject



42
43
44
# File 'lib/fontist/config.rb', line 42

def values
  instance.values
end

Instance Method Details

#custom_valuesObject



147
148
149
# File 'lib/fontist/config.rb', line 147

def custom_values
  @custom_values
end

#default_value(key) ⇒ Object



174
175
176
# File 'lib/fontist/config.rb', line 174

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

#default_valuesObject



178
179
180
181
182
183
184
# File 'lib/fontist/config.rb', line 178

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

#delete(key) ⇒ Object



169
170
171
172
# File 'lib/fontist/config.rb', line 169

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

#loadObject



199
200
201
# File 'lib/fontist/config.rb', line 199

def load
  @custom_values = load_config_file
end

#persistObject



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fontist/config.rb', line 186

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

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

#set(key, value) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fontist/config.rb', line 151

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)

  # Expand fonts_path to absolute path
  v = File.expand_path(v.to_s) if attr == :fonts_path

  @custom_values[attr] = v
  send("#{attr}=", v) if respond_to?("#{attr}=")

  persist
end

#to_file(path) ⇒ Object



203
204
205
# File 'lib/fontist/config.rb', line 203

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

#valuesObject



143
144
145
# File 'lib/fontist/config.rb', line 143

def values
  default_values.merge(@custom_values)
end