Class: Promiscuous::Subscriber::Operation
- Inherits:
-
Object
- Object
- Promiscuous::Subscriber::Operation
- Defined in:
- lib/promiscuous/subscriber/operation.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#dependency ⇒ Object
Returns the value of attribute dependency.
-
#id ⇒ Object
Returns the value of attribute id.
-
#model ⇒ Object
Returns the value of attribute model.
-
#operation ⇒ Object
Returns the value of attribute operation.
Instance Method Summary collapse
- #create(options = {}) ⇒ Object
- #destroy ⇒ Object
- #execute ⇒ Object
- #get_subscribed_model(payload) ⇒ Object
-
#initialize(payload) ⇒ Operation
constructor
A new instance of Operation.
- #unit_of_work ⇒ Object
- #update(should_create_on_failure = true) ⇒ Object
- #warn(msg) ⇒ Object
Constructor Details
#initialize(payload) ⇒ Operation
Returns a new instance of Operation.
5 6 7 8 9 10 11 12 13 |
# File 'lib/promiscuous/subscriber/operation.rb', line 5 def initialize(payload) if payload.is_a?(Hash) self.id = payload['id'] self.operation = payload['operation'].try(:to_sym) self.attributes = payload['attributes'] self.dependency = payload['dependency'] self.model = self.get_subscribed_model(payload) if payload['types'] end end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
2 3 4 |
# File 'lib/promiscuous/subscriber/operation.rb', line 2 def attributes @attributes end |
#dependency ⇒ Object
Returns the value of attribute dependency.
2 3 4 |
# File 'lib/promiscuous/subscriber/operation.rb', line 2 def dependency @dependency end |
#id ⇒ Object
Returns the value of attribute id.
2 3 4 |
# File 'lib/promiscuous/subscriber/operation.rb', line 2 def id @id end |
#model ⇒ Object
Returns the value of attribute model.
2 3 4 |
# File 'lib/promiscuous/subscriber/operation.rb', line 2 def model @model end |
#operation ⇒ Object
Returns the value of attribute operation.
2 3 4 |
# File 'lib/promiscuous/subscriber/operation.rb', line 2 def operation @operation end |
Instance Method Details
#create(options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/promiscuous/subscriber/operation.rb', line 30 def create(={}) model.__promiscuous_fetch_new(id).tap do |instance| instance.__promiscuous_eventual_consistency_update(self) instance.__promiscuous_update(self) instance.save! end rescue Exception => e if model.__promiscuous_duplicate_key_exception?(e) [:on_already_created] ||= proc { warn "ignoring already created record" } [:on_already_created].call else raise e end end |
#destroy ⇒ Object
57 58 59 60 |
# File 'lib/promiscuous/subscriber/operation.rb', line 57 def destroy Promiscuous::Subscriber::Worker::EventualDestroyer.postpone_destroy(model, id) if .dependencies.present? model.__promiscuous_fetch_existing(id).destroy end |
#execute ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/promiscuous/subscriber/operation.rb', line 62 def execute case operation when :create then create when :update then update when :destroy then destroy end end |
#get_subscribed_model(payload) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/promiscuous/subscriber/operation.rb', line 15 def get_subscribed_model(payload) [.app, '*'].each do |app| app_mapping = Promiscuous::Subscriber::Model.mapping[app] || {} payload['types'].to_a.each do |ancestor| model = app_mapping[ancestor] return model if model end end nil end |
#unit_of_work ⇒ Object
70 71 72 |
# File 'lib/promiscuous/subscriber/operation.rb', line 70 def unit_of_work @unit_of_work ||= Promiscuous::Subscriber::UnitOfWork.current end |
#update(should_create_on_failure = true) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/promiscuous/subscriber/operation.rb', line 45 def update(should_create_on_failure=true) model.__promiscuous_fetch_existing(id).tap do |instance| if instance.__promiscuous_eventual_consistency_update(self) instance.__promiscuous_update(self) instance.save! end end rescue model.__promiscuous_missing_record_exception warn "upserting" create :on_already_created => proc { update(false) if should_create_on_failure } end |
#warn(msg) ⇒ Object
26 27 28 |
# File 'lib/promiscuous/subscriber/operation.rb', line 26 def warn(msg) Promiscuous.warn "[receive] #{msg} #{.payload}" end |