Module: ModelSync::Base

Included in:
ActiveRecord::Base
Defined in:
lib/model_sync.rb

Defined Under Namespace

Modules: Config

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/model_sync.rb', line 34

def method_missing(id, *args, &block)
  case(id.to_s)
  when /^create_#{self.class.slave_model_name}$/
    # Only create a new slave if one doesn't already exist
    unless find_slave_instance
      # If we've received a create_{slave_model} call then create a new instance of it and sync to it
      new_instance = self.class.slave_model_class.new
      perform_sync(new_instance)
      new_instance.id = self.id
      new_instance.save
    end
  when /^synch?ed_with_#{self.class.slave_model_name}\?$/
    !!find_slave_instance
  else
    super
  end
end

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
# File 'lib/model_sync.rb', line 3

def self.included(klass)
  klass.class_eval do
    extend Config
  end
end

Instance Method Details

#sync_changesObject



25
26
27
28
29
30
31
32
# File 'lib/model_sync.rb', line 25

def sync_changes
  # If we can find a slave instance...
  if slave_instance = find_slave_instance
    # ... then sync the changes over
    perform_sync(slave_instance)
    slave_instance.save
  end
end