Class: Fontist::SystemIndexFontCollection

Inherits:
Lutaml::Model::Collection
  • Object
show all
Defined in:
lib/fontist/system_index.rb

Constant Summary collapse

ALLOWED_KEYS =
i[path full_name family_name type].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path.



32
33
34
# File 'lib/fontist/system_index.rb', line 32

def path
  @path
end

#paths_loaderObject

Returns the value of attribute paths_loader.



32
33
34
# File 'lib/fontist/system_index.rb', line 32

def paths_loader
  @paths_loader
end

Class Method Details

.from_file(path:, paths_loader:) ⇒ Object



46
47
48
49
50
51
# File 'lib/fontist/system_index.rb', line 46

def self.from_file(path:, paths_loader:)
  # If the file does not exist, return a new collection
  return new.set_content(path, paths_loader) unless File.exist?(path)

  from_yaml(File.read(path)).set_content(path, paths_loader)
end

Instance Method Details

#build(forced: false) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/fontist/system_index.rb', line 108

def build(forced: false)
  previous_index = load_index
  updated_fonts = update
  if forced || changed?(updated_fonts, previous_index.fonts || [])
    to_file(@path)
  end

  self
end

#check_indexObject

Check if the content has all required keys



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fontist/system_index.rb', line 63

def check_index
  Fontist.formulas_repo_path_exists!

  Array(fonts).each do |font|
    missing_keys = ALLOWED_KEYS.reject do |key|
      font.send(key)
    end

    raise_font_index_corrupted(font, missing_keys) if missing_keys.any?
  end
end

#find(font, style) ⇒ Object



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

def find(font, style)
  found_fonts = index.select do |file|
    file.family_name.casecmp?(font) &&
      (style.nil? || file.type.casecmp?(style))
  end

  found_fonts.empty? ? nil : found_fonts
end

#indexObject



89
90
91
92
93
94
95
96
# File 'lib/fontist/system_index.rb', line 89

def index
  return fonts unless index_changed?

  build
  check_index

  fonts
end

#index_changed?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/fontist/system_index.rb', line 98

def index_changed?
  fonts.nil? || fonts.empty? || font_paths != (@paths_loader&.call || []).sort.uniq
end

#rebuildObject



118
119
120
# File 'lib/fontist/system_index.rb', line 118

def rebuild
  build(forced: true)
end

#set_content(path, paths_loader) ⇒ Object



53
54
55
56
57
58
# File 'lib/fontist/system_index.rb', line 53

def set_content(path, paths_loader)
  tap do |content|
    content.set_path(path)
    content.set_path_loader(paths_loader)
  end
end

#set_path(path) ⇒ Object



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

def set_path(path)
  @path = path
end

#set_path_loader(paths_loader) ⇒ Object



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

def set_path_loader(paths_loader)
  @paths_loader = paths_loader
end

#to_file(path) ⇒ Object



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

def to_file(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, to_yaml)
end

#updateObject



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

def update
  tap do |col|
    col.fonts = detect_paths(@paths_loader&.call || [])
  end
end