class Healthicons::Transform
Constants
- DEFAULT_OPTIONS
- HEIGHT_AND_WIDTH
- KEYS_TO_REMOVE
Attributes
Public Class Methods
@param svg [String] @param options [Hash] @return [String]
# File lib/healthicons/transform.rb, line 16 def self.call(svg, options = {}) new(svg, options).render end
Used when transforming a new SVG::XML icon
@param svg [String] @param options [Hash]
# File lib/healthicons/transform.rb, line 24 def initialize(svg, options = {}) @svg = parse_icon_content(svg) @options = cleanup_options(options) end
Public Instance Methods
Generate and return the SVG's altered XML contents
@return [String]
# File lib/healthicons/transform.rb, line 32 def render return '' if @svg.blank? add_icon_options @svg&.to_xml end
Private Instance Methods
Apply defaults to the options hash
@param options [Hash] @return [Hash]
# File lib/healthicons/transform.rb, line 55 def add_default_options(options) DEFAULT_OPTIONS.merge(options) end
Loop through any and all options and add them to the <svg> element
@return [Hash]
# File lib/healthicons/transform.rb, line 74 def add_icon_options return if @options.blank? || !options.is_a?(Hash) set_size_and_width @options.reject { |k| k == :size }.each do |key, val| set_icon_attribute(key, val) end end
Cleanup the options hash by removing unecessary values and appling defaults if they are missing
@param options [Hash] @return [Hash]
# File lib/healthicons/transform.rb, line 45 def cleanup_options(options = {}) options = {} unless options.is_a?(Hash) add_default_options(scrub_options(options)) end
Load and parse icon XML contents with Nokogiri
@param icon_contents [String] @return [String] @note
https://nokogiri.org/tutorials/modifying_an_html_xml_document.html https://nokogiri.org/rdoc/Nokogiri/XML/Builder.html https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Node
# File lib/healthicons/transform.rb, line 108 def parse_icon_content(icon_contents) Nokogiri::HTML::DocumentFragment.parse(icon_contents) rescue '' end
Remove height and width attrbutes, since the SVG needs to have the same height/size
To change the icons size you need to specify the icons size with `size:`
@param options [Hash] @return [Hash]
# File lib/healthicons/transform.rb, line 64 def scrub_options(options = {}) return {} unless options.is_a?(Hash) KEYS_TO_REMOVE.each { |k| options.delete(k) } options end
Used to add the attribute/value to the SVG
@param attr [String] @param val [String] @return [Hash]
# File lib/healthicons/transform.rb, line 96 def set_icon_attribute(attr, val) @svg.child.set_attribute(attr, val.to_s) end
Set the SVG icons height weight based on size,
This is because they need to be proportionate to eachother
@return [Hash]
# File lib/healthicons/transform.rb, line 87 def set_size_and_width HEIGHT_AND_WIDTH.map { |k| set_icon_attribute(k, @options[:size].to_i) } end
Set the icons width/height
@return [String, Integer]
# File lib/healthicons/transform.rb, line 117 def size options[:size].presence end