Class: ActiveModel::AttributeMutationTracker

Inherits:
Object
  • Object
show all
Defined in:
activemodel/lib/active_model/attribute_mutation_tracker.rb

Overview

:nodoc:

Direct Known Subclasses

ForcedMutationTracker

Constant Summary collapse

OPTION_NOT_GIVEN =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(attributes, forced_changes = Set.new) ⇒ AttributeMutationTracker

Returns a new instance of AttributeMutationTracker.



10
11
12
13
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 10

def initialize(attributes, forced_changes = Set.new)
  @attributes = attributes
  @forced_changes = forced_changes
end

Instance Method Details

#any_changes?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 41

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

#change_to_attribute(attr_name) ⇒ Object



35
36
37
38
39
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 35

def change_to_attribute(attr_name)
  if changed?(attr_name)
    [original_value(attr_name), fetch_value(attr_name)]
  end
end

#changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 45

def changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
  attribute_changed?(attr_name) &&
    (OPTION_NOT_GIVEN == from || original_value(attr_name) == from) &&
    (OPTION_NOT_GIVEN == to || fetch_value(attr_name) == to)
end

#changed_attribute_namesObject



15
16
17
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 15

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

#changed_in_place?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 51

def changed_in_place?(attr_name)
  attributes[attr_name].changed_in_place?
end

#changed_valuesObject



19
20
21
22
23
24
25
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 19

def changed_values
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    if changed?(attr_name)
      result[attr_name] = original_value(attr_name)
    end
  end
end

#changesObject



27
28
29
30
31
32
33
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 27

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

#force_change(attr_name) ⇒ Object



64
65
66
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 64

def force_change(attr_name)
  forced_changes << attr_name
end

#forget_change(attr_name) ⇒ Object



55
56
57
58
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 55

def forget_change(attr_name)
  attributes[attr_name] = attributes[attr_name].forgetting_assignment
  forced_changes.delete(attr_name)
end

#original_value(attr_name) ⇒ Object



60
61
62
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 60

def original_value(attr_name)
  attributes[attr_name].original_value
end