Module: TableSync::EventActions
- Included in:
- ConfigDecorator
- Defined in:
- lib/table_sync/event_actions.rb
Defined Under Namespace
Modules: DataWrapper
Instance Method Summary collapse
- #destroy(data) ⇒ Object
- #expected_update_result?(query_results) ⇒ Boolean
- #prevent_incomplete_event!(attributes) ⇒ Object
- #process_destroy(attributes, target_attributes) ⇒ Object
-
#process_upsert(data) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #update(data) ⇒ Object
- #with_wrapping(data = {}, &block) ⇒ Object
Instance Method Details
#destroy(data) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/table_sync/event_actions.rb', line 17 def destroy(data) attributes = data.first || {} target_attributes = attributes.select { |key, _value| target_keys.include?(key) } prevent_incomplete_event!(target_attributes) with_wrapping(DataWrapper::Destroy.new(attributes)) do process_destroy(attributes, target_attributes) end end |
#expected_update_result?(query_results) ⇒ Boolean
86 87 88 |
# File 'lib/table_sync/event_actions.rb', line 86 def expected_update_result?(query_results) query_results.uniq { |d| d.slice(*target_keys) }.size == query_results.size end |
#prevent_incomplete_event!(attributes) ⇒ Object
90 91 92 93 94 |
# File 'lib/table_sync/event_actions.rb', line 90 def prevent_incomplete_event!(attributes) unless target_keys.all?(&attributes.keys.method(:include?)) raise TableSync::UnprovidedEventTargetKeysError.new(target_keys, attributes) end end |
#process_destroy(attributes, target_attributes) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/table_sync/event_actions.rb', line 56 def process_destroy(attributes, target_attributes) model.transaction do @config.callback_registry.get_callbacks(kind: :before_commit, event: :destroy).each do |cb| cb[attributes] end if on_destroy results = on_destroy.call(attributes: attributes, target_keys: target_keys) else results = model.destroy(target_attributes) return if results.empty? raise TableSync::DestroyError.new(target_attributes) if results.size != 1 end @config.model.after_commit do @config.callback_registry.get_callbacks(kind: :after_commit, event: :destroy).each do |cb| cb[results] end end end end |
#process_upsert(data) ⇒ Object
rubocop:disable Metrics/MethodLength
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/table_sync/event_actions.rb', line 27 def process_upsert(data) # rubocop:disable Metrics/MethodLength model.transaction do args = { data: data, target_keys: target_keys, version_key: version_key, first_sync_time_key: first_sync_time_key, default_values: default_values, } @config.callback_registry.get_callbacks(kind: :before_commit, event: :update).each do |cb| cb[data.values.flatten] end results = data.reduce([]) do |upserts, (part_model, part_data)| upserts + part_model.upsert(**args, data: part_data) end return if results.empty? raise TableSync::UpsertError.new(**args) unless expected_update_result?(results) @config.model.after_commit do @config.callback_registry.get_callbacks(kind: :after_commit, event: :update).each do |cb| cb[results] end end end end |
#update(data) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/table_sync/event_actions.rb', line 5 def update(data) data.each_value do |attribute_set| attribute_set.each do |attributes| prevent_incomplete_event!(attributes) end end with_wrapping(DataWrapper::Update.new(data)) do process_upsert(data) end end |
#with_wrapping(data = {}, &block) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/table_sync/event_actions.rb', line 78 def with_wrapping(data = {}, &block) if @config.wrap_receiving @config.wrap_receiving.call(data, block) else yield end end |