Module: Promiscuous::Subscriber::Model::Base

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord, Mongoid, Mongoid::EmbeddedDocs, Observer
Defined in:
lib/promiscuous/subscriber/model/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__promiscuous_eventual_consistency_update(operation) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/promiscuous/subscriber/model/base.rb', line 4

def __promiscuous_eventual_consistency_update(operation)
  return true unless operation.dependency.try(&:version)

  version = operation.dependency.version
  generation = operation.message.generation
  version = (generation << 50) | version

  if self.attributes[Promiscuous::Config.version_field].to_i <= version
    self.send("#{Promiscuous::Config.version_field}=", version)
    true
  else
    Promiscuous.debug "[receive] out of order message #{self.class}/#{id}/g:#{generation},v:#{version}"
    false
  end
end

#__promiscuous_update(payload, options = {}) ⇒ Object



20
21
22
23
24
25
26
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
# File 'lib/promiscuous/subscriber/model/base.rb', line 20

def __promiscuous_update(payload, options={})
  self.class.subscribed_attrs.map(&:to_s).each do |attr|
    unless payload.attributes.has_key?(attr)
      "Attribute '#{attr}' is missing from the payload".tap do |error_msg|
        Promiscuous.warn "[receive] #{error_msg}"
        raise error_msg
      end
    end

    value = payload.attributes[attr]
    update = true

    attr_payload = Promiscuous::Subscriber::Operation.new(value)
    if model = attr_payload.model
      # Nested subscriber
      old_value =  __send__(attr)
      instance = old_value || model.__promiscuous_fetch_new(attr_payload.id)

      if instance.class != model
        # Because of the nasty trick with 'promiscuous_embedded_many'
        instance = model.__promiscuous_fetch_new(attr_payload.id)
      end

      nested_options = {:parent => self, :old_value => old_value}
      update = instance.__promiscuous_update(attr_payload, nested_options)
      value = instance
    end

    self.__send__("#{attr}=", value) if update
    true
  end
end