Class: Coco::Icon

Inherits:
Component
  • Object
show all
Includes:
Concerns::AcceptsOptions
Defined in:
app/components/coco/base/icon/icon.rb

Constant Summary collapse

ICON_CACHE =
{}
InvalidIconError =
Class.new(StandardError)

Instance Method Summary collapse

Methods included from Concerns::AcceptsOptions

#accepted_options, #get_option_group

Constructor Details

#initialize(name: nil, **kwargs) ⇒ Icon

Returns a new instance of Icon.



13
14
15
16
# File 'app/components/coco/base/icon/icon.rb', line 13

def initialize(name: nil, **kwargs)
  @icon_name = name&.to_s&.tr("_", "-")
  set_tag_data_attr(:icon, name) if name
end

Instance Method Details

#name ⇒ Object



18
19
20
# File 'app/components/coco/base/icon/icon.rb', line 18

def name
  @icon_name
end

#read_svg ⇒ Object



26
27
28
29
30
31
32
# File 'app/components/coco/base/icon/icon.rb', line 26

def read_svg
  File.read(svg_path).html_safe
rescue
  if Rails.env.development? || Rails.env.test?
    raise InvalidIconError, "`#{name}` is not a valid icon name"
  end
end

#svg ⇒ Object



22
23
24
# File 'app/components/coco/base/icon/icon.rb', line 22

def svg
  ICON_CACHE[name] ||= read_svg
end

#svg_path ⇒ Object



34
35
36
# File 'app/components/coco/base/icon/icon.rb', line 34

def svg_path
  Coco::Engine.root.join("app/assets/build/coco/icons/#{name}.svg")
end