class Shipyard::Icons

Attributes

icons[R]

Public Class Methods

new(icon_directory, output_directory, base_path = '') click to toggle source
# File lib/shipyard-framework/icons.rb, line 10
def initialize(icon_directory, output_directory, base_path = '')
  @path = icon_directory
  @public = output_directory
  @base_path = base_path
  reload
end

Public Instance Methods

asset_path(svg_id) click to toggle source
# File lib/shipyard-framework/icons.rb, line 26
def asset_path(svg_id)
  "#{base_path}##{svg_id}"
end
base_path() click to toggle source
# File lib/shipyard-framework/icons.rb, line 22
def base_path
  "#{@base_path}/assets/icons.svg"
end
reload() click to toggle source
# File lib/shipyard-framework/icons.rb, line 17
def reload
  @icons = load_svgs.freeze
  @icons
end
write() click to toggle source
# File lib/shipyard-framework/icons.rb, line 30
def write
  html = []
  @icons.each do |icon|
    html << svg_symbol(icon)
  end
  FileUtils.mkdir_p(@public) unless File.exists?(@public) || Dir.exists?(@public)
  File.write("#{@public}/icons.svg", svg_template(html.join))
end

Private Instance Methods

files() click to toggle source
# File lib/shipyard-framework/icons.rb, line 69
def files
  files = Dir[Pathname(Shipyard.icons_path).join('**/*.svg')]
  files.concat Dir[Pathname(@path).join('**/*.svg')]
end
load_svgs() click to toggle source
# File lib/shipyard-framework/icons.rb, line 41
def load_svgs
  files.sort.map do |file|
    html = File.read(file)
    {
      file: file,
      path: file.gsub(@path, ''),
      id: File.basename(file).gsub(/.svg/, ''),
      symbol: File.basename(file).gsub(/.svg/, '').underscore.to_sym,
      view_box: html[/viewBox="(.*?)"/, 1],
      outer_html: html.gsub(/\n|\s+\s+/, ''),
      inner_html: html[/<svg.*?>([\s\S]*?)<\/svg>/, 1].gsub(/\n|\s+\s+/, ''),
      is_outlined: html.include?('non-scaling-stroke')
    }
  end
end
raise_error(name) click to toggle source
# File lib/shipyard-framework/icons.rb, line 74
def raise_error(name)
  return unless ::Rails.env.development? || ::Rails.env.test?
  raise "Missing icon #{name}"
end
svg_symbol(icon) click to toggle source
# File lib/shipyard-framework/icons.rb, line 57
def svg_symbol(icon)
  %(<g id="#{icon[:id]}" viewBox="#{icon[:view_box]}">#{icon[:inner_html]}</g>)
end
svg_template(html) click to toggle source
# File lib/shipyard-framework/icons.rb, line 61
def svg_template(html)
  %(
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      #{html}
    </svg>
  ).gsub(/\n|\s+\s+/, '')
end
updater() click to toggle source
# File lib/shipyard-framework/icons.rb, line 79
def updater
  @updater ||= ActiveSupport::FileUpdateChecker.new([], @path => [:svg]) do
    reload
  end
end