Module: PubSubModelSync::PublisherConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/pub_sub_model_sync/publisher_concern.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#ps_after_publish(_action, _payload) ⇒ Object Also known as: ps_after_sync

after delivering data



17
# File 'lib/pub_sub_model_sync/publisher_concern.rb', line 17

def ps_after_publish(_action, _payload); end

#ps_before_publish(_action, _payload) ⇒ Object Also known as: ps_before_sync

before delivering data (return :cancel to cancel sync)



13
# File 'lib/pub_sub_model_sync/publisher_concern.rb', line 13

def ps_before_publish(_action, _payload); end

#ps_perform_publish(action = :create, parents_actions: false) ⇒ Object

Permits to perform manually the callback for a specific action

Parameters:

  • action (Symbol, default: :create) (defaults to: :create)

    Only :create|:update|:destroy



41
42
43
44
45
46
47
# File 'lib/pub_sub_model_sync/publisher_concern.rb', line 41

def ps_perform_publish(action = :create, parents_actions: false)
  callbacks = self.class.ps_cache_publish_callbacks
  callbacks = self.class.ancestors.map { |p| p.try(:ps_cache_publish_callbacks) }.compact.flatten if parents_actions
  items = callbacks.select { |item| item[:actions].include?(action) }
  items.each { |item| instance_exec(action, &item[:callback]) }
  self
end

#ps_publish(action, data: {}, mapping: [], headers: {}, as_klass: self.class.name) ⇒ Object

Delivers a notification via pubsub

Parameters:

  • action (Symbol, String)

    Sample: create|update|save|destroy|<any_other_key>

  • mapping? (Array<String>)

    If present will generate data using the mapping and added to the payload. Sample: [“id”, “full_name:name”]

  • data? (Hash, Symbol, Proc)

    Hash: Data to be added to the payload Symbol: Method name to be called to retrieve payload data (must return a hash value, receives :action name) Proc: Block to be called to retrieve payload data

  • headers? (Hash, Symbol, Proc)

    : (All available attributes in @Payload.headers) Hash: Data that will be merged with default header values Symbol: Method name that will be called to retrieve header values (must return a hash, receives :action name) Proc: Block to be called to retrieve header values

  • as_klass? (String)

    : Output class name used instead of current class name



33
34
35
36
# File 'lib/pub_sub_model_sync/publisher_concern.rb', line 33

def ps_publish(action, data: {}, mapping: [], headers: {}, as_klass: self.class.name)
  p_klass = PubSubModelSync::MessagePublisher
  p_klass.publish_model(self, action, data: data, mapping: mapping, headers: headers, as_klass: as_klass)
end