Class: Commutator::Collection
- Inherits:
-
Object
- Object
- Commutator::Collection
- Defined in:
- lib/commutator/collection.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.
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
15 16 17 18 19 |
# File 'lib/commutator/collection.rb', line 15 def initialize(response, klass, modifiers: []) @response = response @klass = klass @modifiers = modifiers.map(&:expand_proc_modifiers).freeze end |
Instance Method Details
#all_items ⇒ Object
55 56 57 |
# File 'lib/commutator/collection.rb', line 55 def all_items each_item.each_with_object([]) { |item, arr| arr << item } end |
#each_item ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/commutator/collection.rb', line 47 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
37 38 39 40 41 42 43 44 45 |
# File 'lib/commutator/collection.rb', line 37 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
21 22 23 24 25 26 27 28 |
# File 'lib/commutator/collection.rb', line 21 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
59 60 61 62 63 64 65 |
# File 'lib/commutator/collection.rb', line 59 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.
33 34 35 |
# File 'lib/commutator/collection.rb', line 33 def next_page self.class.new(response.next_page, klass) end |