class Crm::Core::ItemEnumerator
ItemEnumerator
provides methods for accessing items identified by their ID. It implements {#each} and includes the {ruby-doc.org/core/Enumerable.html Enumerable} mixin, which provides methods such as #map
, #select
or #take
. @api public
Attributes
Returns the IDs of the items to enumerate. @return [Array<String>] @api public
If the ItemEnumerator
is the result of a search, it returns the total number of search hits. Otherwise, it returns {#length}. @return [Fixnum] @api public
Public Class Methods
# File lib/crm/core/item_enumerator.rb, line 23 def initialize(ids, total: nil) @ids = ids @total = total || ids.length end
Public Instance Methods
Iterates over the {#ids} and fetches the corresponding items on demand. @overload each
Calls the block once for each item, passing this item as a parameter. @yieldparam item [BasicResource] @return [void]
@overload each
If no block is given, an {http://ruby-doc.org/core/Enumerator.html enumerator} is returned instead. @return [Enumerator<BasicResource>]
@raise [Errors::ResourceNotFound] if at least one of the IDs could not be found. @api public
# 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
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+. @return [Fixnum] @api public
# File lib/crm/core/item_enumerator.rb, line 55 def length @ids.length end