Class: Fontist::Formula

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, path) ⇒ Formula

Returns a new instance of Formula.



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

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

Class Method Details

.allObject



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

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

.find(font_name) ⇒ Object



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

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

.find_by_font_file(font_file) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/fontist/formula.rb', line 55

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



48
49
50
51
52
53
# File 'lib/fontist/formula.rb', line 48

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

  new_from_file(path)
end

.find_fonts(font_name) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/fontist/formula.rb', line 26

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



22
23
24
# File 'lib/fontist/formula.rb', line 22

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

.find_styles(font_name, style_name) ⇒ Object



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

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

.new_from_file(path) ⇒ Object



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

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

.update_formulas_repoObject



8
9
10
# File 'lib/fontist/formula.rb', line 8

def self.update_formulas_repo
  Update.call
end

Instance Method Details



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

def copyright
  @data["copyright"]
end

#descriptionObject



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

def description
  @data["description"]
end

#digestObject



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

def digest
  @data["digest"]
end

#downloadable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/fontist/formula.rb', line 83

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

#extractObject



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

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

#file_sizeObject



131
132
133
134
135
# File 'lib/fontist/formula.rb', line 131

def file_size
  return unless @data["resources"]

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

#fontsObject



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

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

#fonts_by_name(name) ⇒ Object



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

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

#homepageObject



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

def homepage
  @data["homepage"]
end

#instructionsObject



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

def instructions
  @data["instructions"]
end

#keyObject



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

def key
  key_from_path
end

#licenseObject



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

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

#license_requiredObject



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

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

#license_urlObject



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

def license_url
  @data["license_url"]
end

#manual?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/fontist/formula.rb', line 79

def manual?
  !downloadable?
end

#min_fontistObject



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

def min_fontist
  @data["min_fontist"]
end

#pathObject



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

def path
  @path
end

#platformsObject



119
120
121
# File 'lib/fontist/formula.rb', line 119

def platforms
  @data["platforms"]
end

#resourcesObject



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

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

#style_override(font) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/fontist/formula.rb', line 159

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

#to_index_formulaObject



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

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