From a2339b41746b92231dfbb15ab19d0f7e1aa2e69a Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sat, 16 Feb 2008 16:42:46 -0800 Subject: organized congruence stuff in attribute --- lib/active_relation/primitives/attribute.rb | 30 ++++++++++++++--------- lib/active_relation/primitives/expression.rb | 1 - spec/active_relation/primitives/attribute_spec.rb | 16 ++++++------ 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index 4a10dffbe5..e55dd0bdc4 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -40,11 +40,27 @@ module ActiveRelation @alias == other.alias and ancestor == other.ancestor end - alias_method :eql?, :== - def =~(other) - !(history & other.send(:history)).empty? + module Congruence + def self.included(klass) + klass.class_eval do + alias_method :eql?, :== + end + end + + def hash + relation.hash + name.hash + end + + def history + [self] + (ancestor ? [ancestor, ancestor.send(:history)].flatten : []) + end + + def =~(other) + !(history & other.send(:history)).empty? + end end + include Congruence module Predications def equals(other) @@ -100,17 +116,9 @@ module ActiveRelation strategy.attribute prefix, name, self.alias end - def hash - relation.hash + name.hash - end - private def prefix relation.prefix_for(self) end - - def history - [self] + (ancestor ? [ancestor, ancestor.send(:history)].flatten : []) - end end end \ No newline at end of file diff --git a/lib/active_relation/primitives/expression.rb b/lib/active_relation/primitives/expression.rb index 4d28ef108c..35c1b5ff65 100644 --- a/lib/active_relation/primitives/expression.rb +++ b/lib/active_relation/primitives/expression.rb @@ -36,6 +36,5 @@ module ActiveRelation ancestor == other.ancestor and @alias == other.alias end - alias_method :eql?, :== end end \ No newline at end of file diff --git a/spec/active_relation/primitives/attribute_spec.rb b/spec/active_relation/primitives/attribute_spec.rb index 535e6245b5..39feacbac7 100644 --- a/spec/active_relation/primitives/attribute_spec.rb +++ b/spec/active_relation/primitives/attribute_spec.rb @@ -47,14 +47,16 @@ module ActiveRelation end end - describe '=~' do - it "obtains if the attributes are identical" do - Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name) - end + describe Attribute::Congruence do + describe '=~' do + it "obtains if the attributes are identical" do + Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name) + end - it "obtains if the attributes have an overlapping history" do - Attribute.new(@relation, :name, nil, Attribute.new(@relation, :name)).should =~ Attribute.new(@relation, :name) - Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name, nil, Attribute.new(@relation, :name)) + it "obtains if the attributes have an overlapping history" do + Attribute.new(@relation, :name, nil, Attribute.new(@relation, :name)).should =~ Attribute.new(@relation, :name) + Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name, nil, Attribute.new(@relation, :name)) + end end end -- cgit v1.2.3