Class: Anyicon::Icon

Inherits:
Common show all
Defined in:
lib/anyicon/icon.rb

Overview

The Icon class is responsible for managing the rendering of icons from various collections available on GitHub. It handles downloading and caching of icons to ensure they are available for use in a Ruby on Rails application.

Example usage:

icon = Anyicon::Icon.new(icon: 'fontawesome_regular:address-book')
icon.render

The class supports additional properties that can be passed in to customize the SVG elements.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common

#fetch

Constructor Details

#initialize(icon:, **props) ⇒ Icon

Initializes a new Icon instance.

Parameters:

  • icon (String)

    a comma-separated string of icon names, each in the format ‘collection:name’

  • props (Hash)

    additional properties to apply to the SVG element



24
25
26
27
28
# File 'lib/anyicon/icon.rb', line 24

def initialize(icon:, **props)
  super()
  @icons = icon.split(',').map { |i| i.split(':') }
  @props = props
end

Class Method Details

.render(**kwargs) ⇒ String

Renders the SVG content for the specified icons.

Parameters:

  • kwargs (Hash)

    the parameters for initializing an Icon instance

Returns:

  • (String)

    the HTML-safe SVG content



118
119
120
# File 'lib/anyicon/icon.rb', line 118

def render(**kwargs)
  new(**kwargs).render
end

Instance Method Details

#renderString

Renders the SVG content for the specified icons.

Returns:

  • (String)

    the HTML-safe SVG content



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

def render
  result = ''.html_safe
  @icons.each do |icon|
    ensure_icon_exists(icon)
    result.concat(svg_content(icon).html_safe)
  end
  result
end