Class: TableSync::ORMAdapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/table_sync/orm_adapter/base.rb

Direct Known Subclasses

ActiveRecord, Sequel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_class, object_data) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
# File 'lib/table_sync/orm_adapter/base.rb', line 7

def initialize(object_class, object_data)
  @object_class = object_class
  @object_data  = object_data.symbolize_keys

  validate!
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/table_sync/orm_adapter/base.rb', line 5

def object
  @object
end

#object_classObject (readonly)

Returns the value of attribute object_class.



5
6
7
# File 'lib/table_sync/orm_adapter/base.rb', line 5

def object_class
  @object_class
end

#object_dataObject (readonly)

Returns the value of attribute object_data.



5
6
7
# File 'lib/table_sync/orm_adapter/base.rb', line 5

def object_data
  @object_data
end

Class Method Details

.model_namingObject

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/table_sync/orm_adapter/base.rb', line 87

def self.model_naming
  raise NotImplementedError
end

Instance Method Details

#attributesObject

:nocov:

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/table_sync/orm_adapter/base.rb', line 83

def attributes
  raise NotImplementedError
end

#attributes_for_destroyObject



46
47
48
49
50
51
52
# File 'lib/table_sync/orm_adapter/base.rb', line 46

def attributes_for_destroy
  if object.respond_to?(:attributes_for_destroy)
    object.attributes_for_destroy
  else
    needle
  end
end

#attributes_for_headersObject



62
63
64
65
66
67
68
# File 'lib/table_sync/orm_adapter/base.rb', line 62

def attributes_for_headers
  if object.respond_to?(:attributes_for_headers)
    object.attributes_for_headers
  else
    attributes
  end
end

#attributes_for_routing_keyObject



54
55
56
57
58
59
60
# File 'lib/table_sync/orm_adapter/base.rb', line 54

def attributes_for_routing_key
  if object.respond_to?(:attributes_for_routing_key)
    object.attributes_for_routing_key
  else
    attributes
  end
end

#attributes_for_updateObject

ATTRIBUTES



38
39
40
41
42
43
44
# File 'lib/table_sync/orm_adapter/base.rb', line 38

def attributes_for_update
  if object.respond_to?(:attributes_for_sync)
    object.attributes_for_sync
  else
    attributes
  end
end

#empty?Boolean

MISC

Returns:

  • (Boolean)


76
77
78
# File 'lib/table_sync/orm_adapter/base.rb', line 76

def empty?
  object.nil?
end

#findObject



28
29
30
# File 'lib/table_sync/orm_adapter/base.rb', line 28

def find
  self
end

#initObject

FIND OR INIT OBJECT



24
25
26
# File 'lib/table_sync/orm_adapter/base.rb', line 24

def init
  self
end

#needleObject



32
33
34
# File 'lib/table_sync/orm_adapter/base.rb', line 32

def needle
  object_data.slice(*primary_key_columns)
end

#primary_key_columnsObject



70
71
72
# File 'lib/table_sync/orm_adapter/base.rb', line 70

def primary_key_columns
  Array.wrap(object_class.primary_key).map(&:to_sym)
end

#validate!Object

VALIDATE



16
17
18
19
20
# File 'lib/table_sync/orm_adapter/base.rb', line 16

def validate!
  if (primary_key_columns - object_data.keys).any?
    raise TableSync::NoPrimaryKeyError.new(object_class, object_data, primary_key_columns)
  end
end