Class: Commutator::Collection
- Inherits:
-
Object
- Object
- Commutator::Collection
- Defined in:
- lib/commutator/collection.rb,
lib/commutator/collection/cached_lookup.rb
Overview
Wraps a DynamoDB response from query or scan operations to provide a collection of instances of the given class.
NOTE: This can’t use SimpleDelegator because ‘Seahorse::Client::Response` does
not implement `#respond_to?` as needed.
Defined Under Namespace
Modules: CachedLookup
Instance Method Summary collapse
- #all_items ⇒ Object
- #each_item ⇒ Object
- #each_page ⇒ Object
-
#initialize(response, klass, modifiers: []) ⇒ Collection
constructor
A new instance of Collection.
- #items ⇒ Object
- #modify_with(*modifiers, factory: false) ⇒ Object
-
#next_page ⇒ Object
Caution! This advances a page pointer in the response and there’s no going back.
Constructor Details
#initialize(response, klass, modifiers: []) ⇒ Collection
Returns a new instance of Collection.
17 18 19 20 21 |
# File 'lib/commutator/collection.rb', line 17 def initialize(response, klass, modifiers: []) @response = response @klass = klass @modifiers = modifiers.map(&:expand_proc_modifiers).freeze end |
Instance Method Details
#all_items ⇒ Object
57 58 59 |
# File 'lib/commutator/collection.rb', line 57 def all_items each_item.each_with_object([]) { |item, arr| arr << item } end |
#each_item ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/commutator/collection.rb', line 49 def each_item return enum_for(:each_item) unless block_given? each_page do |page| page.items.each { |item| yield(item) } end end |
#each_page ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/commutator/collection.rb', line 39 def each_page return enum_for(:each_page) unless block_given? response.each do |page| next unless page.items.present? page = self.class.new(page, klass, modifiers: @modifiers) yield(page) end end |
#items ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/commutator/collection.rb', line 23 def items return [] if response.items.nil? response.items.map do |item| item = klass.new(item) modify_item(item) end end |
#modify_with(*modifiers, factory: false) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/commutator/collection.rb', line 61 def modify_with(*modifiers, factory: false) modifier = ItemModifiers.new(modifiers, factory: factory) # preserves decorator ordering with minimal object creation new_modifiers = [modifier].unshift(*@modifiers) self.class.new(response, klass, modifiers: new_modifiers) end |
#next_page ⇒ Object
Caution! This advances a page pointer in the response and there’s no going back. It will affect how each_page and each_item behaves. In most cases it’s better to use one of the each methods.
35 36 37 |
# File 'lib/commutator/collection.rb', line 35 def next_page self.class.new(response.next_page, klass) end |