Class: InlineSvg::TransformPipeline::Transformations::DataAttributes

Inherits:
Transformation
  • Object
show all
Defined in:
lib/inline_svg/transform_pipeline/transformations/data_attributes.rb

Instance Attribute Summary

Attributes inherited from Transformation

#value

Instance Method Summary collapse

Methods inherited from Transformation

create_with_value, #initialize

Constructor Details

This class inherits a constructor from InlineSvg::TransformPipeline::Transformations::Transformation

Instance Method Details

#transform(doc) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/inline_svg/transform_pipeline/transformations/data_attributes.rb', line 3

def transform(doc)
  doc = Nokogiri::XML::Document.parse(doc.to_html)
  svg = doc.at_css 'svg'
  with_valid_hash_from(self.value).each_pair do |name, data|
    svg["data-#{name}"] = data
  end
  doc
end

#with_valid_hash_from(hash) ⇒ Object



12
13
14
# File 'lib/inline_svg/transform_pipeline/transformations/data_attributes.rb', line 12

def with_valid_hash_from(hash)
  Hash.try_convert(hash) || {}
end