Module: Flex::ClassProxy::ModelSyncer

Defined in:
lib/flex/class_proxy/model_syncer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#syncedObject

Returns the value of attribute synced.



5
6
7
# File 'lib/flex/class_proxy/model_syncer.rb', line 5

def synced
  @synced
end

Instance Method Details

#add_callbacksObject



19
20
21
22
23
24
25
26
# File 'lib/flex/class_proxy/model_syncer.rb', line 19

def add_callbacks
  context.class_eval do
    raise NotImplementedError, "the class #{self} must implement :after_save and :after_destroy callbacks" \
          unless respond_to?(:after_save) && respond_to?(:after_destroy)
    after_save    { flex.sync }
    after_destroy { flex.sync }
  end
end

#sync(*synced) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/flex/class_proxy/model_syncer.rb', line 7

def sync(*synced)
  # Flex::ActiveModel has its own way of syncing, and a Flex::ModelSyncer cannot be synced by itself
  raise ArgumentError, %(You cannot flex.sync(self) #{context}.) \
        if synced.any?{|s| s == context} && !context.include?(Flex::ModelIndexer)
  synced.each do |s|
    s == context || s.is_a?(Symbol) || s.is_a?(String) || raise(ArgumentError, "self, string or symbol expected, got #{s.inspect}")
  end
  @synced ||= []
  @synced  |= synced
  add_callbacks
end