Class: Db::Sync::Model

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/db/sync/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.include_id?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/db/sync/model.rb', line 16

def self.include_id?
  attribute_names.include?('id')
end

.ordered_attributesObject


28
29
30
# File 'lib/db/sync/model.rb', line 28

def self.ordered_attributes
  pkey + (attribute_names - pkey).sort
end

.pkeyObject


20
21
22
23
24
25
26
# File 'lib/db/sync/model.rb', line 20

def self.pkey
  if include_id?
    ['id']
  else
    attribute_names.sort
  end
end

.recordsObject


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

def self.records
  attributes_order = ordered_attributes
  order(pkey).map do |record|
    res = {}
    attributes_order.each do |key|
      res[key] = record[key]
    end
    res
  end
end

Instance Method Details

#compare_unique_data(other) ⇒ Object


10
11
12
13
14
# File 'lib/db/sync/model.rb', line 10

def compare_unique_data(other)
  self.class.pkey.each do |key|
    attributes[key] <=> other[key]
  end
end

#unique_dataObject


6
7
8
# File 'lib/db/sync/model.rb', line 6

def unique_data
  attributes.slice(*self.class.pkey)
end