Class: Crm::Core::ItemEnumerator
- Inherits:
-
Object
- Object
- Crm::Core::ItemEnumerator
- 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
-
#ids ⇒ Array<String>
readonly
Returns the IDs of the items to enumerate.
-
#total ⇒ Fixnum
readonly
If the ItemEnumerator is the result of a search, it returns the total number of search hits.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates over the #ids and fetches the corresponding items on demand.
-
#initialize(ids, total: nil) ⇒ ItemEnumerator
constructor
A new instance of ItemEnumerator.
-
#length ⇒ Fixnum
(also: #size)
Returns the number of items.
Methods included from Mixins::Inspectable
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
#ids ⇒ Array<String> (readonly)
Returns the IDs of the items to enumerate.
15 16 17 |
# File 'lib/crm/core/item_enumerator.rb', line 15 def ids @ids end |
#total ⇒ Fixnum (readonly)
If the ItemEnumerator is the result of a search, it returns the total number of search hits. Otherwise, it returns #length.
21 22 23 |
# File 'lib/crm/core/item_enumerator.rb', line 21 def total @total end |
Instance Method Details
#each {|item| ... } ⇒ void #each ⇒ Enumerator<BasicResource>
Iterates over the #ids and fetches the corresponding items on demand.
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 |
#length ⇒ Fixnum 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.
55 56 57 |
# File 'lib/crm/core/item_enumerator.rb', line 55 def length @ids.length end |