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



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

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)
      # Save the new instance so that its primary key is generated and pass this value onto our master model
      new_instance.save
      self.update_attribute(self.class.relationship.keys.first, new_instance.read_attribute(self.class.relationship.values.first.to_s))
    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



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

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)
  end
end