Module: Artery::Model::Subscriptions::ClassMethods

Defined in:
lib/artery/model/subscriptions.rb

Instance Method Summary collapse

Instance Method Details

#artery_add_get_subscriptionsObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/artery/model/subscriptions.rb', line 70

def artery_add_get_subscriptions
  artery_add_subscription Routing.uri(model: artery_model_name_plural, action: :get) do |data, reply, _sub|
    obj = artery_find data['uuid']

    representation = data['representation']

    data = obj.blank? ? { error: 'not_found' } : obj.to_artery(representation)

    Artery.publish(reply, data)
  end

  artery_add_subscription Routing.uri(model: artery_model_name_plural, action: :get_all) do |data, reply, _sub|
    scope    = "artery_#{data['scope'] || 'all'}"
    per_page = data['per_page']
    page     = data['page'] || 0

    representation = data['representation']

    data = if respond_to?(scope)
             relation = send(scope)
             relation = relation.offset(page * per_page).limit(per_page) if per_page
             objects = relation.map { |obj| obj.to_artery(representation) }
             {
               objects: objects,
               _index: Artery.message_class.latest_index(artery_model_name)
             }
           else
             Artery.logger.error "No artery scope '#{data['scope']}' defined!"
             { error: 'No such scope!' }
           end

    Artery.publish(reply, data)
  end

  artery_add_subscription Routing.uri(model: artery_model_name_plural,
                                      action: :get_updates) do |data, reply, _sub|
    index = data['after_index'].to_i
    autoenrich = data['representation'].present?
    per_page = data['per_page'] || (autoenrich ? ARTERY_MAX_AUTOENRICHED_UPDATES_SYNC : ARTERY_MAX_UPDATES_SYNC)

    if index.positive?
      messages = Artery.message_class.after_index(artery_model_name, index).limit(per_page)
    else
      Artery.publish(reply, error: :bad_index)
      return
    end

    # Deduplicate
    messages = messages.to_a.group_by { |m| [m.action, m.data] }.values
                       .map { |mm| mm.max_by { |m| m.index.to_i } }
                       .sort_by { |m| m.index.to_i }

    latest_index = Artery.message_class.latest_index(artery_model_name)
    updates_latest_index = messages.last&.index || latest_index

    # Autoenrich data
    if autoenrich
      scope = "artery_#{data['scope'] || 'all'}"
      autoenrich_data = send(scope).artery_find_all(messages.map { |m| m.data['uuid'] }).to_h do |obj|
        [obj.send(artery_uuid_attribute), obj.to_artery(data['representation'])]
      end
    end

    updates = messages.map do |message|
      upd = message.to_artery.merge('action' => message.action)
      # WARNING: duplicated logic with `Subscription#handle`!
      if %i[create update].include?(message.action.to_sym) &&
         autoenrich_data &&
         (attrs = autoenrich_data[message.data['uuid']])
        upd['attributes'] = attrs
      end
      upd
    end

    Artery.publish(reply, updates: updates,
                          _index: updates_latest_index, _continue: updates_latest_index < latest_index)
  end
end

#artery_add_subscription(uri, options = {}, &blk) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/artery/model/subscriptions.rb', line 32

def artery_add_subscription(uri, options = {}, &blk)
  raise ArgumentError, 'block must be provided to handle subscription updates' unless block_given?

  handler ||= Multiblock.wrapper

  if uri.action.blank? || uri.action.to_s == '*'
    yield(handler)
  else
    handler._default(&blk)
  end

  artery[:subscriptions] ||= []
  artery[:subscriptions].push Subscription.new(self, uri, **options, handler: handler)
end

#artery_find(uuid) ⇒ Object



22
23
24
# File 'lib/artery/model/subscriptions.rb', line 22

def artery_find(uuid)
  artery_find_all([uuid]).first
end

#artery_find_all(uuids) ⇒ Object



18
19
20
# File 'lib/artery/model/subscriptions.rb', line 18

def artery_find_all(uuids)
  where "#{artery_uuid_attribute}": uuids
end

#artery_resync!Object



26
27
28
29
30
# File 'lib/artery/model/subscriptions.rb', line 26

def artery_resync!
  return false if artery_source_model?

  artery[:subscriptions]&.detect(&:synchronize?)&.receive_all
end

#artery_rewind_message_counter!(service: nil) ⇒ Object

rubocop:disable Naming/PredicateMethod



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/artery/model/subscriptions.rb', line 55

def artery_rewind_message_counter!(service: nil) # rubocop:disable Naming/PredicateMethod
  return false if artery_source_model?

  subscriptions = artery[:subscriptions]&.select do |subscription|
    next false unless subscription.rewindable?

    service.nil? || subscription.uri.service.to_sym == service.to_sym
  end
  return false if subscriptions.blank?

  subscriptions.each(&:rewind_message_counter!)
  true
end

#artery_watch_model(service:, model: nil, action: nil, **kwargs, &blk) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/artery/model/subscriptions.rb', line 47

def artery_watch_model(service:, model: nil, action: nil, **kwargs, &blk)
  model  ||= artery_model_name
  action ||= '*'

  artery_add_subscription Routing.uri(service: service, model: model, action: action), **kwargs, client: true,
                          &blk
end