Module: Hoodoo::Client::PaginatedEnumeration

Included in:
AugmentedArray
Defined in:
lib/hoodoo/client/paginated_enumeration.rb

Overview

Ruby mixin providing an enumeration mechanism, allowing the caller to iterate over all the resource instances in the list, automatically performing the necessary pagination behind the scenes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#next_page_procObject

Proc called by enumerate_all to provide the next ‘page’ of values to be enumerated through. Returns an Hoodoo::Client::AugmentedArray.



23
24
25
# File 'lib/hoodoo/client/paginated_enumeration.rb', line 23

def next_page_proc
  @next_page_proc
end

Instance Method Details

#enumerate_allObject

Yields each resource instance, automatically paginating through the entire set of resources.

Provide a block to process each resource instance. For example:

results = members.list(:search => { :surname => 'Smith' } ).enumerate_all do | member |
  if member.platform_errors.has_errors?
    .. deal with error ...
    break
  else
    .. process member ...
  end
end

Each iteration yields a Hoodoo::Client::AugmentedHash representation of the requested resource instance. The caller must check for errors on the value yielded with each iteration, as per the example above.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hoodoo/client/paginated_enumeration.rb', line 43

def enumerate_all

  raise "Must provide a block to enumerate_all" unless block_given?

  # The first set of results is in 'this' AugmentedArray
  results = self

  loop do

    if results.size > 0

      if results.platform_errors.has_errors?
        raise 'Hoodoo::Client::PaginatedEnumeration#enumerate_all: Unexpected internal state combination of results set and results error indication'
      end

      # Yield a resource at a time to the caller
      #
      # Note: An inter-resource call in a single service returns each
      #       resource as a Hash, which must be converted to AugmentedHash
      results.each do | result |
        yield to_augmented_hash(result)
      end
      results = next_page_proc.call()
    else
      # Return errors in an (empty) AugmentedHash
      if results.platform_errors.has_errors?
        yield copy_hash_errors_and_options( Hoodoo::Client::AugmentedHash.new, results )
      end
      break
    end

  end

end