Class: Fontist::Formula

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/formula.rb

Constant Summary collapse

NAMESPACES =
{
  "sil" => "SIL",
  "macos" => "macOS",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, path) ⇒ Formula

Returns a new instance of Formula.



95
96
97
98
# File 'lib/fontist/formula.rb', line 95

def initialize(data, path)
  @data = data
  @path = real_path(path)
end

Class Method Details

.allObject



17
18
19
20
21
# File 'lib/fontist/formula.rb', line 17

def self.all
  Dir[Fontist.formulas_path.join("**/*.yml").to_s].map do |path|
    Formula.new_from_file(path)
  end
end

.all_keysObject



23
24
25
26
27
# File 'lib/fontist/formula.rb', line 23

def self.all_keys
  Dir[Fontist.formulas_path.join("**/*.yml").to_s].map do |path|
    path.sub("#{Fontist.formulas_path}/", "").sub(".yml", "")
  end
end

.find(font_name) ⇒ Object



29
30
31
# File 'lib/fontist/formula.rb', line 29

def self.find(font_name)
  Indexes::FontIndex.from_yaml.load_formulas(font_name).first
end

.find_by_font_file(font_file) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/fontist/formula.rb', line 80

def self.find_by_font_file(font_file)
  key = Indexes::FilenameIndex
    .from_yaml
    .load_index_formulas(File.basename(font_file))
    .map(&:name)
    .first

  find_by_key(key)
end

.find_by_key(key) ⇒ Object



63
64
65
66
67
68
# File 'lib/fontist/formula.rb', line 63

def self.find_by_key(key)
  path = Fontist.formulas_path.join("#{key}.yml")
  return unless File.exist?(path)

  new_from_file(path)
end

.find_by_key_or_name(name) ⇒ Object



59
60
61
# File 'lib/fontist/formula.rb', line 59

def self.find_by_key_or_name(name)
  find_by_key(name) || find_by_name(name)
end

.find_by_name(name) ⇒ Object



70
71
72
73
74
# File 'lib/fontist/formula.rb', line 70

def self.find_by_name(name)
  key = name_to_key(name)

  find_by_key(key)
end

.find_fonts(font_name) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/fontist/formula.rb', line 37

def self.find_fonts(font_name)
  formulas = Indexes::FontIndex.from_yaml.load_formulas(font_name)

  formulas.map do |formula|
    formula.fonts.select do |f|
      f.name.casecmp?(font_name)
    end
  end.flatten
end

.find_many(font_name) ⇒ Object



33
34
35
# File 'lib/fontist/formula.rb', line 33

def self.find_many(font_name)
  Indexes::FontIndex.from_yaml.load_formulas(font_name)
end

.find_styles(font_name, style_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fontist/formula.rb', line 47

def self.find_styles(font_name, style_name)
  formulas = Indexes::FontIndex.from_yaml.load_formulas(font_name)

  formulas.map do |formula|
    formula.fonts.map do |f|
      f.styles.select do |s|
        f.name.casecmp?(font_name) && s.type.casecmp?(style_name)
      end
    end
  end.flatten
end

.name_to_key(name) ⇒ Object



76
77
78
# File 'lib/fontist/formula.rb', line 76

def self.name_to_key(name)
  name.downcase.gsub(" ", "_")
end

.new_from_file(path) ⇒ Object



90
91
92
93
# File 'lib/fontist/formula.rb', line 90

def self.new_from_file(path)
  data = YAML.load_file(path)
  new(data, path)
end

.update_formulas_repoObject



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

def self.update_formulas_repo
  Update.call
end

Instance Method Details



134
135
136
# File 'lib/fontist/formula.rb', line 134

def copyright
  @data["copyright"]
end

#descriptionObject



126
127
128
# File 'lib/fontist/formula.rb', line 126

def description
  @data["description"]
end

#digestObject



192
193
194
# File 'lib/fontist/formula.rb', line 192

def digest
  @data["digest"]
end

#downloadable?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/fontist/formula.rb', line 108

def downloadable?
  @data.key?("resources")
end

#extractObject



158
159
160
# File 'lib/fontist/formula.rb', line 158

def extract
  Helpers.parse_to_object(@data["extract"])
end

#file_sizeObject



162
163
164
165
166
# File 'lib/fontist/formula.rb', line 162

def file_size
  return unless @data["resources"]

  @data["resources"].values.first["file_size"]&.to_i
end

#font_by_name(name) ⇒ Object



176
177
178
179
180
# File 'lib/fontist/formula.rb', line 176

def font_by_name(name)
  fonts.find do |font|
    font.name.casecmp?(name)
  end
end

#fontsObject



188
189
190
# File 'lib/fontist/formula.rb', line 188

def fonts
  @fonts ||= Helpers.parse_to_object(fonts_by_family)
end

#fonts_by_name(name) ⇒ Object



182
183
184
185
186
# File 'lib/fontist/formula.rb', line 182

def fonts_by_name(name)
  fonts.select do |font|
    font.name.casecmp?(name)
  end
end

#homepageObject



130
131
132
# File 'lib/fontist/formula.rb', line 130

def homepage
  @data["homepage"]
end

#instructionsObject



172
173
174
# File 'lib/fontist/formula.rb', line 172

def instructions
  @data["instructions"]
end

#keyObject



116
117
118
119
# File 'lib/fontist/formula.rb', line 116

def key
  @key ||= {}
  @key[@path] ||= key_from_path
end

#licenseObject



142
143
144
# File 'lib/fontist/formula.rb', line 142

def license
  @data["open_license"] || @data["requires_license_agreement"]
end

#license_requiredObject



146
147
148
# File 'lib/fontist/formula.rb', line 146

def license_required
  @data["requires_license_agreement"] ? true : false
end

#license_urlObject



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

def license_url
  @data["license_url"]
end

#manual?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/fontist/formula.rb', line 104

def manual?
  !downloadable?
end

#min_fontistObject



154
155
156
# File 'lib/fontist/formula.rb', line 154

def min_fontist
  @data["min_fontist"]
end

#nameObject



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

def name
  @name ||= {}
  @name[key] ||= namespace.empty? ? base_name : "#{namespace}/#{base_name}"
end

#pathObject



112
113
114
# File 'lib/fontist/formula.rb', line 112

def path
  @path
end

#platformsObject



150
151
152
# File 'lib/fontist/formula.rb', line 150

def platforms
  @data["platforms"]
end

#resourcesObject



168
169
170
# File 'lib/fontist/formula.rb', line 168

def resources
  Helpers.parse_to_object(@data["resources"]&.values)
end

#style_override(font) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/fontist/formula.rb', line 196

def style_override(font)
  fonts
    .map(&:styles)
    .flatten
    .detect { |s| s.family_name == font }
    &.dig(:override) || {}
end

#to_index_formulaObject



100
101
102
# File 'lib/fontist/formula.rb', line 100

def to_index_formula
  Indexes::IndexFormula.new(path)
end