class Keycloak::Utils::RepresentationIterator

RepresentationIterator provides ability to enable lazy loading if necessary.

NOTE: It's worth noting that `to_a` may be costly if you have a large dataset of users, which could cause out-of-memory, but using `each` instead of `to_a` could save you if you really want iterate all users.

Constants

DEFAULT_PER_PAGE

Attributes

cursor[R]
first[RW]
per_page[RW]
till[RW]

Public Class Methods

new(client, params, per_page: DEFAULT_PER_PAGE, till: nil, &block) click to toggle source
# File lib/keycloak/utils/representation_iterator.rb, line 16
def initialize(client, params, per_page: DEFAULT_PER_PAGE, till: nil, &block)
  @client = client
  @params = params
  @params[:first] ||= 0
  @first = @params[:first]
  @till = @params[:max] || till
  @per_page = per_page
  @block = block

  @data = []
  @cursor = 0
end

Public Instance Methods

each() { |data| ... } click to toggle source
# File lib/keycloak/utils/representation_iterator.rb, line 35
def each
  while true
    return if at_end?
    _fetch_next if @data.empty? || @cursor >= @data.size
    return if @data.empty?

    yield @data[@cursor]
    @cursor += 1
  end
end
first=(num) click to toggle source
# File lib/keycloak/utils/representation_iterator.rb, line 29
def first=(num)
  @first = num
  @cursor = 0
  @data = []
end

Private Instance Methods

_fetch_next() click to toggle source
# File lib/keycloak/utils/representation_iterator.rb, line 48
def _fetch_next
  @params[:first] = @first
  @params[:max] = @per_page
  @data = @client.instance_eval(&@block)

  @first += @per_page
  @cursor = 0
end
at_end?() click to toggle source
# File lib/keycloak/utils/representation_iterator.rb, line 57
def at_end?
  @till && @till <= @params[:first] + @cursor
end