From 71d65b7571d5ff01e0de3c08a9396dc1e1386118 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 30 Sep 2009 23:35:23 -0400 Subject: Pull primitives up one level --- lib/arel/algebra.rb | 7 +- lib/arel/algebra/attribute.rb | 150 ++++++++++++++++++++++++++++++ lib/arel/algebra/expression.rb | 43 +++++++++ lib/arel/algebra/ordering.rb | 23 +++++ lib/arel/algebra/primitives.rb | 5 - lib/arel/algebra/primitives/attribute.rb | 150 ------------------------------ lib/arel/algebra/primitives/expression.rb | 43 --------- lib/arel/algebra/primitives/ordering.rb | 23 ----- lib/arel/algebra/primitives/value.rb | 14 --- lib/arel/algebra/value.rb | 14 +++ 10 files changed, 236 insertions(+), 236 deletions(-) create mode 100644 lib/arel/algebra/attribute.rb create mode 100644 lib/arel/algebra/expression.rb create mode 100644 lib/arel/algebra/ordering.rb delete mode 100644 lib/arel/algebra/primitives.rb delete mode 100644 lib/arel/algebra/primitives/attribute.rb delete mode 100644 lib/arel/algebra/primitives/expression.rb delete mode 100644 lib/arel/algebra/primitives/ordering.rb delete mode 100644 lib/arel/algebra/primitives/value.rb create mode 100644 lib/arel/algebra/value.rb (limited to 'lib') diff --git a/lib/arel/algebra.rb b/lib/arel/algebra.rb index 319f8fc242..980c558918 100644 --- a/lib/arel/algebra.rb +++ b/lib/arel/algebra.rb @@ -1,4 +1,9 @@ require 'arel/algebra/core_extensions' + +require 'arel/algebra/attribute' +require 'arel/algebra/expression' +require 'arel/algebra/ordering' require 'arel/algebra/predicates' require 'arel/algebra/relations' -require 'arel/algebra/primitives' +require 'arel/algebra/value' + diff --git a/lib/arel/algebra/attribute.rb b/lib/arel/algebra/attribute.rb new file mode 100644 index 0000000000..40a7d61a53 --- /dev/null +++ b/lib/arel/algebra/attribute.rb @@ -0,0 +1,150 @@ +require 'set' + +module Arel + class Attribute + attributes :relation, :name, :alias, :ancestor + deriving :== + delegate :engine, :christener, :to => :relation + + def initialize(relation, name, options = {}) + @relation, @name, @alias, @ancestor = relation, name, options[:alias], options[:ancestor] + end + + def named?(hypothetical_name) + (@alias || name).to_s == hypothetical_name.to_s + end + + def aggregation? + false + end + + def inspect + "" + end + + module Transformations + def self.included(klass) + klass.send :alias_method, :eql?, :== + end + + def hash + @hash ||= history.size + name.hash + relation.hash + end + + def as(aliaz = nil) + Attribute.new(relation, name, :alias => aliaz, :ancestor => self) + end + + def bind(new_relation) + relation == new_relation ? self : Attribute.new(new_relation, name, :alias => @alias, :ancestor => self) + end + + def to_attribute(relation) + bind(relation) + end + end + include Transformations + + module Congruence + def history + @history ||= [self] + (ancestor ? ancestor.history : []) + end + + def join? + relation.join? + end + + def root + history.last + end + + def original_relation + @original_relation ||= original_attribute.relation + end + + def original_attribute + @original_attribute ||= history.detect { |a| !a.join? } + end + + def find_correlate_in(relation) + relation[self] || self + end + + def descends_from?(other) + history.include?(other) + end + + def /(other) + other ? (history & other.history).size : 0 + end + end + include Congruence + + module Predications + def eq(other) + Predicates::Equality.new(self, other) + end + + def lt(other) + Predicates::LessThan.new(self, other) + end + + def lteq(other) + Predicates::LessThanOrEqualTo.new(self, other) + end + + def gt(other) + Predicates::GreaterThan.new(self, other) + end + + def gteq(other) + Predicates::GreaterThanOrEqualTo.new(self, other) + end + + def matches(regexp) + Predicates::Match.new(self, regexp) + end + + def in(array) + Predicates::In.new(self, array) + end + end + include Predications + + module Expressions + def count(distinct = false) + distinct ? Distinct.new(self).count : Count.new(self) + end + + def sum + Sum.new(self) + end + + def maximum + Maximum.new(self) + end + + def minimum + Minimum.new(self) + end + + def average + Average.new(self) + end + end + include Expressions + + module Orderings + def asc + Ascending.new(self) + end + + def desc + Descending.new(self) + end + + alias_method :to_ordering, :asc + end + include Orderings + end +end diff --git a/lib/arel/algebra/expression.rb b/lib/arel/algebra/expression.rb new file mode 100644 index 0000000000..875498c282 --- /dev/null +++ b/lib/arel/algebra/expression.rb @@ -0,0 +1,43 @@ +module Arel + class Expression < Attribute + attributes :attribute, :alias, :ancestor + deriving :== + delegate :relation, :to => :attribute + alias_method :name, :alias + + def initialize(attribute, aliaz = nil, ancestor = nil) + @attribute, @alias, @ancestor = attribute, aliaz, ancestor + end + + def aggregation? + true + end + + def inspect + "<#{self.class.name} #{attribute.inspect}>" + end + + module Transformations + def as(aliaz) + self.class.new(attribute, aliaz, self) + end + + def bind(new_relation) + new_relation == relation ? self : self.class.new(attribute.bind(new_relation), @alias, self) + end + + def to_attribute(relation) + Attribute.new(relation, @alias, :ancestor => self) + end + end + include Transformations + end + + class Count < Expression; end + class Distinct < Expression; end + class Sum < Expression; end + class Maximum < Expression; end + class Minimum < Expression; end + class Average < Expression; end +end + diff --git a/lib/arel/algebra/ordering.rb b/lib/arel/algebra/ordering.rb new file mode 100644 index 0000000000..8439e937a9 --- /dev/null +++ b/lib/arel/algebra/ordering.rb @@ -0,0 +1,23 @@ +module Arel + class Ordering + delegate :relation, :to => :attribute + + def bind(relation) + self.class.new(attribute.bind(relation)) + end + + def to_ordering + self + end + end + + class Ascending < Ordering + attributes :attribute + deriving :initialize, :== + end + + class Descending < Ordering + attributes :attribute + deriving :initialize, :== + end +end diff --git a/lib/arel/algebra/primitives.rb b/lib/arel/algebra/primitives.rb deleted file mode 100644 index df8d16a5d5..0000000000 --- a/lib/arel/algebra/primitives.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'arel/algebra/primitives/attribute' -require 'arel/algebra/primitives/ordering' -require 'arel/algebra/primitives/value' -require 'arel/algebra/primitives/expression' - diff --git a/lib/arel/algebra/primitives/attribute.rb b/lib/arel/algebra/primitives/attribute.rb deleted file mode 100644 index 40a7d61a53..0000000000 --- a/lib/arel/algebra/primitives/attribute.rb +++ /dev/null @@ -1,150 +0,0 @@ -require 'set' - -module Arel - class Attribute - attributes :relation, :name, :alias, :ancestor - deriving :== - delegate :engine, :christener, :to => :relation - - def initialize(relation, name, options = {}) - @relation, @name, @alias, @ancestor = relation, name, options[:alias], options[:ancestor] - end - - def named?(hypothetical_name) - (@alias || name).to_s == hypothetical_name.to_s - end - - def aggregation? - false - end - - def inspect - "" - end - - module Transformations - def self.included(klass) - klass.send :alias_method, :eql?, :== - end - - def hash - @hash ||= history.size + name.hash + relation.hash - end - - def as(aliaz = nil) - Attribute.new(relation, name, :alias => aliaz, :ancestor => self) - end - - def bind(new_relation) - relation == new_relation ? self : Attribute.new(new_relation, name, :alias => @alias, :ancestor => self) - end - - def to_attribute(relation) - bind(relation) - end - end - include Transformations - - module Congruence - def history - @history ||= [self] + (ancestor ? ancestor.history : []) - end - - def join? - relation.join? - end - - def root - history.last - end - - def original_relation - @original_relation ||= original_attribute.relation - end - - def original_attribute - @original_attribute ||= history.detect { |a| !a.join? } - end - - def find_correlate_in(relation) - relation[self] || self - end - - def descends_from?(other) - history.include?(other) - end - - def /(other) - other ? (history & other.history).size : 0 - end - end - include Congruence - - module Predications - def eq(other) - Predicates::Equality.new(self, other) - end - - def lt(other) - Predicates::LessThan.new(self, other) - end - - def lteq(other) - Predicates::LessThanOrEqualTo.new(self, other) - end - - def gt(other) - Predicates::GreaterThan.new(self, other) - end - - def gteq(other) - Predicates::GreaterThanOrEqualTo.new(self, other) - end - - def matches(regexp) - Predicates::Match.new(self, regexp) - end - - def in(array) - Predicates::In.new(self, array) - end - end - include Predications - - module Expressions - def count(distinct = false) - distinct ? Distinct.new(self).count : Count.new(self) - end - - def sum - Sum.new(self) - end - - def maximum - Maximum.new(self) - end - - def minimum - Minimum.new(self) - end - - def average - Average.new(self) - end - end - include Expressions - - module Orderings - def asc - Ascending.new(self) - end - - def desc - Descending.new(self) - end - - alias_method :to_ordering, :asc - end - include Orderings - end -end diff --git a/lib/arel/algebra/primitives/expression.rb b/lib/arel/algebra/primitives/expression.rb deleted file mode 100644 index 875498c282..0000000000 --- a/lib/arel/algebra/primitives/expression.rb +++ /dev/null @@ -1,43 +0,0 @@ -module Arel - class Expression < Attribute - attributes :attribute, :alias, :ancestor - deriving :== - delegate :relation, :to => :attribute - alias_method :name, :alias - - def initialize(attribute, aliaz = nil, ancestor = nil) - @attribute, @alias, @ancestor = attribute, aliaz, ancestor - end - - def aggregation? - true - end - - def inspect - "<#{self.class.name} #{attribute.inspect}>" - end - - module Transformations - def as(aliaz) - self.class.new(attribute, aliaz, self) - end - - def bind(new_relation) - new_relation == relation ? self : self.class.new(attribute.bind(new_relation), @alias, self) - end - - def to_attribute(relation) - Attribute.new(relation, @alias, :ancestor => self) - end - end - include Transformations - end - - class Count < Expression; end - class Distinct < Expression; end - class Sum < Expression; end - class Maximum < Expression; end - class Minimum < Expression; end - class Average < Expression; end -end - diff --git a/lib/arel/algebra/primitives/ordering.rb b/lib/arel/algebra/primitives/ordering.rb deleted file mode 100644 index 8439e937a9..0000000000 --- a/lib/arel/algebra/primitives/ordering.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Arel - class Ordering - delegate :relation, :to => :attribute - - def bind(relation) - self.class.new(attribute.bind(relation)) - end - - def to_ordering - self - end - end - - class Ascending < Ordering - attributes :attribute - deriving :initialize, :== - end - - class Descending < Ordering - attributes :attribute - deriving :initialize, :== - end -end diff --git a/lib/arel/algebra/primitives/value.rb b/lib/arel/algebra/primitives/value.rb deleted file mode 100644 index e363805140..0000000000 --- a/lib/arel/algebra/primitives/value.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Arel - class Value - attributes :value, :relation - deriving :initialize, :== - - def bind(relation) - Value.new(value, relation) - end - - def to_ordering - self - end - end -end diff --git a/lib/arel/algebra/value.rb b/lib/arel/algebra/value.rb new file mode 100644 index 0000000000..e363805140 --- /dev/null +++ b/lib/arel/algebra/value.rb @@ -0,0 +1,14 @@ +module Arel + class Value + attributes :value, :relation + deriving :initialize, :== + + def bind(relation) + Value.new(value, relation) + end + + def to_ordering + self + end + end +end -- cgit v1.2.3