class Fontist::Import::RecursiveExtraction

Constants

LICENSE_PATTERN

Public Class Methods

new(archive, subarchive: nil, subdir: nil) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 9
def initialize(archive, subarchive: nil, subdir: nil)
  @archive = archive
  @subdir = subdir
  @operations = {}
  @font_files = []
  @collection_files = []

  save_operation_subdir
end

Public Instance Methods

extension() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 19
def extension
  fetch_extension(@archive)
end
font_collection_files() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 28
def font_collection_files
  ensure_extracted
  @collection_files
end
font_files() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 23
def font_files
  ensure_extracted
  @font_files
end
license_text() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 33
def license_text
  ensure_extracted
  @license_text
end
operations() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 38
def operations
  ensure_extracted
  @operations
end

Private Instance Methods

ensure_extracted() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 64
def ensure_extracted
  return if @extracted

  extract_data(@archive)
  @extracted = true
end
extract_data(archive) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 71
def extract_data(archive)
  Excavate::Archive.new(path(archive)).files(recursive_packages: true) do |path|
    match_license(path)
    match_font(path) if font_directory?(path)
  end
end
fetch_extension(file) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 52
def fetch_extension(file)
  File.extname(filename(file)).sub(/^\./, "")
end
filename(file) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 56
def filename(file)
  if file.respond_to?(:original_filename)
    file.original_filename
  else
    File.basename(file)
  end
end
font_directory?(path) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 99
def font_directory?(path)
  return true unless subdirectory_pattern

  File.fnmatch?(subdirectory_pattern, File.dirname(path))
end
license?(file) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 86
def license?(file)
  file.match?(LICENSE_PATTERN)
end
match_font(path) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 90
def match_font(path)
  case Files::FontDetector.detect(path)
  when :font
    @font_files << Otf::FontFile.new(path)
  when :collection
    @collection_files << Files::CollectionFile.new(path)
  end
end
match_license(path) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 82
def match_license(path)
  @license_text ||= File.read(path) if license?(path)
end
path(file) click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 78
def path(file)
  file.respond_to?(:path) ? file.path : file
end
save_operation_subdir() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 45
def save_operation_subdir
  return unless @subdir

  @operations[:options] ||= {}
  @operations[:options][:fonts_sub_dir] = @subdir
end
subdirectory_pattern() click to toggle source
# File lib/fontist/import/recursive_extraction.rb, line 105
def subdirectory_pattern
  @subdirectory_pattern ||= "*" + @subdir.chomp("/") if @subdir
end