Class: ActiveModel::AttributeMutationTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/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.



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

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

Instance Method Details

#any_changes?Boolean

Returns:

  • (Boolean)


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

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

#change_to_attribute(attr_name) ⇒ Object



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

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)


46
47
48
49
50
51
52
# File 'lib/active_model/attribute_mutation_tracker.rb', line 46

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



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

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

#changed_in_place?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/active_model/attribute_mutation_tracker.rb', line 54

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

#changed_valuesObject



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

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



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

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

#force_change(attr_name) ⇒ Object



68
69
70
# File 'lib/active_model/attribute_mutation_tracker.rb', line 68

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

#forget_change(attr_name) ⇒ Object



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

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



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

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