Sha256: c47893e9263812f060618653e0aaccf330c9fb6f31738b8315879b55febfcc51

Contents?: true

Size: 886 Bytes

Versions: 7

Compression:

Stored size: 886 Bytes

Contents

require 'real_page/utils'

module RealPage
  module Utils
    # Fetch an array from a hash that was generated by parsing XML using
    # MultiXml. If you provide a model to instantiate, an instance will be
    # initialized with each data element.
    class ArrayFetcher
      def initialize(hash:, key:, model: nil)
        @hash  = hash
        @key   = key
        @model = model
      end

      # @return [Array] take the MultiXml hash and return a proper array
      def fetch
        return [] if empty?
        value = hash[key]
        value = [value] unless value.is_a?(Array)
        return value unless model
        value.map { |v| model.new(v) }
      end

      private

      attr_reader :hash, :key, :model

      def empty?
        return true if hash.nil?
        return true if hash.is_a?(String) && hash.strip! == ''
        hash.empty?
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
real_page-2.3.6 lib/real_page/utils/array_fetcher.rb
real_page-2.3.5 lib/real_page/utils/array_fetcher.rb
real_page-2.3.4 lib/real_page/utils/array_fetcher.rb
real_page-2.3.3 lib/real_page/utils/array_fetcher.rb
real_page-2.3.2 lib/real_page/utils/array_fetcher.rb
real_page-2.3.1 lib/real_page/utils/array_fetcher.rb
real_page-2.3.0 lib/real_page/utils/array_fetcher.rb