Class: DuckRecord::AttributeMutationTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/duck_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
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 5

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

Instance Method Details

#any_changes?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 33

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

#change_to_attribute(attr_name) ⇒ Object



27
28
29
30
31
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 27

def change_to_attribute(attr_name)
  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)


37
38
39
40
41
42
43
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 37

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_in_place?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 45

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

#changed_valuesObject



10
11
12
13
14
15
16
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 10

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



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

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

#force_change(attr_name) ⇒ Object



59
60
61
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 59

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

#forget_change(attr_name) ⇒ Object



49
50
51
52
53
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 49

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



55
56
57
# File 'lib/duck_record/attribute_mutation_tracker.rb', line 55

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