Module: Fontist::Indexes::IndexMixin
- Included in:
- DefaultFamilyFontIndex, FilenameIndex, PreferredFamilyFontIndex
- Defined in:
- lib/fontist/indexes/index_mixin.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #add_formula(formula) ⇒ Object
- #add_index_formula(style, formula_path) ⇒ Object
- #build ⇒ Object
- #index_key_for_style(_style) ⇒ Object
- #load_formulas(key) ⇒ Object
- #load_index_formulas(key) ⇒ Object
- #to_file(file_path = self.class.path) ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/fontist/indexes/index_mixin.rb', line 4 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#add_formula(formula) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fontist/indexes/index_mixin.rb', line 42 def add_formula(formula) raise unless formula.is_a?(Formula) formula.all_fonts.each do |font| font.styles.each do |style| add_index_formula(style, formula.path) end end entries end |
#add_index_formula(style, formula_path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/fontist/indexes/index_mixin.rb', line 59 def add_index_formula(style, formula_path) key = index_key_for_style(style) raise if key.nil? || key.empty? key = normalize_key(key) formula_path = Array(formula_path) paths = formula_path.map { |p| relative_formula_path(p) } if index_formula(key) index_formula(key).formula_path.concat(paths).uniq! return end entries << FormulaKeyToPath.new( key: key, formula_path: paths, ) end |
#build ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/fontist/indexes/index_mixin.rb', line 32 def build Formula.all.each do |formula| add_formula(formula) end to_file self end |
#index_key_for_style(_style) ⇒ Object
54 55 56 57 |
# File 'lib/fontist/indexes/index_mixin.rb', line 54 def index_key_for_style(_style) raise NotImplementedError, "index_key_for_style(style) must be implemented in including class" end |
#load_formulas(key) ⇒ Object
78 79 80 |
# File 'lib/fontist/indexes/index_mixin.rb', line 78 def load_formulas(key) index_formulas(key).flat_map(&:to_full) end |
#load_index_formulas(key) ⇒ Object
82 83 84 |
# File 'lib/fontist/indexes/index_mixin.rb', line 82 def load_index_formulas(key) index_formulas(key) end |
#to_file(file_path = self.class.path) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/fontist/indexes/index_mixin.rb', line 86 def to_file(file_path = self.class.path) # Use default path if file_path is nil file_path = self.class.path if file_path.nil? # puts "Writing index to #{file_path}" FileUtils.mkdir_p(File.dirname(file_path)) File.write(file_path, to_yaml) end |