diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-12 15:40:06 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-12 15:40:06 -0700 |
commit | fff47a5a5e92eccf949785231d1f6953d6fdc640 (patch) | |
tree | e7e651d680bacc1542883b6a1c6c2ef21c3f2d1d | |
parent | ea111521b7eb7456934a283d96f6eb986322baf2 (diff) | |
download | rails-fff47a5a5e92eccf949785231d1f6953d6fdc640.tar.gz rails-fff47a5a5e92eccf949785231d1f6953d6fdc640.tar.bz2 rails-fff47a5a5e92eccf949785231d1f6953d6fdc640.zip |
some memoizing and hash equality performance optimizations
-rw-r--r-- | lib/arel/primitives/attribute.rb | 14 | ||||
-rw-r--r-- | lib/arel/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 2 | ||||
-rw-r--r-- | spec/arel/unit/primitives/attribute_spec.rb | 20 | ||||
-rw-r--r-- | spec/arel/unit/relations/insertion_spec.rb | 2 | ||||
-rw-r--r-- | spec/arel/unit/relations/update_spec.rb | 2 |
6 files changed, 17 insertions, 25 deletions
diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 78a519b35c..acc4d86a80 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -9,8 +9,8 @@ module Arel @relation, @name, @alias, @ancestor = relation, name, options[:alias], options[:ancestor] end - def alias_or_name - @alias || name + def named?(hypothetical_name) + (@alias || name).to_s == hypothetical_name.to_s end def aggregation? @@ -57,12 +57,12 @@ module Arel include Transformations module Congruence - def self.included(klass) - klass.hash_on :name - end - + # def self.included(klass) + # klass.hash_on :name + # end + # def history - [self] + (ancestor ? [ancestor, ancestor.history].flatten : []) + @history ||= [self] + (ancestor ? [ancestor, ancestor.history].flatten : []) end def match?(other) diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 7b6f3a46f8..663711760c 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -8,7 +8,7 @@ module Arel :to => :relation def attributes - relation.attributes.collect { |a| a.bind(self) } + @attributes ||= relation.attributes.collect { |a| a.bind(self) } end end end
\ No newline at end of file diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 4653323514..b2f811cea2 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -136,7 +136,7 @@ module Arel end def find_attribute_matching_name(name) - attributes.detect { |a| a.alias_or_name.to_s == name.to_s } + attributes.detect { |a| a.named?(name) } end # TESTME - added relation_for(x)[x] because of AR diff --git a/spec/arel/unit/primitives/attribute_spec.rb b/spec/arel/unit/primitives/attribute_spec.rb index e21a5902d5..7e0155f84d 100644 --- a/spec/arel/unit/primitives/attribute_spec.rb +++ b/spec/arel/unit/primitives/attribute_spec.rb @@ -4,7 +4,7 @@ module Arel describe Attribute do before do @relation = Table.new(:users) - @attribute = Attribute.new(@relation, :id) + @attribute = @relation[:id] end describe Attribute::Transformations do @@ -45,22 +45,21 @@ module Arel end describe Attribute::Congruence do - describe 'match?' do - + describe '#match?' do it "obtains if the attributes are identical" do - Attribute.new(@relation, :name).should be_match(Attribute.new(@relation, :name)) + @attribute.should be_match(@attribute) end it "obtains if the attributes have an overlapping history" do - Attribute.new(@relation, :name, :ancestor => Attribute.new(@relation, :name)).should be_match(Attribute.new(@relation, :name)) - Attribute.new(@relation, :name).should be_match(Attribute.new(@relation, :name, :ancestor => Attribute.new(@relation, :name))) + Attribute.new(@relation, :id, :ancestor => @attribute).should be_match(@attribute) + @attribute.should be_match(Attribute.new(@relation, :id, :ancestor => @attribute)) end end describe '/' do before do @aliased_relation = @relation.alias - @doubly_aliased_relation = @aliased_relation.alias.alias.alias.alias + @doubly_aliased_relation = @aliased_relation.alias end describe 'when dividing two identical attributes' do @@ -85,13 +84,6 @@ module Arel end end end - - describe 'hashing' do - it "implements hash equality" do - Attribute.new(@relation, 'name').should hash_the_same_as(Attribute.new(@relation, 'name')) - Attribute.new(@relation, 'name').should_not hash_the_same_as(Attribute.new(@relation, 'id')) - end - end end describe '#to_sql' do diff --git a/spec/arel/unit/relations/insertion_spec.rb b/spec/arel/unit/relations/insertion_spec.rb index 10b70a2036..0667aa665e 100644 --- a/spec/arel/unit/relations/insertion_spec.rb +++ b/spec/arel/unit/relations/insertion_spec.rb @@ -24,7 +24,7 @@ module Arel @insertion.to_sql.should be_like(" INSERT INTO `users` - (`users`.`id`, `users`.`name`) VALUES (1, 'nick') + (`users`.`name`, `users`.`id`) VALUES ('nick', 1) ") end diff --git a/spec/arel/unit/relations/update_spec.rb b/spec/arel/unit/relations/update_spec.rb index f411781392..976e88dddc 100644 --- a/spec/arel/unit/relations/update_spec.rb +++ b/spec/arel/unit/relations/update_spec.rb @@ -10,7 +10,7 @@ module Arel it "manufactures sql updating attributes when given multiple attributes" do Update.new(@relation, @relation[:id] => 1, @relation[:name] => "nick").to_sql.should be_like(" UPDATE `users` - SET `users`.`id` = 1, `users`.`name` = 'nick' + SET `users`.`name` = 'nick', `users`.`id` = 1 ") end |