Class: ElasticWeb::ResourceCollection

Inherits:
Resource
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticweb/resource_collection.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#data, #message, #refs, #status

Instance Method Summary collapse

Methods inherited from Resource

#method_missing, #parse_references

Constructor Details

#initialize(original_data, options = {}) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/elasticweb/resource_collection.rb', line 7

def initialize(original_data, options = {})
  original_data.merge!({ data: [] }) unless original_data.dig(:data).is_a?(Array)

  data = original_data.dig(:data)
  @collection = Array(data).map do |resource|
    Resource.new(data: resource)
  end

  if (pager = original_data.delete(:pager))
    @pager = OpenStruct.new({
        total: pager[:total],
        per_page: pager[:per_page],
        current_page: pager[:current_page],
        next_page_url: pager[:next_page_url],
        prev_page_url: pager[:prev_page_url]
    })
  else
    @pager = nil
  end

  references = original_data.delete(:references) { Hash.new }
  @refs = references.present? ? OpenStruct.new(parse_references(references)) : nil
  @options = options.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ElasticWeb::Resource

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



5
6
7
# File 'lib/elasticweb/resource_collection.rb', line 5

def collection
  @collection
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/elasticweb/resource_collection.rb', line 5

def options
  @options
end

#pagerObject (readonly)

Returns the value of attribute pager.



5
6
7
# File 'lib/elasticweb/resource_collection.rb', line 5

def pager
  @pager
end

Instance Method Details

#eachObject



32
33
34
35
36
# File 'lib/elasticweb/resource_collection.rb', line 32

def each
  return enum_for(:each) unless block_given?

  @collection.each { |item| yield item }
end