Sha256: 9422a855f1caffa9e3ebf23c70e1bdd229d146a9c68cb6d4fb12e004388dd7f6

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require "rails_icons/icon/attributes"

class RailsIcons::Icon
  def initialize(name:, library:, set:, args:)
    @name = name
    @library = library.to_s
    @set = set
    @args = args
  end

  def svg
    raise RailsIcons::NotFound, error_message unless File.exist?(file_path)

    svg_file = Nokogiri::HTML::DocumentFragment.parse(File.read(file_path)).at_css("svg")

    attach_attributes to: svg_file

    svg_file.to_html.html_safe
  end

  private

  def error_message
    "Icon not found: `#{@library} / #{set} / #{@name}`"
  end

  def file_path
    custom_library.dig("path") ||
      Rails.root.join("app", "assets", "svg", "icons", @library, set, "#{@name}.svg")
  end

  def custom_library?
    custom_library.present?
  end

  def attach_attributes(to:)
    RailsIcons::Icon::Attributes
      .new(default_attributes: default_attributes, args: @args)
      .attach(to: to)
  end

  def default_attributes
    {
      "stroke-width": default_stroke_width,
      class: default_css,
      data: default_data
    }
  end

  def set
    return @set if @set.present?

    RailsIcons.configuration.default_set
  end

  def default_css
    library_set_attributes.dig(:default, :css)
  end

  def default_data
    library_set_attributes.dig(:default, :data)
  end

  def default_stroke_width
    library_set_attributes.dig(:default, :stroke_width)
  end

  def library_set_attributes
    return custom_library || {} if custom_library?

    RailsIcons.configuration.libraries.dig(@library, set) || {}
  end

  def custom_library
    RailsIcons
      .configuration
      .libraries
      .dig("custom")
      &.with_indifferent_access
      &.dig(@library, set) || {}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_icons-0.4.0 lib/rails_icons/icon.rb
rails_icons-0.3.0 lib/rails_icons/icon.rb