Sha256: fa1e41aaf7d840df30b8edbd368f63f73e35ab279e63ac0845fb0f85e3f94e05

Contents?: true

Size: 968 Bytes

Versions: 2

Compression:

Stored size: 968 Bytes

Contents

module MoneyS3
  module Parsers
    module BaseParser
      EMPTY_ARRAY = []

      attr_accessor :raw

      def initialize(raw)
        @raw = raw
      end

      private

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

        raw[locator]
      end

      def submodel_at(klass, locator)
        element_xml = at locator

        klass.new(element_xml) if element_xml
      end

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

        elements = raw.dig(*locator) || EMPTY_ARRAY
        if elements.is_a?(Hash) || elements.is_a?(String)
          elements = [elements]
        end

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

      def all(locator)
        return EMPTY_ARRAY if raw.nil?

        result = raw[locator]

        if result.is_a? Hash
          [result]
        elsif result.is_a? Array
          result
        else
          EMPTY_ARRAY
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
money_s3-0.5.0 lib/money_s3/parsers/base_parser.rb
money_s3-0.4.0 lib/money_s3/parsers/base_parser.rb