Module: Tarantool::Model::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Tarantool::Model
Defined in:
tarantool-model/lib/tarantool/model/persistence.rb

Overview

create, save, update, destroy and reload with validations and callbacks. Every write refreshes the attributes from the row the storage returns, so server-side defaults and sequences show up.

Instance Method Summary collapse

Instance Method Details

#destroy ⇒ Object

A row that is already gone still counts as destroyed.



49
50
51
52
53
# File 'tarantool-model/lib/tarantool/model/persistence.rb', line 49

def destroy
  return false unless run_callbacks(:destroy) { delete_row }

  freeze
end

#reload ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
# File 'tarantool-model/lib/tarantool/model/persistence.rb', line 55

def reload
  row = space_storage.get(space_name, key_in_database)
  raise RecordNotFound.new(self.class, key_in_database) unless row

  assign_row(row)
  clear_changes_information

  self
end

#save ⇒ Object



26
27
28
# File 'tarantool-model/lib/tarantool/model/persistence.rb', line 26

def save
  valid? && persist
end

#save! ⇒ Object

Raises:



30
31
32
33
34
# File 'tarantool-model/lib/tarantool/model/persistence.rb', line 30

def save!
  raise RecordInvalid, self unless valid?

  persist || raise(RecordNotSaved, self)
end

#update(attributes) ⇒ Object



36
37
38
39
40
# File 'tarantool-model/lib/tarantool/model/persistence.rb', line 36

def update(attributes)
  assign_attributes(attributes)

  save
end

#update!(attributes) ⇒ Object



42
43
44
45
46
# File 'tarantool-model/lib/tarantool/model/persistence.rb', line 42

def update!(attributes)
  assign_attributes(attributes)

  save!
end