Module: Kirei::Model

Extended by:
T::Helpers, T::Sig
Defined in:
lib/kirei/model.rb,
lib/kirei/model/class_methods.rb,
lib/kirei/model/human_id_generator.rb,
lib/kirei/model/base_class_interface.rb

Defined Under Namespace

Modules: BaseClassInterface, ClassMethods Classes: HumanIdGenerator

Instance Method Summary collapse

Instance Method Details

#classObject

rubocop:disable all



10
# File 'lib/kirei/model.rb', line 10

def class; super; end

#deleteObject



28
29
30
31
# File 'lib/kirei/model.rb', line 28

def delete
  count = self.class.query.where({ id: id }).delete
  count == 1
end

#saveObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kirei/model.rb', line 36

def save
  previous_record = self.class.find_by({ id: id })

  hash = serialize
  Helpers.deep_symbolize_keys!(hash)
  hash = T.cast(hash, T::Hash[Symbol, T.untyped])

  if previous_record.nil?
    self.class.create(hash)
  else
    update(hash)
  end
end

#update(hash) ⇒ Object



18
19
20
21
22
23
# File 'lib/kirei/model.rb', line 18

def update(hash)
  hash[:updated_at] = Time.now.utc if respond_to?(:updated_at) && hash[:updated_at].nil?
  self.class.wrap_jsonb_non_primivitives!(hash)
  self.class.query.where({ id: id }).update(hash)
  self.class.find_by({ id: id })
end