Class: DynamodbRecord::HasAndBelongsToManyCollection
- Inherits:
-
Object
- Object
- DynamodbRecord::HasAndBelongsToManyCollection
- Includes:
- Enumerable
- Defined in:
- lib/dynamodb_record/has_and_belongs_to_many_collection.rb
Overview
Dynamodb::ManyToManyCollection is a class that represent a ManyToManyCollection
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #create!(params = {}) ⇒ Object
- #each ⇒ Object
-
#initialize(pager, base_object) ⇒ HasAndBelongsToManyCollection
constructor
A new instance of HasAndBelongsToManyCollection.
- #last_evaluated_key ⇒ Object
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #page(last_key) ⇒ Object
Constructor Details
#initialize(pager, base_object) ⇒ HasAndBelongsToManyCollection
Returns a new instance of HasAndBelongsToManyCollection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 8 def initialize(pager, base_object) @base_object = base_object @pager = pager @klass = @pager.klass @options = @pager. @items = [] @pager.items.each do |item| # p item object = @klass.client.get_item( table_name: @klass.table_name, key: {id: item["#{@klass.to_s.downcase}_id"]} ) @items << @klass.send(:from_database, object.item) end end |
Instance Method Details
#<<(object) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 44 def <<(object) table_name = @options[:table_name] item = @options[:expression_attribute_values].transform_keys { |k| k.delete_prefix(':').to_sym } item[:"#{@klass.to_s.downcase}_id"] = object.id key = {table_name:, item:} @klass.client.put_item(key) @items << object end |
#create!(params = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 53 def create!(params = {}) raise "#{@base_object.class} must be saved" if @base_object.new_record object = @klass.send(:from_database, params) object.save! table_name = @options[:table_name] item = @options[:expression_attribute_values].transform_keys { |k| k.delete_prefix(':').to_sym } item[:"#{@klass.to_s.downcase}_id"] = object.id key = {table_name:, item:} @klass.client.put_item(key) object end |
#each ⇒ Object
24 25 26 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 24 def each(&) @items.each(&) end |
#last_evaluated_key ⇒ Object
28 29 30 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 28 def last_evaluated_key @pager.last_evaluated_key end |
#next_page ⇒ Object
36 37 38 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 36 def next_page self.class.new(@pager.next_page, @base_object) end |
#next_page? ⇒ Boolean
32 33 34 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 32 def next_page? @pager ? @pager.next_page? : false end |
#page(last_key) ⇒ Object
40 41 42 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 40 def page(last_key) self.class.new(@pager.next_page(last_key), @base_object) if last_key end |