Class: Dynamoid::TransactionWrite::Action
- Inherits:
-
Object
- Object
- Dynamoid::TransactionWrite::Action
- Defined in:
- lib/dynamoid/transaction_write/action.rb
Direct Known Subclasses
Constant Summary collapse
- VALID_OPTIONS =
%i[skip_callbacks skip_validation raise_validation_error skip_existence_check].freeze
Instance Attribute Summary collapse
-
#additions ⇒ Object
Returns the value of attribute additions.
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#deletions ⇒ Object
Returns the value of attribute deletions.
-
#model ⇒ Object
Returns the value of attribute model.
-
#options ⇒ Object
Returns the value of attribute options.
-
#removals ⇒ Object
Returns the value of attribute removals.
Instance Method Summary collapse
-
#add(values) ⇒ Object
increments a number or adds to a set, starts at 0 or [] if it doesn’t yet exist.
- #add_timestamps(attributes, skip_created_at: false) ⇒ Object
- #changes_applied ⇒ Object
-
#delete(field_or_values) ⇒ Object
deletes a value or values from a set type or simply sets a field to nil.
- #find_from_attributes(model_or_model_class, attributes) ⇒ Object
-
#hash_key ⇒ Object
returns hash_key from the model if it exists or first element from id.
-
#initialize(model_or_model_class, attributes = {}, options = {}) {|_self| ... } ⇒ Action
constructor
A new instance of Action.
- #model_class ⇒ Object
- #raise_validation_error? ⇒ Boolean
-
#range_key ⇒ Object
returns range_key from the model if it exists or second element from id, or nil.
- #run_callbacks ⇒ Object
-
#sanitize_item(attributes) ⇒ Object
copied from the protected method in AwsSdkV3.
-
#set(values) ⇒ Object
sets a value in the attributes.
- #skip_callbacks? ⇒ Boolean
- #skip_validation? ⇒ Boolean
- #to_h ⇒ Object
- #touch_model_timestamps(skip_created_at: false) ⇒ Object
- #valid? ⇒ Boolean
- #write_attributes_to_model ⇒ Object
Constructor Details
#initialize(model_or_model_class, attributes = {}, options = {}) {|_self| ... } ⇒ Action
Returns a new instance of Action.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dynamoid/transaction_write/action.rb', line 9 def initialize(model_or_model_class, attributes = {}, = {}) if model_or_model_class.is_a?(Dynamoid::Document) self.model = model_or_model_class else @model_class = model_or_model_class end self.attributes = attributes self. = || {} self.additions = {} self.deletions = {} self.removals = [] invalid_keys = self..keys - VALID_OPTIONS raise ArgumentError, "Invalid options found: '#{invalid_keys}'" if invalid_keys.present? yield(self) if block_given? end |
Instance Attribute Details
#additions ⇒ Object
Returns the value of attribute additions.
7 8 9 |
# File 'lib/dynamoid/transaction_write/action.rb', line 7 def additions @additions end |
#attributes ⇒ Object
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/dynamoid/transaction_write/action.rb', line 7 def attributes @attributes end |
#deletions ⇒ Object
Returns the value of attribute deletions.
7 8 9 |
# File 'lib/dynamoid/transaction_write/action.rb', line 7 def deletions @deletions end |
#model ⇒ Object
Returns the value of attribute model.
7 8 9 |
# File 'lib/dynamoid/transaction_write/action.rb', line 7 def model @model end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/dynamoid/transaction_write/action.rb', line 7 def @options end |
#removals ⇒ Object
Returns the value of attribute removals.
7 8 9 |
# File 'lib/dynamoid/transaction_write/action.rb', line 7 def removals @removals end |
Instance Method Details
#add(values) ⇒ Object
increments a number or adds to a set, starts at 0 or [] if it doesn’t yet exist
51 52 53 |
# File 'lib/dynamoid/transaction_write/action.rb', line 51 def add(values) additions.merge!(values) end |
#add_timestamps(attributes, skip_created_at: false) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/dynamoid/transaction_write/action.rb', line 102 def (attributes, skip_created_at: false) return attributes if [:skip_timestamps] || !model_class&. result = attributes.clone = DateTime.now.in_time_zone(Time.zone) result[:created_at] ||= unless skip_created_at result[:updated_at] ||= result end |
#changes_applied ⇒ Object
98 99 100 |
# File 'lib/dynamoid/transaction_write/action.rb', line 98 def changes_applied !!model&.changes_applied end |
#delete(field_or_values) ⇒ Object
deletes a value or values from a set type or simply sets a field to nil
56 57 58 59 60 61 62 63 |
# File 'lib/dynamoid/transaction_write/action.rb', line 56 def delete(field_or_values) if field_or_values.is_a?(Hash) deletions.merge!(field_or_values) else # adds to array of fields for use in REMOVE update expression removals << field_or_values end end |
#find_from_attributes(model_or_model_class, attributes) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dynamoid/transaction_write/action.rb', line 65 def find_from_attributes(model_or_model_class, attributes) model_class = model_or_model_class.is_a?(Dynamoid::Document) ? model_or_model_class.class : model_or_model_class if attributes.is_a?(Hash) raise Dynamoid::Errors::MissingHashKey unless attributes[model_class.hash_key].present? model_class.find(attributes[model_class.hash_key], range_key: model_class.range_key? ? attributes[model_class.range_key] : nil, consistent_read: true) else model_class.find(attributes, consistent_read: true) end end |
#hash_key ⇒ Object
returns hash_key from the model if it exists or first element from id
31 32 33 34 35 |
# File 'lib/dynamoid/transaction_write/action.rb', line 31 def hash_key return model.hash_key if model attributes[model_class.hash_key] end |
#model_class ⇒ Object
26 27 28 |
# File 'lib/dynamoid/transaction_write/action.rb', line 26 def model_class model&.class || @model_class end |
#raise_validation_error? ⇒ Boolean
90 91 92 |
# File 'lib/dynamoid/transaction_write/action.rb', line 90 def raise_validation_error? !![:raise_validation_error] end |
#range_key ⇒ Object
returns range_key from the model if it exists or second element from id, or nil
38 39 40 41 42 43 |
# File 'lib/dynamoid/transaction_write/action.rb', line 38 def range_key return nil unless model_class.range_key? return model.attributes[model_class.range_key] if model attributes[model_class.range_key] end |
#run_callbacks ⇒ Object
94 95 96 |
# File 'lib/dynamoid/transaction_write/action.rb', line 94 def run_callbacks yield if block_given? end |
#sanitize_item(attributes) ⇒ Object
copied from the protected method in AwsSdkV3
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/dynamoid/transaction_write/action.rb', line 127 def sanitize_item(attributes) config_value = Dynamoid.config.store_attribute_with_nil_value store_attribute_with_nil_value = config_value.nil? ? false : !!config_value attributes.reject do |_, v| ((v.is_a?(Set) || v.is_a?(String)) && v.empty?) || (!store_attribute_with_nil_value && v.nil?) end.transform_values do |v| v.is_a?(Hash) ? v.stringify_keys : v end end |
#set(values) ⇒ Object
sets a value in the attributes
46 47 48 |
# File 'lib/dynamoid/transaction_write/action.rb', line 46 def set(values) attributes.merge!(values) end |
#skip_callbacks? ⇒ Boolean
78 79 80 |
# File 'lib/dynamoid/transaction_write/action.rb', line 78 def skip_callbacks? !![:skip_callbacks] end |
#skip_validation? ⇒ Boolean
82 83 84 |
# File 'lib/dynamoid/transaction_write/action.rb', line 82 def skip_validation? !![:skip_validation] end |
#to_h ⇒ Object
139 140 141 |
# File 'lib/dynamoid/transaction_write/action.rb', line 139 def to_h raise 'override me' end |
#touch_model_timestamps(skip_created_at: false) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/dynamoid/transaction_write/action.rb', line 112 def (skip_created_at: false) return if !model || [:skip_timestamps] || !model_class. = DateTime.now.in_time_zone(Time.zone) model.updated_at = model.created_at ||= unless skip_created_at end |
#valid? ⇒ Boolean
86 87 88 |
# File 'lib/dynamoid/transaction_write/action.rb', line 86 def valid? model&.valid? end |
#write_attributes_to_model ⇒ Object
120 121 122 123 124 |
# File 'lib/dynamoid/transaction_write/action.rb', line 120 def write_attributes_to_model return unless model && attributes.present? attributes.each { |attribute, value| model.write_attribute(attribute, value) } end |