diff options
author | Carl Lerche <carllerche@mac.com> | 2010-04-02 19:04:23 -0700 |
---|---|---|
committer | Carl Lerche <carllerche@mac.com> | 2010-04-02 19:04:23 -0700 |
commit | a46922e4a1089c9880c30b389e1e1d9dfbab02ae (patch) | |
tree | 2115e291ff1799d662bbb445b96060f2d029cc0d /lib/arel/algebra | |
parent | 233ee77f4511255ff2ff7c0b0ebf1cee13e7fc10 (diff) | |
download | rails-a46922e4a1089c9880c30b389e1e1d9dfbab02ae.tar.gz rails-a46922e4a1089c9880c30b389e1e1d9dfbab02ae.tar.bz2 rails-a46922e4a1089c9880c30b389e1e1d9dfbab02ae.zip |
Create an Arel::Header class representing a relation's attributes
Diffstat (limited to 'lib/arel/algebra')
-rw-r--r-- | lib/arel/algebra/attributes/attribute.rb | 2 | ||||
-rw-r--r-- | lib/arel/algebra/header.rb | 71 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/join.rb | 3 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/project.rb | 2 | ||||
-rw-r--r-- | lib/arel/algebra/relations/relation.rb | 40 | ||||
-rw-r--r-- | lib/arel/algebra/relations/utilities/compound.rb | 6 | ||||
-rw-r--r-- | lib/arel/algebra/relations/utilities/externalization.rb | 2 |
7 files changed, 99 insertions, 27 deletions
diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb index 331f218463..afcbdd8301 100644 --- a/lib/arel/algebra/attributes/attribute.rb +++ b/lib/arel/algebra/attributes/attribute.rb @@ -29,7 +29,7 @@ module Arel end def hash - @hash ||= history.size + name.hash + relation.hash + @hash ||= name.hash + root.relation.hash end def as(aliaz = nil) diff --git a/lib/arel/algebra/header.rb b/lib/arel/algebra/header.rb new file mode 100644 index 0000000000..ec45488dbf --- /dev/null +++ b/lib/arel/algebra/header.rb @@ -0,0 +1,71 @@ +module Arel + class Header + include Enumerable + + def initialize(attrs = []) + @attributes = attrs.to_ary + @names = Hash.new do |h,k| + h[k] = @attributes.detect { |a| a.named?(k) } + end + end + + def each(&block) + to_ary.each(&block) + self + end + + def [](key) + case key + when String, Symbol then find_by_name(key) + when Attribute then find_by_attribute(key) + end + end + + def ==(other) + to_set == other.to_set + end + + def union(other) + new(to_ary | other) + end + + alias | union + + def to_ary + @attributes + end + + def bind(relation) + Header.new(map { |a| a.bind(relation) }) + end + + # TMP + def index(i) + to_ary.index(i) + end + + private + + def new(attrs) + self.class.new(attrs) + end + + def matching(attribute) + # (@matching_attributes ||= attributes.inject({}) do |hash, a| + # (hash[a.is_a?(Value) ? a.value : a.root] ||= []) << a + # hash + # end)[attribute.root] || [] + select { |a| !a.is_a?(Value) && a.root == attribute.root } + end + + def find_by_name(name) + @names[name.to_sym] + end + + def find_by_attribute(attr) + matching(attr).max do |a, b| + (a.original_attribute / attr) <=> (b.original_attribute / attr) + end + end + end +end
\ No newline at end of file diff --git a/lib/arel/algebra/relations/operations/join.rb b/lib/arel/algebra/relations/operations/join.rb index 300cd31bcd..21bcfaa62d 100644 --- a/lib/arel/algebra/relations/operations/join.rb +++ b/lib/arel/algebra/relations/operations/join.rb @@ -19,8 +19,7 @@ module Arel end def attributes - @attributes ||= (relation1.externalize.attributes + - relation2.externalize.attributes).collect { |a| a.bind(self) } + @attributes ||= (relation1.externalize.attributes | relation2.externalize.attributes).bind(self) end def wheres diff --git a/lib/arel/algebra/relations/operations/project.rb b/lib/arel/algebra/relations/operations/project.rb index a1140e91c1..49d0e1be36 100644 --- a/lib/arel/algebra/relations/operations/project.rb +++ b/lib/arel/algebra/relations/operations/project.rb @@ -10,7 +10,7 @@ module Arel end def attributes - @attributes ||= projections.collect { |p| p.bind(self) } + @attributes ||= Header.new(projections).bind(self) end def externalizable? diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index 1c1ded15c9..ef2108dcaa 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -84,16 +84,14 @@ module Arel module AttributeAccessable def [](index) - @cached_attributes ||= {} - @cached_attributes[index] ||= case index - when Symbol, String - find_attribute_matching_name(index) - when Attribute, Expression - find_attribute_matching_attribute(index) - when ::Array - # TESTME - index.collect { |i| self[i] } + attr = attributes[index] + + # Handles a strange ActiveRecord case + if !attr && (index.is_a?(String) || index.is_a?(Symbol)) + attr = Attribute.new(self, index) end + + attr end def find_attribute_matching_name(name) @@ -127,18 +125,18 @@ module Arel include AttributeAccessable module DefaultOperations - def attributes; [] end - def projections; [] end - def wheres; [] end - def orders; [] end - def inserts; [] end - def groupings; [] end - def havings; [] end - def joins(formatter = nil); nil end # FIXME - def taken; nil end - def skipped; nil end - def sources; [] end - def locked; [] end + def attributes; Header.new end + def projections; [] end + def wheres; [] end + def orders; [] end + def inserts; [] end + def groupings; [] end + def havings; [] end + def joins(formatter = nil); nil end # FIXME + def taken; nil end + def skipped; nil end + def sources; [] end + def locked; [] end end include DefaultOperations end diff --git a/lib/arel/algebra/relations/utilities/compound.rb b/lib/arel/algebra/relations/utilities/compound.rb index 7039b82575..416717310c 100644 --- a/lib/arel/algebra/relations/utilities/compound.rb +++ b/lib/arel/algebra/relations/utilities/compound.rb @@ -13,7 +13,7 @@ module Arel @requires end - [:attributes, :wheres, :groupings, :orders, :havings, :projections].each do |operation_name| + [:wheres, :groupings, :orders, :havings, :projections].each do |operation_name| class_eval <<-OPERATION, __FILE__, __LINE__ def #{operation_name} @#{operation_name} ||= relation.#{operation_name}.collect { |o| o.bind(self) } @@ -21,6 +21,10 @@ module Arel OPERATION end + def attributes + @attributes ||= relation.attributes.bind(self) + end + def hash @hash ||= :relation.hash end diff --git a/lib/arel/algebra/relations/utilities/externalization.rb b/lib/arel/algebra/relations/utilities/externalization.rb index 795a3919f2..edd8f99221 100644 --- a/lib/arel/algebra/relations/utilities/externalization.rb +++ b/lib/arel/algebra/relations/utilities/externalization.rb @@ -8,7 +8,7 @@ module Arel end def attributes - @attributes ||= relation.attributes.collect { |a| a.to_attribute(self) } + @attributes ||= Header.new(relation.attributes.map { |a| a.to_attribute(self) }) end end |