Sha256: 7ae0df206e0a8136beebc20946892d173db0fd6689153641d8aa74e1c6c9ba7e

Contents?: true

Size: 891 Bytes

Versions: 4

Compression:

Stored size: 891 Bytes

Contents

module MoneyS3
  module Parsers
    module BaseParser
      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
          StringWithAttributes.new(element.text, 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('/'))

        elements.map do |element|
          klass.new(element)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
money_s3-0.10.0 lib/money_s3/parsers/base_parser.rb
money_s3-0.9.0 lib/money_s3/parsers/base_parser.rb
money_s3-0.8.0 lib/money_s3/parsers/base_parser.rb
money_s3-0.7.0 lib/money_s3/parsers/base_parser.rb