Class: Rod::Rest::CollectionProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rod/rest/collection_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy, association_name, size, client) ⇒ CollectionProxy

Initializes a CollectionPorxy.

  • :proxy - the object this collection belongs to

  • :association_name - the name of proxie’s plural association this collection is returned for

  • :size - the size of the collection

  • :client - the REST API client



14
15
16
17
18
19
# File 'lib/rod/rest/collection_proxy.rb', line 14

def initialize(proxy,association_name,size,client)
  @proxy = proxy
  @association_name = association_name
  @size = size
  @client = client
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



7
8
9
# File 'lib/rod/rest/collection_proxy.rb', line 7

def size
  @size
end

Instance Method Details

#[](index) ⇒ Object

Returns the index-th element of the collection.



27
28
29
30
31
32
33
# File 'lib/rod/rest/collection_proxy.rb', line 27

def [](index)
  begin
    @client.fetch_related_object(@proxy,@association_name,index)
  rescue MissingResource
    nil
  end
end

#eachObject

Iterates over the elements of the collection.



41
42
43
44
45
46
47
48
49
# File 'lib/rod/rest/collection_proxy.rb', line 41

def each
  if block_given?
    @size.times do |index|
      yield self[index]
    end
  else
    enum_for(:each)
  end
end

#empty?Boolean

Returns true if the collection is empty (i.e. its size == 0).

Returns:

  • (Boolean)


22
23
24
# File 'lib/rod/rest/collection_proxy.rb', line 22

def empty?
  self.size == 0
end

#lastObject

Returns the last element of the collection.



36
37
38
# File 'lib/rod/rest/collection_proxy.rb', line 36

def last
  size > 0 ? self[size - 1] : nil
end