Class: Vng::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vng/relation.rb

Overview

Provides methods to iterate through collections of Vonigo resources.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|Hash| ... } ⇒ Relation

Returns a new instance of Relation.

Yields:

  • (Hash)

    the options to change which items to iterate through.



8
9
10
11
12
# File 'lib/vng/relation.rb', line 8

def initialize(options = {}, &item_block)
  @options = {parts: ['objectID', 'isActive'], next_page: 1}
  @options.merge! options
  @item_block = item_block
end

Instance Method Details

#eachObject



36
37
38
39
40
41
# File 'lib/vng/relation.rb', line 36

def each
  @last_index = 0
  while next_item = find_next
    yield next_item
  end
end

#select(*parts) ⇒ Vng::Relation

Specifies which parts of the resource to fetch when hitting the data API.

Parameters:

  • parts (Array<Symbol>)

    The parts to fetch.

Returns:



28
29
30
31
32
33
34
# File 'lib/vng/relation.rb', line 28

def select(*parts)
  if @options[:parts] != parts + ['objectID', 'isActive']
    @items = nil
    @options.merge! parts: (parts + ['objectID', 'isActive'])
  end
  self
end

#where(conditions = {}) ⇒ Vng::Relation

Specifies which items to fetch when hitting the data API.

Parameters:

  • conditions (Hash<Symbol, String>) (defaults to: {})

    The conditions for the items.

Returns:



17
18
19
20
21
22
23
# File 'lib/vng/relation.rb', line 17

def where(conditions = {})
  if @options[:conditions] != conditions
    @items = []
    @options.merge! conditions: conditions
  end
  self
end