module Coco class Svg < Coco::Component SVG_CACHE = {} InvalidSvgError = Class.new(StandardError) attr_reader :path def initialize(path: nil, **kwargs) @path = path.gsub(".svg", "") end def svg SVG_CACHE[path] ||= read_svg end 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 def full_path Coco::Engine.root.join("app/assets/build/coco/img/#{path}.svg") end end end