Class: ActiveRecord::AttributeMutationTracker

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

Overview

:nodoc:

Constant Summary collapse

OPTION_NOT_GIVEN =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeMutationTracker

Returns a new instance of AttributeMutationTracker.



5
6
7
8
9
# File 'lib/active_record/attribute_mutation_tracker.rb', line 5

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

Instance Method Details

#any_changes?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/active_record/attribute_mutation_tracker.rb', line 39

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

#change_to_attribute(attr_name) ⇒ Object



32
33
34
35
36
37
# File 'lib/active_record/attribute_mutation_tracker.rb', line 32

def change_to_attribute(attr_name)
  attr_name = attr_name.to_s
  if changed?(attr_name)
    [attributes[attr_name].original_value, attributes.fetch_value(attr_name)]
  end
end

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

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/active_record/attribute_mutation_tracker.rb', line 43

def changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
  attr_name = attr_name.to_s
  forced_changes.include?(attr_name) ||
    attributes[attr_name].changed? &&
    (OPTION_NOT_GIVEN == from || attributes[attr_name].original_value == from) &&
    (OPTION_NOT_GIVEN == to || attributes[attr_name].value == to)
end

#changed_attribute_namesObject



11
12
13
# File 'lib/active_record/attribute_mutation_tracker.rb', line 11

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 'lib/active_record/attribute_mutation_tracker.rb', line 51

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

#changed_valuesObject



15
16
17
18
19
20
21
# File 'lib/active_record/attribute_mutation_tracker.rb', line 15

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

#changesObject



23
24
25
26
27
28
29
30
# File 'lib/active_record/attribute_mutation_tracker.rb', line 23

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

#deprecated_force_change(attr_name) ⇒ Object



69
70
71
# File 'lib/active_record/attribute_mutation_tracker.rb', line 69

def deprecated_force_change(attr_name)
  deprecated_forced_changes << attr_name.to_s
end

#force_change(attr_name) ⇒ Object



65
66
67
# File 'lib/active_record/attribute_mutation_tracker.rb', line 65

def force_change(attr_name)
  forced_changes << attr_name.to_s
end

#forget_change(attr_name) ⇒ Object



55
56
57
58
59
# File 'lib/active_record/attribute_mutation_tracker.rb', line 55

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

#original_value(attr_name) ⇒ Object



61
62
63
# File 'lib/active_record/attribute_mutation_tracker.rb', line 61

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