Module: Dinamo::Model::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Dinamo::Model
Defined in:
lib/dinamo/model/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create_or_update(*args) ⇒ Object



109
110
111
# File 'lib/dinamo/model/persistence.rb', line 109

def create_or_update(*args)
  new_record? ? create_record(*args) : update_record(*args)
end

#create_record(validate: true) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/dinamo/model/persistence.rb', line 113

def create_record(validate: true)
  if !validate || valid?
    self.class.adapter.insert(**self.class.symbolize(attributes))
    self.new_record = false
    self
  else
    fail Exceptions::ValidationError
  end
end

#destroyObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dinamo/model/persistence.rb', line 77

def destroy
  returned_value =
    if persisted?
      self.class.adapter.delete(primary_keys)
      true
    else
      false
    end
  (@destroyed = true) && freeze
  returned_value
end

#destroyed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/dinamo/model/persistence.rb', line 89

def destroyed?
  !!@destroyed
end

#new_record=(bool) ⇒ Object



69
70
71
# File 'lib/dinamo/model/persistence.rb', line 69

def new_record=(bool)
  @new_record = bool
end

#new_record?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/dinamo/model/persistence.rb', line 73

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/dinamo/model/persistence.rb', line 93

def persisted?
  !(new_record? || destroyed?)
end

#reload!Object



164
165
166
167
168
169
# File 'lib/dinamo/model/persistence.rb', line 164

def reload!
  fresh_object = self.class.get(primary_keys)
  @attributes = fresh_object.instance_variable_get(:'@attributes')
  @new_record = false
  self
end

#save(*args) ⇒ Object



97
98
99
100
101
# File 'lib/dinamo/model/persistence.rb', line 97

def save(*args)
  !!save!(*args)
rescue Exceptions::ValidationError
  false
end

#save!(*args) ⇒ Object



103
104
105
106
107
# File 'lib/dinamo/model/persistence.rb', line 103

def save!(*args)
  with_callback :save, *args do
    !!create_or_update(*args)
  end
end

#transaction(&block) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/dinamo/model/persistence.rb', line 123

def transaction(&block)
  returned_value = block.call
  unless returned_value
    silent_assign(previous_attributes)
    clear_previous_attributes
  end
  returned_value
end

#update(validate: false, **new_attributes) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dinamo/model/persistence.rb', line 132

def update(validate: false, **new_attributes)
  transaction do
    with_callback :update do
      self.attributes = new_attributes
      return save if changed?
      true
    end
  end
rescue Exceptions::PrimaryKeyError
  false
end

#update!(validate: false, **new_attributes) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/dinamo/model/persistence.rb', line 144

def update!(validate: false, **new_attributes)
  transaction do
    with_callback :update do
      self.attributes = new_attributes
      return save! if changed?
      true
    end
  end
end

#update_record(validate: true) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/dinamo/model/persistence.rb', line 154

def update_record(validate: true)
  if !validate || valid?
    symbolized = self.class.symbolize(variable_attributes)
    updated_object = self.class.adapter.update(primary_keys, **symbolized)
    self.attributes = updated_object.attributes
  else
    fail Exceptions::ValidationError
  end
end