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
- #<<(pluralizable_object) ⇒ Object
- #create!(params = {}) ⇒ Object
- #destroy(object) ⇒ 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 = [] @base_model = @klass.to_s.downcase.split('::').last @pager.items.each do |item| object = @klass.client.get_item( table_name: @klass.table_name, key: {id: item["#{@base_model}_id"]} ) @items << @klass.send(:from_database, object.item) end end |
Instance Method Details
#<<(pluralizable_object) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 44 def <<(pluralizable_object) pluralizable_object = [pluralizable_object] unless pluralizable_object.is_a?(Array) pluralizable_object.each do |object| table_name = @options[:table_name] item = @options[:expression_attribute_values].transform_keys { |k| k.delete_prefix(':').to_sym } item[:"#{@base_model}_id"] = object.id item[:created_at] = DateTime.now.to_s key = {table_name:, item:} res = @items.none? { |data| data.id == object.id } if res @klass.client.put_item(key) @items << object end end @items end |
#create!(params = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 74 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 item[:created_at] = DateTime.now.to_s key = {table_name:, item:} @klass.client.put_item(key) object end |
#destroy(object) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/dynamodb_record/has_and_belongs_to_many_collection.rb', line 64 def destroy(object) table_name = @options[:table_name] item = @options[:expression_attribute_values].transform_keys { |k| k.delete_prefix(':').to_sym } item[:"#{@base_model}_id"] = object.id key = {table_name:, key: item} @klass.client.delete_item(key) @items.delete_if { |data| data.id == object.id } 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 |