aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2015-10-06 08:51:14 -0600
committerSean Griffin <sean@seantheprogrammer.com>2015-10-06 08:52:28 -0600
commit9e25e0e1735f2ccca69679243aa8cf0885104164 (patch)
treec3759c6e1b0ca18a1d88df98a4e67dc621554457 /activerecord/lib
parent7a96e629efdd91865309e334d89bdca7c63093df (diff)
downloadrails-9e25e0e1735f2ccca69679243aa8cf0885104164.tar.gz
rails-9e25e0e1735f2ccca69679243aa8cf0885104164.tar.bz2
rails-9e25e0e1735f2ccca69679243aa8cf0885104164.zip
Implement equality comparison on `AttributeSet` and friends
Any gems or libraries which do work with serialization or YAML will ultimately need to compare these objects (albeit indirectly) to ensure correctness. These will likely never get used internally (as they're slow), but we should still expose them for others.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/attribute_set.rb4
-rw-r--r--activerecord/lib/active_record/attribute_set/builder.rb30
2 files changed, 23 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb
index 026b3cf014..be581ac2a9 100644
--- a/activerecord/lib/active_record/attribute_set.rb
+++ b/activerecord/lib/active_record/attribute_set.rb
@@ -91,6 +91,10 @@ module ActiveRecord
AttributeSet.new(new_attributes)
end
+ def ==(other)
+ attributes == other.attributes
+ end
+
protected
attr_reader :attributes
diff --git a/activerecord/lib/active_record/attribute_set/builder.rb b/activerecord/lib/active_record/attribute_set/builder.rb
index f974b7a876..3bd7c7997b 100644
--- a/activerecord/lib/active_record/attribute_set/builder.rb
+++ b/activerecord/lib/active_record/attribute_set/builder.rb
@@ -68,10 +68,29 @@ module ActiveRecord
end
end
+ def ==(other)
+ if other.is_a?(LazyAttributeHash)
+ materialize == other.materialize
+ else
+ materialize == other
+ end
+ end
+
protected
attr_reader :types, :values, :additional_types, :delegate_hash
+ def materialize
+ unless @materialized
+ values.each_key { |key| self[key] }
+ types.each_key { |key| self[key] }
+ unless frozen?
+ @materialized = true
+ end
+ end
+ delegate_hash
+ end
+
private
def assign_default_value(name)
@@ -85,16 +104,5 @@ module ActiveRecord
delegate_hash[name] = Attribute.uninitialized(name, type)
end
end
-
- def materialize
- unless @materialized
- values.each_key { |key| self[key] }
- types.each_key { |key| self[key] }
- unless frozen?
- @materialized = true
- end
- end
- delegate_hash
- end
end
end