Sha256: 9f9efee83fe1be1da03b97deb06be12a99914e81fef9a068f0ad1742ee667318

Contents?: true

Size: 714 Bytes

Versions: 1

Compression:

Stored size: 714 Bytes

Contents

module Decontaminate
  module Decoder
    class Scalar
      attr_reader :xpath, :type

      def initialize(xpath, type)
        @xpath = xpath
        @type = type
      end

      def decode(xml_node)
        child = xml_node.at_xpath xpath
        text = coerce_node_to_text child

        return unless text

        case type
        when :string
          text
        when :integer
          text.to_i
        when :float
          text.to_f
        end
      end

      private

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decontaminate-0.1.0 lib/decontaminate/decoder/scalar.rb