class Fontist::Import::FormulaSerializer

Public Class Methods

new(formula, code) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 4
def initialize(formula, code)
  @formula = formula
  @code = code
end

Public Instance Methods

call() click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 9
def call
  to_h.compact
end

Private Instance Methods

collection_font?(font) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 64
def collection_font?(font)
  font[:styles].first[:font].end_with?(".ttc", ".TTC")
end
collection_font_style(style) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 80
def collection_font_style(style)
  Hash.new.tap do |h|
    h[:type] = style[:type]

    if style[:collection]
      h[:full_name] = collection_full_name(style[:collection])
    end
  end
end
collection_font_styles(styles) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 74
def collection_font_styles(styles)
  styles.map do |s|
    collection_font_style(s)
  end
end
collection_fonts(fonts) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 68
def collection_fonts(fonts)
  fonts.map do |f|
    { name: f[:name], styles: collection_font_styles(f[:styles]) }
  end
end
collection_full_name(collection_label) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 90
def collection_full_name(collection_label)
  if collection_label.is_a?(Hash)
    collection_label.values_at(:name, :style).join(" ")
  else
    collection_label
  end
end
collections(fonts) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 59
def collections(fonts)
  fonts.select { |f| collection_font?(f) }
    .group_by { |f| f[:styles].first[:font] }
end
extract_type(code) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/LineLength

# File lib/fontist/import/formula_serializer.rb, line 107
def extract_type(code)
  case code
  when /def extract.+exe_extract.+cab_extract.+ppviewer\.cab/m
    [{ format: :exe }, { format: :cab, file: "ppviewer.cab" }]
  when /def extract.+cab_extract/m
    { format: :cab }
  when /def extract.+(zip_extract|unzip).+fonts_sub_dir: "(.+?)"/m
    dir = code.match(/def extract.+(zip_extract|unzip).+fonts_sub_dir: "(.+?)"/m)[2]
    { format: :zip, options: { fonts_sub_dir: dir } }
  when /def extract.+(zip_extract|unzip)/m
    { format: :zip }
  else
    raise NotImplementedError, "Please implement an extract format"
  end
end
font_collections(fonts) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 49
def font_collections(fonts)
  collections = collections(fonts)
  return if collections.empty?

  collections.map do |filename, coll_fonts|
    { filename: filename,
      fonts: collection_fonts(coll_fonts) }
  end
end
fonts_attributes() click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 26
def fonts_attributes
  { display_progress_bar: formula_progress_bar(@formula),
    resources: @formula.resources,
    font_collections: font_collections(@formula.fonts),
    fonts: standalone_fonts(@formula.fonts),
    extract: extract_type(@code) }
end
formula_name(formula) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 41
def formula_name(formula)
  formula.class.name.match(/Formulas::(.+)Fonts?/)[1]
end
formula_progress_bar(formula) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 45
def formula_progress_bar(formula)
  formula.options&.dig(:progress_bar)
end
info_attributes() click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 19
def info_attributes
  { key: @formula.key,
    name: formula_name(@formula),
    description: @formula.description,
    homepage: @formula.homepage }
end
license_attributes() click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 34
def license_attributes
  { copyright: @formula.copyright,
    license_url: @formula.license_url,
    requires_license_agreement: requires_license_agreement(@formula),
    open_license: open_license(@formula) }
end
open_license(formula) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 128
def open_license(formula)
  formula.license if formula.license && !formula.license_required
end
requires_license_agreement(formula) click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/LineLength

# File lib/fontist/import/formula_serializer.rb, line 124
def requires_license_agreement(formula)
  formula.license if formula.license && formula.license_required
end
standalone_fonts(fonts) click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 98
def standalone_fonts(fonts)
  standalone = fonts.reject do |f|
    collection_font?(f)
  end

  standalone.empty? ? nil : standalone
end
to_h() click to toggle source
# File lib/fontist/import/formula_serializer.rb, line 15
def to_h
  info_attributes.merge(fonts_attributes).merge(license_attributes)
end