From 2d021c641ab8c9215df863531cfb0d8ff8b9554a Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Mon, 19 May 2008 19:38:36 -0700 Subject: [PATCH] removed more boiler-plate --- lib/arel/primitives/attribute.rb | 37 +++++++++------------ lib/arel/primitives/expression.rb | 11 ++---- lib/arel/primitives/value.rb | 12 ++----- lib/arel/relations/operations/join.rb | 2 +- lib/arel/relations/utilities/aggregation.rb | 11 ++---- lib/arel/relations/utilities/nil.rb | 6 ++-- lib/arel/relations/writes/delete.rb | 10 ++---- lib/arel/relations/writes/insert.rb | 9 ++--- lib/arel/relations/writes/update.rb | 11 ++---- 9 files changed, 31 insertions(+), 78 deletions(-) diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index ca1d31ffcf..dffe24d121 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -2,7 +2,8 @@ require 'set' module Arel class Attribute - attr_reader :relation, :name, :alias, :ancestor + attributes :relation, :name, :alias, :ancestor + deriving :== delegate :engine, :christener, :to => :relation def initialize(relation, name, options = {}) @@ -28,26 +29,6 @@ module Arel def to_sql(formatter = Sql::WhereCondition.new(relation)) formatter.attribute self end - - def ==(other) - Attribute === other and - name == other.name and - @alias == other.alias and - ancestor == other.ancestor and - relation == other.relation - 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] - end module Transformations def self.included(klass) @@ -80,11 +61,23 @@ module Arel 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] + end + def descends_from?(other) history.include?(other) end diff --git a/lib/arel/primitives/expression.rb b/lib/arel/primitives/expression.rb index 9afafcce2f..26186ccad8 100644 --- a/lib/arel/primitives/expression.rb +++ b/lib/arel/primitives/expression.rb @@ -1,6 +1,7 @@ module Arel class Expression < Attribute - attr_reader :attribute, :function_sql + attributes :attribute, :function_sql, :alias, :ancestor + deriving :== delegate :relation, :to => :attribute alias_method :name, :alias @@ -16,14 +17,6 @@ module Arel true end - def ==(other) - Expression === other and - attribute == other.attribute and - function_sql == other.function_sql and - ancestor == other.ancestor and - @alias == other.alias - end - module Transformations def as(aliaz) Expression.new(attribute, function_sql, aliaz, self) diff --git a/lib/arel/primitives/value.rb b/lib/arel/primitives/value.rb index 4509f13d17..809af7c206 100644 --- a/lib/arel/primitives/value.rb +++ b/lib/arel/primitives/value.rb @@ -1,12 +1,9 @@ module Arel class Value - attr_reader :value, :relation - + attributes :value, :relation + deriving :initialize, :== delegate :inclusion_predicate_sql, :equality_predicate_sql, :to => :value - def initialize(value, relation) - @value, @relation = value, relation - end def to_sql(formatter = Sql::WhereCondition.new(relation)) formatter.value value @@ -16,11 +13,6 @@ module Arel object.to_sql(Sql::Value.new(relation)) end - def ==(other) - Value === other and - value == other.value - end - def bind(relation) Value.new(value, relation) end diff --git a/lib/arel/relations/operations/join.rb b/lib/arel/relations/operations/join.rb index 01fa6da255..243a0289c7 100644 --- a/lib/arel/relations/operations/join.rb +++ b/lib/arel/relations/operations/join.rb @@ -5,7 +5,7 @@ module Arel delegate :engine, :name, :to => :relation1 hash_on :relation1 - def initialize(join_sql, relation1, relation2 = Nil.new, *predicates) + def initialize(join_sql, relation1, relation2 = Nil.instance, *predicates) @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates end diff --git a/lib/arel/relations/utilities/aggregation.rb b/lib/arel/relations/utilities/aggregation.rb index 66150bff0a..9f5ead8f86 100644 --- a/lib/arel/relations/utilities/aggregation.rb +++ b/lib/arel/relations/utilities/aggregation.rb @@ -1,11 +1,9 @@ module Arel class Aggregation < Compound + attributes :relation + deriving :initialize, :== include Recursion::BaseCase - def initialize(relation) - @relation = relation - end - def wheres [] end @@ -21,11 +19,6 @@ module Arel def name relation.name + '_aggregation' end - - def ==(other) - Aggregation === other and - self.relation == other.relation - end end class Relation diff --git a/lib/arel/relations/utilities/nil.rb b/lib/arel/relations/utilities/nil.rb index 2dcfb47233..2b8dc570f8 100644 --- a/lib/arel/relations/utilities/nil.rb +++ b/lib/arel/relations/utilities/nil.rb @@ -1,10 +1,8 @@ module Arel class Nil < Relation + include Singleton + def table_sql(formatter = nil); '' end def name; '' end - - def ==(other) - Nil === other - end end end \ No newline at end of file diff --git a/lib/arel/relations/writes/delete.rb b/lib/arel/relations/writes/delete.rb index 2eaad6d1da..e72679c4d6 100644 --- a/lib/arel/relations/writes/delete.rb +++ b/lib/arel/relations/writes/delete.rb @@ -1,8 +1,7 @@ module Arel class Deletion < Compound - def initialize(relation) - @relation = relation - end + attributes :relation + deriving :initialize, :== def to_sql(formatter = nil) [ @@ -16,10 +15,5 @@ module Arel def call(connection = engine.connection) connection.delete(to_sql) end - - def ==(other) - Deletion === other and - relation == other.relation - end end end \ No newline at end of file diff --git a/lib/arel/relations/writes/insert.rb b/lib/arel/relations/writes/insert.rb index a1c4c93de5..0545d7faa3 100644 --- a/lib/arel/relations/writes/insert.rb +++ b/lib/arel/relations/writes/insert.rb @@ -1,6 +1,7 @@ module Arel class Insert < Compound - attr_reader :record + attributes :relation, :record + deriving :== def initialize(relation, record) @relation, @record = relation, record.bind(relation) @@ -18,11 +19,5 @@ module Arel def call(connection = engine.connection) connection.insert(to_sql) end - - def ==(other) - Insert === other and - relation == other.relation and - record == other.record - end end end \ No newline at end of file diff --git a/lib/arel/relations/writes/update.rb b/lib/arel/relations/writes/update.rb index 760f4e931f..18b7ad9de1 100644 --- a/lib/arel/relations/writes/update.rb +++ b/lib/arel/relations/writes/update.rb @@ -1,7 +1,8 @@ module Arel class Update < Compound - attr_reader :assignments - + attributes :relation, :assignments + deriving :== + def initialize(relation, assignments) @relation, @assignments = relation, assignments.bind(relation) end @@ -20,11 +21,5 @@ module Arel def call(connection = engine.connection) connection.update(to_sql) end - - def ==(other) - Update === other and - relation == other.relation and - assignments == other.assignments - end end end \ No newline at end of file