Class: Icons::Icon

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/icons/icon.rb,
lib/icons/icon/file_path.rb,
lib/icons/icon/attributes.rb,
lib/icons/icon/configurable.rb

Defined Under Namespace

Modules: Configurable Classes: Attributes, FilePath

Instance Method Summary collapse

Constructor Details

#initialize(name:, library:, arguments:, variant: nil) ⇒ Icon

Returns a new instance of Icon.

Parameters:

  • name (String)

    The icon name

  • library (String, Symbol)

    The icon library

  • variant (String, Symbol, nil) (defaults to: nil)

    The icon variant (optional)

  • arguments (Hash)

    Additional attributes including class, data, stroke_width, etc.



17
18
19
20
21
22
23
24
# File 'lib/icons/icon.rb', line 17

def initialize(name:, library:, arguments:, variant: nil)
  @config = Icons.configuration

  @name = name
  @library = library.to_sym
  @variant = (variant || set_variant)&.to_sym
  @arguments = arguments
end

Instance Method Details

#svgString

Returns the rendered SVG markup for the icon

Returns:

  • (String)

    The SVG markup as an HTML string

Raises:



32
33
34
35
36
37
38
39
# File 'lib/icons/icon.rb', line 32

def svg
  Nokogiri::HTML::DocumentFragment.parse(File.read(file_path))
    .at_css("svg")
    .tap { |svg| attach_attributes(to: svg) }
    .to_html
rescue Errno::ENOENT
  raise Icons::IconNotFound, error_message
end