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

:nocov:



13
14
15
16
17
18
# File 'lib/table_sync/orm_adapter/base.rb', line 13

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

:nocov:

Raises:

  • (NotImplementedError)


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

def self.model_naming
  raise NotImplementedError
end

Instance Method Details

#attributesObject

:nocov:

Raises:

  • (NotImplementedError)


89
90
91
# File 'lib/table_sync/orm_adapter/base.rb', line 89

def attributes
  raise NotImplementedError
end

#attributes_for_destroyObject



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

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

#attributes_for_headersObject



68
69
70
71
72
73
74
# File 'lib/table_sync/orm_adapter/base.rb', line 68

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

#attributes_for_routing_keyObject



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

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



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

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

#empty?Boolean

MISC

Returns:

  • (Boolean)


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

def empty?
  object.nil?
end

#findObject



34
35
36
# File 'lib/table_sync/orm_adapter/base.rb', line 34

def find
  self
end

#initObject

FIND OR INIT OBJECT



30
31
32
# File 'lib/table_sync/orm_adapter/base.rb', line 30

def init
  self
end

#needleObject



38
39
40
# File 'lib/table_sync/orm_adapter/base.rb', line 38

def needle
  object_data.slice(*primary_key_columns)
end

#primary_key_columnsObject



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

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

#validate!Object

VALIDATE



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

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