Module: Synchroniser::Model

Included in:
Post
Defined in:
lib/synchroniser/model.rb

Instance Method Summary collapse

Instance Method Details

#after_saveObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/synchroniser/model.rb', line 31

def after_save
  # we must have a new object
  sync_item = SyncItem.new({ 
      :group       => group,
      :item        => self.item,
      :class_name  => self.class.to_s,
      :class_id    => self.id,
      :status      => "1" })
  sync_item.save
  SyncItem.logger.info("Created new #{sync_item.class_name}")
end

#before_validationObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/synchroniser/model.rb', line 18

def before_validation
  if (sync_item = SyncItem.find_by_group_and_item(group, self.item)).nil? === false
    # we've already sync'd this object
    sync_item.status = 2 
    sync_item.save
    SyncItem.logger.info("#{sync_item.class_name} with id #{sync_item.class_id} doesn't require an update")
    #stop further saving
    #TODO on innodb tables this will cause the transaction to rollback
    return false
  end
  return true
end

#delete_expired_itemsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/synchroniser/model.rb', line 4

def delete_expired_items
  params = {:status => false,
            :group    => group}
  SyncItem.find(:all, :conditions => params).each {|sync_item|
    begin
      eval "#{sync_item.class_name}.find(#{sync_item.id}).delete"
    rescue Exception
      SyncItem.logger.warn("Unable to remove #{sync_item.class_name}.find(#{sync_item.id})")
    end
    sync_item.delete
  }
  SyncItem.logger.info("Expired Items Deleted")
end

#groupObject



47
48
49
# File 'lib/synchroniser/model.rb', line 47

def group
  @@group
end

#group=(value) ⇒ Object



43
44
45
# File 'lib/synchroniser/model.rb', line 43

def group=(value)
  @@group = value
end

#verifyObject



51
52
53
54
55
# File 'lib/synchroniser/model.rb', line 51

def verify
    if self.item.nil?
      raise NotImplementedError, "Method call '#{self.class}.item' is returning null. Please ensure it returns a unique value per instance."
    end
end