Module: Superstore::Persistence::ClassMethods

Defined in:
lib/superstore/persistence.rb

Instance Method Summary collapse

Instance Method Details

#_insert_record(attributes) ⇒ Object



10
11
12
13
14
# File 'lib/superstore/persistence.rb', line 10

def _insert_record(attributes)
  id = attributes.fetch(primary_key)

  adapter.insert table_name, id, serialize_attributes(attributes)
end

#_update_record(attributes, constraints) ⇒ Object



16
17
18
19
20
# File 'lib/superstore/persistence.rb', line 16

def _update_record(attributes, constraints)
  id = constraints.fetch(primary_key)

  adapter.update table_name, id, serialize_attributes(attributes)
end

#find_by_id(id) ⇒ Object



6
7
8
# File 'lib/superstore/persistence.rb', line 6

def find_by_id(id)
  find_by(id: id)
end

#instantiate(attributes, column_types = {}, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/superstore/persistence.rb', line 32

def instantiate(attributes, column_types = {}, &block)
  if attributes[superstore_column].is_a?(String)
    attributes = JSON.parse(attributes[superstore_column]).merge('id' => attributes['id'])
  end

  super(attributes, column_types, &block)
end

#serialize_attributes(attributes) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/superstore/persistence.rb', line 41

def serialize_attributes(attributes)
  serialized = {}
  attributes.each do |attr_name, value|
    next if attr_name == primary_key
    serialized[attr_name] = attribute_types[attr_name].serialize(value)
  end
  serialized
end