Class: Mobility::Plugins::ActiveModel::Dirty::MobilityMutationTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/mobility/plugins/active_model/dirty.rb

Constant Summary collapse

OPTION_NOT_GIVEN =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ MobilityMutationTracker

Returns a new instance of MobilityMutationTracker.


197
198
199
200
201
# File 'lib/mobility/plugins/active_model/dirty.rb', line 197

def initialize(model)
  @model = model
  @current_changes = {}.with_indifferent_access
  @previous_changes = {}.with_indifferent_access
end

Instance Attribute Details

#previous_changesObject (readonly)

Returns the value of attribute previous_changes


195
196
197
# File 'lib/mobility/plugins/active_model/dirty.rb', line 195

def previous_changes
  @previous_changes
end

Instance Method Details

#attribute_change(attr_name) ⇒ Object Also known as: attribute_change_to_be_saved


232
233
234
235
236
# File 'lib/mobility/plugins/active_model/dirty.rb', line 232

def attribute_change(attr_name)
  if attribute_changed?(attr_name)
    [attribute_was(attr_name), fetch_value(attr_name)]
  end
end

#attribute_changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) ⇒ Boolean Also known as: will_save_change_to_attribute?

Returns:

  • (Boolean)

250
251
252
253
254
# File 'lib/mobility/plugins/active_model/dirty.rb', line 250

def attribute_changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
  current_changes.include?(attr_name) &&
    (OPTION_NOT_GIVEN == from || attribute_was(attr_name) == from) &&
    (OPTION_NOT_GIVEN == to || fetch_value(attr_name) == to)
end

#attribute_previous_change(attr_name) ⇒ Object Also known as: saved_change_to_attribute


238
239
240
# File 'lib/mobility/plugins/active_model/dirty.rb', line 238

def attribute_previous_change(attr_name)
  previous_changes[attr_name]
end

#attribute_previously_changed?(attr_name) ⇒ Boolean Also known as: saved_change_to_attribute?

Returns:

  • (Boolean)

256
257
258
# File 'lib/mobility/plugins/active_model/dirty.rb', line 256

def attribute_previously_changed?(attr_name)
  previous_changes.include?(attr_name)
end

#attribute_previously_was(attr_name) ⇒ Object Also known as: attribute_before_last_save


242
243
244
245
246
247
248
# File 'lib/mobility/plugins/active_model/dirty.rb', line 242

def attribute_previously_was(attr_name)
  if attribute_previously_changed?(attr_name)
    # Calling +first+ here fetches the value before change from the
    # hash.
    previous_changes[attr_name].first
  end
end

#attribute_was(attr_name) ⇒ Object Also known as: attribute_in_database


260
261
262
263
264
265
266
# File 'lib/mobility/plugins/active_model/dirty.rb', line 260

def attribute_was(attr_name)
  if attribute_changed?(attr_name)
    current_changes[attr_name]
  else
    fetch_value(attr_name)
  end
end

#attribute_will_change!(attr_name) ⇒ Object


268
269
270
# File 'lib/mobility/plugins/active_model/dirty.rb', line 268

def attribute_will_change!(attr_name)
  current_changes[attr_name] = fetch_value(attr_name) unless current_changes.include?(attr_name)
end

#changedObject


208
209
210
# File 'lib/mobility/plugins/active_model/dirty.rb', line 208

def changed
  attr_names.select { |attr_name| attribute_changed?(attr_name) }
end

#changed?Boolean

Returns:

  • (Boolean)

228
229
230
# File 'lib/mobility/plugins/active_model/dirty.rb', line 228

def changed?
  attr_names.any? { |attr| attribute_changed?(attr) }
end

#changed_attributesObject


212
213
214
215
216
217
218
# File 'lib/mobility/plugins/active_model/dirty.rb', line 212

def changed_attributes
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    if attribute_changed?(attr_name)
      result[attr_name] = attribute_was(attr_name)
    end
  end
end

#changesObject


220
221
222
223
224
225
226
# File 'lib/mobility/plugins/active_model/dirty.rb', line 220

def changes
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    if change = attribute_change(attr_name)
      result.merge!(attr_name => change)
    end
  end
end

#finalize_changesObject


203
204
205
206
# File 'lib/mobility/plugins/active_model/dirty.rb', line 203

def finalize_changes
  @previous_changes = changes
  @current_changes = {}.with_indifferent_access
end

#restore_attribute!(attr_name) ⇒ Object


272
273
274
# File 'lib/mobility/plugins/active_model/dirty.rb', line 272

def restore_attribute!(attr_name)
  current_changes.delete(attr_name)
end