class Decontaminate::Decoder::Scalar

Attributes

transformer[R]
type[R]
xpath[R]

Public Class Methods

new(xpath, type, transformer) click to toggle source
# File lib/decontaminate/decoder/scalar.rb, line 6
def initialize(xpath, type, transformer)
  @xpath = xpath
  @type = type
  @transformer = transformer
end

Public Instance Methods

decode(this, xml_node) click to toggle source
# File lib/decontaminate/decoder/scalar.rb, line 12
def decode(this, xml_node)
  value = value_from_xml_node xml_node
  value = this.instance_exec(value, &transformer) if transformer
  value
end

Private Instance Methods

coerce_node_to_text(node) click to toggle source
# File lib/decontaminate/decoder/scalar.rb, line 28
def coerce_node_to_text(node)
  if node.is_a?(Nokogiri::XML::Text) || node.is_a?(Nokogiri::XML::Attr)
    node.to_s
  else
    node.at_xpath('text()').to_s
  end
end
coerce_string_to_boolean(str) click to toggle source
# File lib/decontaminate/decoder/scalar.rb, line 49
def coerce_string_to_boolean(str)
  str == 'true' || str == '1'
end
coerce_string_to_type(str, type) click to toggle source
# File lib/decontaminate/decoder/scalar.rb, line 36
def coerce_string_to_type(str, type)
  case type
  when :string
    str
  when :integer
    str.to_i
  when :float
    str.to_f
  when :boolean
    coerce_string_to_boolean str
  end
end
value_from_xml_node(xml_node) click to toggle source
# File lib/decontaminate/decoder/scalar.rb, line 20
def value_from_xml_node(xml_node)
  child = xml_node && xml_node.at_xpath(xpath)
  return unless child

  text = coerce_node_to_text child
  coerce_string_to_type text, type
end