class SvgSprite::SVG

Attributes

filepath[R]
fill[R]
optimize[R]
stroke[R]

Public Class Methods

new(filepath, optimize:, stroke:, fill:) click to toggle source
# File lib/svg_sprite/svg.rb, line 7
def initialize(filepath, optimize:, stroke:, fill:)
  @filepath = filepath
  @optimize = optimize
  @stroke = stroke
  @fill = fill
end

Public Instance Methods

height() click to toggle source
# File lib/svg_sprite/svg.rb, line 28
def height
  symbol[:height]
end
id() click to toggle source
# File lib/svg_sprite/svg.rb, line 32
def id
  File.basename(filepath, ".*")
end
symbol() click to toggle source
# File lib/svg_sprite/svg.rb, line 14
def symbol
  @symbol ||= xml.css("svg").first.clone.tap do |node|
    node.name = "symbol"
    node.set_attribute :id, id

    process_stroke(node)
    process_fill(node)
  end
end
width() click to toggle source
# File lib/svg_sprite/svg.rb, line 24
def width
  symbol[:width]
end

Private Instance Methods

process_attribute(symbol, attribute, value) click to toggle source
# File lib/svg_sprite/svg.rb, line 52
        def process_attribute(symbol, attribute, value)
  return unless value

  symbol.css("[#{attribute}]").each do |node|
    if value == "current-color" && node[attribute] != "none"
      node.set_attribute(attribute, "currentColor")
    end

    if value == "remove" && node[attribute] != "none"
      node.remove_attribute(attribute)
    end
  end
end
process_fill(node) click to toggle source
# File lib/svg_sprite/svg.rb, line 48
        def process_fill(node)
  process_attribute(node, "fill", fill)
end
process_stroke(node) click to toggle source
# File lib/svg_sprite/svg.rb, line 44
        def process_stroke(node)
  process_attribute(node, "stroke", stroke)
end
xml() click to toggle source
# File lib/svg_sprite/svg.rb, line 36
        def xml
  @xml ||= begin
    contents = File.read(filepath)
    contents = SvgOptimizer.optimize(contents) if optimize
    Nokogiri::XML(contents)
  end
end