Class: DynamodbRecord::Collection
- Inherits:
-
Object
- Object
- DynamodbRecord::Collection
- Includes:
- Enumerable
- Defined in:
- lib/dynamodb_record/collection.rb
Instance Method Summary collapse
- #create!(items = {}) ⇒ Object
- #each ⇒ Object
-
#initialize(pager, klass, options = {}) ⇒ Collection
constructor
attr_reader :last_evaluated_key.
- #last_evaluated_key ⇒ Object
- #next_page? ⇒ Boolean
Constructor Details
#initialize(pager, klass, options = {}) ⇒ Collection
attr_reader :last_evaluated_key
9 10 11 12 13 14 15 |
# File 'lib/dynamodb_record/collection.rb', line 9 def initialize(pager, klass, = {}) @klass = klass @table_name = [:table_name] @foreign_key = [:expression_attribute_values].transform_keys { |k| k.delete_prefix(':').to_sym } rescue nil @items = pager.items.map { |item| klass.send(:from_database, item) } @last_evaluated_key = pager.last_evaluated_key end |
Instance Method Details
#create!(items = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dynamodb_record/collection.rb', line 34 def create!(items = {}) items.merge!(@foreign_key) object = @klass.create!(items) unless @klass.to_s.pluralize.downcase == @table_name.to_s client = @klass.client @foreign_key.merge!({ "#{@klass.to_s.downcase}_id": object.id, created_at: Time.now.to_i }) client.put_item({ table_name: @table_name, item: @foreign_key }) end @items << object object end |
#each ⇒ Object
26 27 28 |
# File 'lib/dynamodb_record/collection.rb', line 26 def each(&) @items.each(&) end |
#last_evaluated_key ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/dynamodb_record/collection.rb', line 17 def last_evaluated_key if @last_evaluated_key.nil? nil else json_string = JSON.dump(@last_evaluated_key) Base64.urlsafe_encode64(json_string) end end |
#next_page? ⇒ Boolean
30 31 32 |
# File 'lib/dynamodb_record/collection.rb', line 30 def next_page? last_evaluated_key ? true : false end |