Class: ActiveRecord::AttributeMutationTracker
- Defined in:
- activerecord/lib/active_record/attribute_mutation_tracker.rb
Overview
:nodoc:
Constant Summary collapse
- OPTION_NOT_GIVEN =
Object.new
Instance Method Summary collapse
- #any_changes? ⇒ Boolean
- #change_to_attribute(attr_name) ⇒ Object
- #changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) ⇒ Boolean
- #changed_in_place?(attr_name) ⇒ Boolean
- #changed_values ⇒ Object
- #changes ⇒ Object
- #force_change(attr_name) ⇒ Object
- #forget_change(attr_name) ⇒ Object
-
#initialize(attributes) ⇒ AttributeMutationTracker
constructor
A new instance of AttributeMutationTracker.
- #original_value(attr_name) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ AttributeMutationTracker
Returns a new instance of AttributeMutationTracker.
7 8 9 10 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 7 def initialize(attributes) @attributes = attributes @forced_changes = Set.new end |
Instance Method Details
#any_changes? ⇒ Boolean
36 37 38 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 36 def any_changes? attr_names.any? { |attr| changed?(attr) } end |
#change_to_attribute(attr_name) ⇒ Object
29 30 31 32 33 34 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 29 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
40 41 42 43 44 45 46 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 40 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
48 49 50 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 48 def changed_in_place?(attr_name) attributes[attr_name.to_s].changed_in_place? end |
#changed_values ⇒ Object
12 13 14 15 16 17 18 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 12 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 |
#changes ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 20 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
62 63 64 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 62 def force_change(attr_name) forced_changes << attr_name.to_s end |
#forget_change(attr_name) ⇒ Object
52 53 54 55 56 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 52 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
58 59 60 |
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 58 def original_value(attr_name) attributes[attr_name.to_s].original_value end |