Class: LocoMotion::Icons::Renderer
- Inherits:
-
Object
- Object
- LocoMotion::Icons::Renderer
- Defined in:
- lib/loco_motion/icons/renderer.rb
Overview
Renders an icon as inline SVG from a vendored or app-synced icon library.
Resolution checks, in order: the consuming application's own
<Rails.root>/app/assets/svg/icons (so any library a consumer syncs in —
or an icon they override — wins); in development only, the local icon
cache (config.icon_cache_path) so a newly-used icon renders without
re-running loco_motion:icons:sync; then LocoMotion's bundled icons
shipped inside the engine. This lets the default Heroicons work with zero
setup while letting consumers add or override icons without touching the
gem. Test and production skip the cache, so the committed set stays the
source of truth.
SVG files are parsed in XML mode so case-sensitive attributes like
viewBox (and clipPath, linearGradient, ...) keep their casing —
unlike HTML-mode parsing, which would lowercase them into invalid SVG.
Constant Summary collapse
- DEFAULT_LIBRARY =
:heroicons- DEFAULT_VARIANT =
:outline
Instance Method Summary collapse
-
#initialize(name:, library: DEFAULT_LIBRARY, variant: DEFAULT_VARIANT, attributes: {}) ⇒ Renderer
constructor
A new instance of Renderer.
-
#to_svg ⇒ String
The inline SVG markup with our attributes applied.
Constructor Details
#initialize(name:, library: DEFAULT_LIBRARY, variant: DEFAULT_VARIANT, attributes: {}) ⇒ Renderer
Returns a new instance of Renderer.
50 51 52 53 54 55 56 |
# File 'lib/loco_motion/icons/renderer.rb', line 50 def initialize(name:, library: DEFAULT_LIBRARY, variant: DEFAULT_VARIANT, attributes: {}) ref = Reference.parse(name, default_library: library || DEFAULT_LIBRARY, default_variant: variant) @name = ref[:name] @library = ref[:library].to_s @variant = ref[:variant]&.to_s @attributes = attributes || {} end |
Instance Method Details
#to_svg ⇒ String
Returns The inline SVG markup with our attributes applied.
61 62 63 64 65 66 |
# File 'lib/loco_motion/icons/renderer.rb', line 61 def to_svg document = Nokogiri::XML(::File.read(file_path)) svg = document.root apply_attributes(svg) svg.to_xml end |