Method: WebifyRuby::Convert#initialize

Defined in:
lib/webify_ruby/convert.rb

#initialize(file, dir: nil, css: nil, link_to: nil, unique_id: nil, html: nil) ⇒ Convert

Public: Initialize a Convertion of font-file.

file - A String containing relative or full path of file to convert. :dir - A String indicating to the desired to save converted files (optional). :css - A String or Boolean value indicating a desired CSS behavior.

If present, it can be either directory path in String or Boolean.
If value is set to true, then a stylesheet file won't be created,
but code will become accessible as :styles attribute (optional).

:link_to - A String notation indicating how to prefix a font url in CSS (optional). :unique_id - A custom identifier which will separate different fonts (optional). :html - If present, it will create an HTML file in the given directory.

Note: CSS has to be set to generate a file too. (optional).

Returns nothing. Raises Errno::ENOENT if the inputted file cannot be found. Raises Error if the inputted font file is not withing valid extensions. Raises Error::ENOENT if the directory of inputted file is not accessible.

Raises:

  • (Errno::ENOENT)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/webify_ruby/convert.rb', line 56

def initialize(file, dir: nil, css: nil, link_to: nil, unique_id: nil, html: nil)
  [file, dir, css, link_to, unique_id, html]

  @desired_dir = dir
  @css = css
  @link_to = link_to
  @unique_id = unique_id
  @html = html

  raise Errno::ENOENT, "The font file '#{file}' does not exist" unless File.exists?(file)
  @original_file = file

  raise Error, "The font file '#{file}' is not valid" unless WebifyRuby::EXT.include? File.extname(@original_file)

  @original_dir = File.dirname(@original_file)
  raise Errno::ENOENT, "Can't find directory '#{@original_dir}'" unless File.directory? @original_dir

  @result_dir = Dir.mktmpdir(nil, destination_dir)
  
  unless @unique_id.nil?
    custom_dir = File.join(File.expand_path('..', @result_dir), @unique_id)
    FileUtils.mv @result_dir, custom_dir
    FileUtils.rm_rf File.join(custom_dir, File.basename(@result_dir))
    @result_dir = custom_dir
  end

  FileUtils.cp(@original_file, @result_dir)

  @file = File.join(@result_dir, File.basename(@original_file))

  process

  
  if affected_files.to_a.length == 0
    # :nocov:
    WebifyRuby.logger.info "Host did not create any files\n@command\n#{@command}\n@output\n#{@output}\n"
    # :nocov:
  end

  generate_css unless @css.nil?
  generate_html unless @css_file.nil? or @html.nil?
end