Class: Coco::Svg

Inherits:
Component
  • Object
show all
Defined in:
app/components/coco/base/svg/svg.rb

Constant Summary collapse

SVG_CACHE =
{}
InvalidSvgError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, **kwargs) ⇒ Svg

Returns a new instance of Svg.



9
10
11
# File 'app/components/coco/base/svg/svg.rb', line 9

def initialize(path: nil, **kwargs)
  @path = path.gsub(".svg", "")
end

Instance Attribute Details

#path ⇒ Object (readonly)

Returns the value of attribute path.



7
8
9
# File 'app/components/coco/base/svg/svg.rb', line 7

def path
  @path
end

Instance Method Details

#full_path ⇒ Object



25
26
27
# File 'app/components/coco/base/svg/svg.rb', line 25

def full_path
  Coco::Engine.root.join("app/assets/build/coco/img/#{path}.svg")
end

#read_svg ⇒ Object



17
18
19
20
21
22
23
# File 'app/components/coco/base/svg/svg.rb', line 17

def read_svg
  File.read(full_path).html_safe
rescue
  if Rails.env.development? || Rails.env.test?
    raise InvalidSvgError, "The SVG `#{full_path}` was not found"
  end
end

#svg ⇒ Object



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

def svg
  SVG_CACHE[path] ||= read_svg
end