Class: RealPage::Utils::ArrayFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/real_page/utils/array_fetcher.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(hash:, key:, model: nil) ⇒ ArrayFetcher

Returns a new instance of ArrayFetcher.



9
10
11
12
13
# File 'lib/real_page/utils/array_fetcher.rb', line 9

def initialize(hash:, key:, model: nil)
  @hash  = hash
  @key   = key
  @model = model
end

Instance Method Details

#fetchArray

Returns take the MultiXml hash and return a proper array.

Returns:

  • (Array)

    take the MultiXml hash and return a proper array



16
17
18
19
20
21
22
# File 'lib/real_page/utils/array_fetcher.rb', line 16

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