Class: Fontist::Config
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Fontist::Config
- Defined in:
- lib/fontist/config.rb
Class Method Summary collapse
- .custom_values ⇒ Object
- .default_value(key) ⇒ Object
- .delete(key) ⇒ Object
-
.fonts_install_location ⇒ Symbol
Gets fonts installation location Priority: ENV > config file > default (“fontist”).
- .from_file(path) ⇒ Object
- .instance ⇒ Object
- .reset ⇒ Object
- .set(key, value) ⇒ Object
-
.set_fonts_install_location(location) ⇒ Object
Sets fonts installation location in config file.
-
.system_fonts_path ⇒ String?
Gets system fonts path Priority: ENV > config file > nil (use default in InstallLocation).
-
.user_fonts_path ⇒ String?
Gets user fonts path Priority: ENV > config file > nil (use default in InstallLocation).
- .values ⇒ Object
Instance Method Summary collapse
- #custom_values ⇒ Object
- #default_value(key) ⇒ Object
- #default_values ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(**attrs) ⇒ Config
constructor
A new instance of Config.
- #load ⇒ Object
- #persist ⇒ Object
- #set(key, value) ⇒ Object
- #to_file(path) ⇒ Object
- #values ⇒ Object
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_values ⇒ Object
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_location ⇒ Symbol
Gets fonts installation location Priority: ENV > config file > default (“fontist”)
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 |
.instance ⇒ Object
34 35 36 |
# File 'lib/fontist/config.rb', line 34 def instance @instance ||= new.tap(&:load) end |
.reset ⇒ Object
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
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_path ⇒ String?
Gets system fonts path Priority: ENV > config file > nil (use default in InstallLocation)
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_path ⇒ String?
Gets user fonts path Priority: ENV > config file > nil (use default in InstallLocation)
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 |
.values ⇒ Object
42 43 44 |
# File 'lib/fontist/config.rb', line 42 def values instance.values end |
Instance Method Details
#custom_values ⇒ Object
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_values ⇒ Object
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 |
#load ⇒ Object
199 200 201 |
# File 'lib/fontist/config.rb', line 199 def load @custom_values = load_config_file end |
#persist ⇒ Object
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.(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 |
#values ⇒ Object
143 144 145 |
# File 'lib/fontist/config.rb', line 143 def values default_values.merge(@custom_values) end |