Sha256: 077a86cc068b74aca0d46b6f022a46398ae77b328cd7d4e2fff487b81c1f21a8

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module ParserCore
  module BaseParser
    include Mega
    EMPTY_ARRAY = []

    attr_accessor :raw

    def initialize(raw)
      @raw = raw
    end

    def attributes
      raw.attributes
    end

    private

    def at(locator)
      return nil if raw.nil?

      element = raw.locate(locator.to_s).first

      if element
        if element.nodes.any? && element.nodes.all? { |node| node.is_a? Ox::CData }
          element.nodes.first.value
        else
          element.text
        end
      end
    end

    def attributes_at(locator)
      return nil if raw.nil?

      element = raw.locate(locator.to_s).first

      if element
        element.attributes
      end
    end

    def has?(locator)
      raw.locate(locator).any?
    end

    def submodel_at(klass, locator)
      element_xml = raw.locate(locator).first

      klass.new(element_xml) if element_xml
    end

    def array_of_at(klass, locator)
      return EMPTY_ARRAY if raw.nil?

      elements = raw.locate([*locator].join('/'))

      if klass == String && locator.size == 2
        elements.map do |element|
          klass.new(element.nodes.first)
        end
      else
        elements.map do |element|
          klass.new(element)
        end
      end
    end

    def to_h
      {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
damedata_parser_core-0.3.1 lib/parser_core/base_parser.rb