Class: RIQ::BatchManager

Inherits:
Object
  • Object
show all
Defined in:
lib/riq/batch_manager.rb

Overview

Manages caching and fetching for a certain type of child object.

Direct Known Subclasses

ListItemManager

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, opts = {}) ⇒ BatchManager

Returns a new instance of BatchManager.

Parameters:

  • klass (RIQObject)

    The child class that’s being fetched, such as Account or List

  • opts (Hash) (defaults to: {})

    fetch options

Raises:



11
12
13
14
15
16
17
18
19
20
# File 'lib/riq/batch_manager.rb', line 11

def initialize(klass, opts = {})
  @klass = klass
  raise(RIQError, 'Must pass a RIQ Class') unless @klass.ancestors.include?(RIQ::RIQObject)
  @fetch_options = {}
  reset_cache
  
  # cause otherwise it's a variable
  self.send(:fetch_options=, opts)
  @client = RIQ.client
end

Instance Attribute Details

#fetch_optionsHash

Returns current fetch options.

Returns:

  • (Hash)

    current fetch options



7
8
9
# File 'lib/riq/batch_manager.rb', line 7

def fetch_options
  @fetch_options
end

Instance Method Details

#each(&blok) ⇒ Object

Iterator for each item in the manager. Pass a block to it!

Examples:

RIQ.lists.each do |l|
  puts l
end


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/riq/batch_manager.rb', line 27

def each(&blok)
  reset_cache
  loop do
    x = next_item
    if x
      blok.call(x)
    else
      return
    end
  end
end

#firstRIQObject

Returns the first child object, mainly used for testing

Returns:



41
42
43
44
45
46
47
48
# File 'lib/riq/batch_manager.rb', line 41

def first
  reset_cache
  o = fetch_options.dup
  self.send(:fetch_options=, {_limit: 1})
  r = fetch_page.first
  self.send(:fetch_options=, o)
  r
end