Class: Crm::Core::ItemEnumerator

Inherits:
Object
  • Object
show all
Includes:
Mixins::Inspectable, Enumerable
Defined in:
lib/crm/core/item_enumerator.rb

Overview

ItemEnumerator provides methods for accessing items identified by their ID. It implements #each and includes the Enumerable mixin, which provides methods such as #map, #select or #take.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Inspectable

#inspect

Constructor Details

#initialize(ids, total: nil) ⇒ ItemEnumerator

Returns a new instance of ItemEnumerator.



23
24
25
26
# File 'lib/crm/core/item_enumerator.rb', line 23

def initialize(ids, total: nil)
  @ids = ids
  @total = total || ids.length
end

Instance Attribute Details

#idsArray<String> (readonly)

Returns the IDs of the items to enumerate.

Returns:

  • (Array<String>)


15
16
17
# File 'lib/crm/core/item_enumerator.rb', line 15

def ids
  @ids
end

#totalFixnum (readonly)

If the ItemEnumerator is the result of a search, it returns the total number of search hits. Otherwise, it returns #length.

Returns:

  • (Fixnum)


21
22
23
# File 'lib/crm/core/item_enumerator.rb', line 21

def total
  @total
end

Instance Method Details

#each {|item| ... } ⇒ void #eachEnumerator<BasicResource>

Iterates over the #ids and fetches the corresponding items on demand.

Overloads:

  • #each {|item| ... } ⇒ void

    This method returns an undefined value.

    Calls the block once for each item, passing this item as a parameter.

    Yield Parameters:

  • #eachEnumerator<BasicResource>

    If no block is given, an enumerator is returned instead.

    Returns:

Raises:



39
40
41
42
43
44
45
46
47
48
# File 'lib/crm/core/item_enumerator.rb', line 39

def each(&block)
  return enum_for(:each) unless block_given?

  server_limit = 100
  @ids.each_slice(server_limit) do |sliced_ids|
    RestApi.instance.get('mget', {'ids' => sliced_ids}).map do |item|
      block.call "Crm::#{item['base_type']}".constantize.new(item)
    end
  end
end

#lengthFixnum Also known as: size

Returns the number of items. Prefer this method over Enumerable#count because #length doesn’t fetch the items and therefore is faster than Enumerable#count.

Returns:

  • (Fixnum)


55
56
57
# File 'lib/crm/core/item_enumerator.rb', line 55

def length
  @ids.length
end