Class: ActiveModel::AttributeMutationTracker
- Defined in:
- activemodel/lib/active_model/attribute_mutation_tracker.rb
Overview
:nodoc:
Direct Known Subclasses
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_attribute_names ⇒ Object
- #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.
10 11 12 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 10 def initialize(attributes) @attributes = attributes end |
Instance Method Details
#any_changes? ⇒ Boolean
40 41 42 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 40 def any_changes? attr_names.any? { |attr| changed?(attr) } end |
#change_to_attribute(attr_name) ⇒ Object
34 35 36 37 38 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 34 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
44 45 46 47 48 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 44 def changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) attribute_changed?(attr_name) && (OPTION_NOT_GIVEN == from || original_value(attr_name) == type_cast(attr_name, from)) && (OPTION_NOT_GIVEN == to || fetch_value(attr_name) == type_cast(attr_name, to)) end |
#changed_attribute_names ⇒ Object
14 15 16 |
# File 'activemodel/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
50 51 52 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 50 def changed_in_place?(attr_name) attributes[attr_name].changed_in_place? end |
#changed_values ⇒ Object
18 19 20 21 22 23 24 |
# File 'activemodel/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] = original_value(attr_name) end end end |
#changes ⇒ Object
26 27 28 29 30 31 32 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 26 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
63 64 65 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 63 def force_change(attr_name) forced_changes[attr_name] = fetch_value(attr_name) end |
#forget_change(attr_name) ⇒ Object
54 55 56 57 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 54 def forget_change(attr_name) attributes[attr_name] = attributes[attr_name].forgetting_assignment forced_changes.delete(attr_name) end |
#original_value(attr_name) ⇒ Object
59 60 61 |
# File 'activemodel/lib/active_model/attribute_mutation_tracker.rb', line 59 def original_value(attr_name) attributes[attr_name].original_value end |