Class: RemoteResource::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/remote_resource/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, resources_collection, response_hash) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
13
14
15
# File 'lib/remote_resource/collection.rb', line 9

def initialize(resource_klass, resources_collection, response_hash)
  @resource_klass       = resource_klass
  @resources_collection = resources_collection
  @response_hash        = response_hash
  @meta                 = response_hash[:meta] || {}
  @_response            = response_hash[:_response]
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



7
8
9
# File 'lib/remote_resource/collection.rb', line 7

def _response
  @_response
end

#metaObject (readonly)

Returns the value of attribute meta.



7
8
9
# File 'lib/remote_resource/collection.rb', line 7

def meta
  @meta
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



7
8
9
# File 'lib/remote_resource/collection.rb', line 7

def resource_klass
  @resource_klass
end

#resources_collectionObject (readonly)

Returns the value of attribute resources_collection.



7
8
9
# File 'lib/remote_resource/collection.rb', line 7

def resources_collection
  @resources_collection
end

Instance Method Details

#each(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/remote_resource/collection.rb', line 17

def each(&block)
  if resources_collection.is_a? Array
    if defined?(@collection)
      @collection.each(&block)
    else
      @collection = []
      resources_collection.each do |element|
        record = resource_klass.new element.merge(@response_hash)
        @collection << record
        yield(record)
      end
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/remote_resource/collection.rb', line 32

def empty?
  resources_collection.blank?
end

#success?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/remote_resource/collection.rb', line 36

def success?
  _response.success?
end