Sha256: f22c140e376f869150a6438fa502177778b1168dda40d252e9d66746e0402c77

Contents?: true

Size: 872 Bytes

Versions: 1

Compression:

Stored size: 872 Bytes

Contents

module MoneyS3
  module BaseElement
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
money_s3-0.3.0 lib/money_s3/base_element.rb