From a29ceffc9476c99ff02f0617d2e38627c526bac2 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Tue, 11 Mar 2008 22:42:47 -0700 Subject: implemented hashing macro; implemented custom matcher testing this macro --- lib/active_relation/.DS_Store | Bin 6148 -> 6148 bytes lib/active_relation/extensions/object.rb | 10 +++++++++- lib/active_relation/primitives/attribute.rb | 5 +---- lib/active_relation/relations/relation.rb | 4 ---- lib/active_relation/relations/table.rb | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) (limited to 'lib/active_relation') diff --git a/lib/active_relation/.DS_Store b/lib/active_relation/.DS_Store index 2a449ff62e..9918127870 100644 Binary files a/lib/active_relation/.DS_Store and b/lib/active_relation/.DS_Store differ diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index d13cf9aabb..c1269ee37b 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -1,4 +1,12 @@ -class Object +class Object + def self.hash_on(delegatee) + def eql?(other) + self == other + end + + delegate :hash, :to => delegatee + end + def bind(relation) ActiveRelation::Scalar.new(self, relation) end diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index baaae1973c..20677f824b 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -48,10 +48,7 @@ module ActiveRelation module Congruence def self.included(klass) - klass.class_eval do - alias_method :eql?, :== - delegate :hash, :to => :name - end + klass.hash_on :name end def history diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 1c97cc7035..f9823bf92d 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -102,10 +102,6 @@ module ActiveRelation false end - def eql?(other) - self == other - end - def to_sql(strategy = Sql::Relation.new(engine)) strategy.select [ "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new(engine)) }.join(', ')}", diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index 84eb1213ee..4682298608 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -3,7 +3,7 @@ module ActiveRelation cattr_accessor :engine attr_reader :name, :engine - delegate :hash, :to => :name + hash_on :name def initialize(name, engine = Table.engine) @name, @engine = name.to_s, engine -- cgit v1.2.3 From 12ef6a5ad15078d2f634d3c6cdfc453848f90122 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Tue, 11 Mar 2008 23:22:26 -0700 Subject: refactored session's interaction with engine/connection - follows law of demeter - Table.engine uses AR::Base adapter --- lib/active_relation/relations/deletion.rb | 4 ++++ lib/active_relation/relations/insertion.rb | 4 ++++ lib/active_relation/relations/relation.rb | 4 ++++ lib/active_relation/relations/update.rb | 4 ++++ lib/active_relation/sessions/session.rb | 8 ++++---- 5 files changed, 20 insertions(+), 4 deletions(-) (limited to 'lib/active_relation') diff --git a/lib/active_relation/relations/deletion.rb b/lib/active_relation/relations/deletion.rb index f3d81baf27..c5906e8bc8 100644 --- a/lib/active_relation/relations/deletion.rb +++ b/lib/active_relation/relations/deletion.rb @@ -12,6 +12,10 @@ module ActiveRelation ].compact.join("\n") end + def call(connection = engine.connection) + connection.delete(to_sql) + end + def ==(other) self.class == other.class and relation == other.relation diff --git a/lib/active_relation/relations/insertion.rb b/lib/active_relation/relations/insertion.rb index 16fe3d5f46..036db1a319 100644 --- a/lib/active_relation/relations/insertion.rb +++ b/lib/active_relation/relations/insertion.rb @@ -15,6 +15,10 @@ module ActiveRelation ].join("\n") end + def call(connection = engine.connection) + connection.insert(to_sql) + end + def ==(other) self.class == other.class and relation == other.relation and diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index f9823bf92d..69935a7be0 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -115,6 +115,10 @@ module ActiveRelation ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql + + def call(connection = engine.connection) + connection.select_all(to_sql) + end def attribute_for_name(name) attributes.detect { |a| a.alias_or_name.to_s == name.to_s } diff --git a/lib/active_relation/relations/update.rb b/lib/active_relation/relations/update.rb index c50919af3e..0914cda035 100644 --- a/lib/active_relation/relations/update.rb +++ b/lib/active_relation/relations/update.rb @@ -16,6 +16,10 @@ module ActiveRelation ].join("\n") end + def call(connection = engine.connection) + connection.update(to_sql) + end + def ==(other) self.class == other.class and relation == other.relation and diff --git a/lib/active_relation/sessions/session.rb b/lib/active_relation/sessions/session.rb index 4bdbe69978..fe917a0e4d 100644 --- a/lib/active_relation/sessions/session.rb +++ b/lib/active_relation/sessions/session.rb @@ -25,22 +25,22 @@ module ActiveRelation module CRUD def create(insert) - insert.engine.insert(insert.to_sql) + insert.call(insert.engine.connection) end def read(select) @read ||= Hash.new do |hash, select| - hash[select] = select.engine.select_all(select.to_sql) + hash[select] = select.call(select.engine.connection) end @read[select] end def update(update) - update.engine.update(update.to_sql) + update.call(update.engine.connection) end def delete(delete) - delete.engine.delete(delete.to_sql) + delete.call(delete.engine.connection) end end include CRUD -- cgit v1.2.3 From 8a62a733c107a33edee8cabc908c3ebc3b280e8b Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Tue, 11 Mar 2008 23:39:40 -0700 Subject: renamed strategy method to format - strategy (the method on scalar and attribute) is a complex double-dispatching scheme to format (to_sql) a scalar in the light of the particular attribute; that is, it casts strings to integers if the column is int, etc. --- lib/active_relation/predicates.rb | 2 +- lib/active_relation/primitives/attribute.rb | 8 ++++++-- lib/active_relation/primitives/scalar.rb | 6 +++--- lib/active_relation/relations/relation.rb | 4 ++-- lib/active_relation/sql.rb | 14 +++++++------- 5 files changed, 19 insertions(+), 15 deletions(-) (limited to 'lib/active_relation') diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index ba926a86e5..12ddd1b48d 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -25,7 +25,7 @@ module ActiveRelation end def to_sql(strategy = nil) - "#{operand1.to_sql(operand2.strategy)} #{predicate_sql} #{operand2.to_sql(operand1.strategy)}" + "#{operand2.format(operand1)} #{predicate_sql} #{operand1.format(operand2)}" end def descend diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index 20677f824b..bbea7b4554 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -111,15 +111,19 @@ module ActiveRelation end include Expressions - def to_sql(strategy = self.strategy) + def to_sql(strategy = Sql::Predicate.new(engine)) strategy.attribute prefix, name, self.alias end + def format(object) + object.to_sql(strategy) + end + + private def strategy Sql::Attribute.new(self) end - private def prefix relation.prefix_for(self) end diff --git a/lib/active_relation/primitives/scalar.rb b/lib/active_relation/primitives/scalar.rb index fa88404ee3..d428541a50 100644 --- a/lib/active_relation/primitives/scalar.rb +++ b/lib/active_relation/primitives/scalar.rb @@ -6,12 +6,12 @@ module ActiveRelation @value, @relation = value, relation end - def to_sql(strategy = self.strategy) + def to_sql(strategy = Sql::Predicate.new(relation.engine)) strategy.scalar value end - def strategy - ActiveRelation::Sql::Scalar.new(relation.engine) + def format(object) + object.to_sql(Sql::Scalar.new(relation.engine)) end def ==(other) diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 69935a7be0..ee2e00aa21 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -132,8 +132,8 @@ module ActiveRelation self end - def strategy - Sql::Predicate.new(engine) + def format(object) + object.to_sql(Sql::Predicate.new(engine)) end def attributes; [] end diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 99cfc66383..ff00223ce7 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -4,8 +4,8 @@ module ActiveRelation delegate :quote_table_name, :quote_column_name, :quote, :to => :engine end - # module Formatting Context / Strategy # unit test me!!! - class Strategy + # unit test me!!! + class Formatter attr_reader :engine include Quoting @@ -14,7 +14,7 @@ module ActiveRelation end end - class Projection < Strategy + class Projection < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" + (aliaz ? " AS #{quote(aliaz.to_s)}" : "") end @@ -24,7 +24,7 @@ module ActiveRelation end end - class Predicate < Strategy + class Predicate < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" end @@ -38,19 +38,19 @@ module ActiveRelation end end - class Selection < Strategy + class Selection < Formatter def scalar(scalar) scalar end end - class Relation < Strategy + class Relation < Formatter def select(select_sql, aliaz) select_sql end end - class Aggregation < Strategy + class Aggregation < Formatter def select(select_sql, aliaz) "(#{select_sql}) AS #{engine.quote_table_name(aliaz)}" end -- cgit v1.2.3 From b1acebaaf0823c093853ade5700bbf5117b4f31a Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Wed, 12 Mar 2008 23:31:18 -0700 Subject: - renamed scalar to value - added better test coverage and documentation of binary spec #to_sql --- lib/active_relation/extensions/array.rb | 2 +- lib/active_relation/extensions/object.rb | 2 +- lib/active_relation/primitives.rb | 2 +- lib/active_relation/primitives/scalar.rb | 25 ------------------------- lib/active_relation/primitives/value.rb | 25 +++++++++++++++++++++++++ lib/active_relation/sql.rb | 14 +++++++------- 6 files changed, 35 insertions(+), 35 deletions(-) delete mode 100644 lib/active_relation/primitives/scalar.rb create mode 100644 lib/active_relation/primitives/value.rb (limited to 'lib/active_relation') diff --git a/lib/active_relation/extensions/array.rb b/lib/active_relation/extensions/array.rb index aa4354a78a..4bd20d8121 100644 --- a/lib/active_relation/extensions/array.rb +++ b/lib/active_relation/extensions/array.rb @@ -2,7 +2,7 @@ class Array def to_hash Hash[*flatten] end - + def to_sql(strategy = nil) "(#{collect(&:to_sql).join(', ')})" end diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index c1269ee37b..ab874150ed 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -8,7 +8,7 @@ class Object end def bind(relation) - ActiveRelation::Scalar.new(self, relation) + ActiveRelation::Value.new(self, relation) end def metaclass diff --git a/lib/active_relation/primitives.rb b/lib/active_relation/primitives.rb index 7629256034..9909734d24 100644 --- a/lib/active_relation/primitives.rb +++ b/lib/active_relation/primitives.rb @@ -1,4 +1,4 @@ require 'active_relation/primitives/attribute' -require 'active_relation/primitives/scalar' +require 'active_relation/primitives/value' require 'active_relation/primitives/expression' diff --git a/lib/active_relation/primitives/scalar.rb b/lib/active_relation/primitives/scalar.rb deleted file mode 100644 index d428541a50..0000000000 --- a/lib/active_relation/primitives/scalar.rb +++ /dev/null @@ -1,25 +0,0 @@ -module ActiveRelation - class Scalar - attr_reader :value, :relation - - def initialize(value, relation) - @value, @relation = value, relation - end - - def to_sql(strategy = Sql::Predicate.new(relation.engine)) - strategy.scalar value - end - - def format(object) - object.to_sql(Sql::Scalar.new(relation.engine)) - end - - def ==(other) - value == other.value - end - - def qualify - self - end - end -end \ No newline at end of file diff --git a/lib/active_relation/primitives/value.rb b/lib/active_relation/primitives/value.rb new file mode 100644 index 0000000000..ce9497cf34 --- /dev/null +++ b/lib/active_relation/primitives/value.rb @@ -0,0 +1,25 @@ +module ActiveRelation + class Value + attr_reader :value, :relation + + def initialize(value, relation) + @value, @relation = value, relation + end + + def to_sql(strategy = Sql::Predicate.new(relation.engine)) + strategy.value value + end + + def format(object) + object.to_sql(Sql::Value.new(relation.engine)) + end + + def ==(other) + value == other.value + end + + def qualify + self + end + end +end \ No newline at end of file diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index ff00223ce7..fb2177a55b 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -29,8 +29,8 @@ module ActiveRelation "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" end - def scalar(scalar, column = nil) - quote(scalar, column) + def value(value, column = nil) + quote(value, column) end def select(select_sql, aliaz) @@ -39,8 +39,8 @@ module ActiveRelation end class Selection < Formatter - def scalar(scalar) - scalar + def value(value) + value end end @@ -61,12 +61,12 @@ module ActiveRelation @attribute, @engine = attribute, attribute.engine end - def scalar(scalar) - quote(scalar, @attribute.column) + def value(value) + quote(value, @attribute.column) end end - class Scalar < Predicate + class Value < Predicate end end end \ No newline at end of file -- cgit v1.2.3 From c823c2c2cc3e5d0a709d67e585e73fde0f11512f Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Thu, 13 Mar 2008 22:39:51 -0700 Subject: renamed sql formatting strategies to correspond with sql grammar rule names in the mysql bnf --- lib/active_relation/primitives/attribute.rb | 2 +- lib/active_relation/primitives/value.rb | 2 +- lib/active_relation/relations/join.rb | 2 +- lib/active_relation/relations/relation.rb | 18 +++++++++--------- lib/active_relation/sql.rb | 27 +++++++++++++-------------- 5 files changed, 25 insertions(+), 26 deletions(-) (limited to 'lib/active_relation') diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index bbea7b4554..0fed676727 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -111,7 +111,7 @@ module ActiveRelation end include Expressions - def to_sql(strategy = Sql::Predicate.new(engine)) + def to_sql(strategy = Sql::WhereCondition.new(engine)) strategy.attribute prefix, name, self.alias end diff --git a/lib/active_relation/primitives/value.rb b/lib/active_relation/primitives/value.rb index ce9497cf34..096c876ecd 100644 --- a/lib/active_relation/primitives/value.rb +++ b/lib/active_relation/primitives/value.rb @@ -6,7 +6,7 @@ module ActiveRelation @value, @relation = value, relation end - def to_sql(strategy = Sql::Predicate.new(relation.engine)) + def to_sql(strategy = Sql::WhereCondition.new(relation.engine)) strategy.value value end diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 043b237de7..4a57795280 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -61,7 +61,7 @@ module ActiveRelation delegate :engine, :to => :relation def table_sql - relation.aggregation?? relation.to_sql(Sql::Aggregation.new(engine)) : relation.send(:table_sql) + relation.aggregation?? relation.to_sql(Sql::TableReference.new(engine)) : relation.send(:table_sql) end def selects diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index ee2e00aa21..9847cf2b7c 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -102,16 +102,16 @@ module ActiveRelation false end - def to_sql(strategy = Sql::Relation.new(engine)) + def to_sql(strategy = Sql::SelectStatement.new(engine)) strategy.select [ - "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new(engine)) }.join(', ')}", + "SELECT #{attributes.collect{ |a| a.to_sql(Sql::SelectExpression.new(engine)) }.join(', ')}", "FROM #{table_sql}", - (joins unless joins.blank? ), - ("WHERE #{selects.collect{|s| s.to_sql(Sql::Selection.new(engine))}.join("\n\tAND ")}" unless selects.blank? ), - ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank? ), - ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), - ("LIMIT #{limit}" unless limit.blank? ), - ("OFFSET #{offset}" unless offset.blank? ) + (joins unless joins.blank? ), + ("WHERE #{selects.collect{|s| s.to_sql(Sql::WhereClause.new(engine))}.join("\n\tAND ")}" unless selects.blank? ), + ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank? ), + ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), + ("LIMIT #{limit}" unless limit.blank? ), + ("OFFSET #{offset}" unless offset.blank? ) ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql @@ -133,7 +133,7 @@ module ActiveRelation end def format(object) - object.to_sql(Sql::Predicate.new(engine)) + object.to_sql(Sql::WhereCondition.new(engine)) end def attributes; [] end diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index fb2177a55b..b38e6dc392 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -4,7 +4,6 @@ module ActiveRelation delegate :quote_table_name, :quote_column_name, :quote, :to => :engine end - # unit test me!!! class Formatter attr_reader :engine include Quoting @@ -14,7 +13,7 @@ module ActiveRelation end end - class Projection < Formatter + class SelectExpression < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" + (aliaz ? " AS #{quote(aliaz.to_s)}" : "") end @@ -24,7 +23,13 @@ module ActiveRelation end end - class Predicate < Formatter + class WhereClause < Formatter + def value(value) + value + end + end + + class WhereCondition < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" end @@ -38,25 +43,19 @@ module ActiveRelation end end - class Selection < Formatter - def value(value) - value - end - end - - class Relation < Formatter + class SelectStatement < Formatter def select(select_sql, aliaz) select_sql end end - class Aggregation < Formatter + class TableReference < Formatter def select(select_sql, aliaz) - "(#{select_sql}) AS #{engine.quote_table_name(aliaz)}" + "(#{select_sql}) AS #{quote_table_name(aliaz)}" end end - class Attribute < Predicate + class Attribute < WhereCondition def initialize(attribute) @attribute, @engine = attribute, attribute.engine end @@ -66,7 +65,7 @@ module ActiveRelation end end - class Value < Predicate + class Value < WhereCondition end end end \ No newline at end of file -- cgit v1.2.3 From a8cf09a5b6bd4019e0f68e70d62d6e6be6b3784b Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Thu, 13 Mar 2008 22:46:12 -0700 Subject: added abstract declaration --- lib/active_relation/extensions.rb | 1 + lib/active_relation/extensions/class.rb | 17 +++++++++++++++++ lib/active_relation/extensions/object.rb | 10 +--------- lib/active_relation/sql.rb | 2 ++ 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 lib/active_relation/extensions/class.rb (limited to 'lib/active_relation') diff --git a/lib/active_relation/extensions.rb b/lib/active_relation/extensions.rb index 8a024947ed..7268a5549b 100644 --- a/lib/active_relation/extensions.rb +++ b/lib/active_relation/extensions.rb @@ -1,3 +1,4 @@ require 'active_relation/extensions/object' +require 'active_relation/extensions/class' require 'active_relation/extensions/array' require 'active_relation/extensions/hash' diff --git a/lib/active_relation/extensions/class.rb b/lib/active_relation/extensions/class.rb new file mode 100644 index 0000000000..0e5b728c26 --- /dev/null +++ b/lib/active_relation/extensions/class.rb @@ -0,0 +1,17 @@ +class Class + def abstract(*methods) + methods.each do |method| + define_method method do + raise NotImplementedError + end + end + end + + def hash_on(delegatee) + define_method :eql? do |other| + self == other + end + + delegate :hash, :to => delegatee + end +end \ No newline at end of file diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index ab874150ed..90eb73f0b0 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -1,12 +1,4 @@ -class Object - def self.hash_on(delegatee) - def eql?(other) - self == other - end - - delegate :hash, :to => delegatee - end - +class Object def bind(relation) ActiveRelation::Value.new(self, relation) end diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index b38e6dc392..e5a4eed08b 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -8,6 +8,8 @@ module ActiveRelation attr_reader :engine include Quoting + abstract :attribute, :select, :value + def initialize(engine) @engine = engine end -- cgit v1.2.3 From 384008b04b48735a73dbba5a7d4a18e794c897ad Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Thu, 13 Mar 2008 22:52:18 -0700 Subject: annotated abstraction - in compound - created superclass for the create/insert/update write operations, marked :call as abstract --- lib/active_relation/relations.rb | 1 + lib/active_relation/relations/compound.rb | 2 ++ lib/active_relation/relations/deletion.rb | 2 +- lib/active_relation/relations/insertion.rb | 2 +- lib/active_relation/relations/update.rb | 2 +- lib/active_relation/sql.rb | 5 +++-- 6 files changed, 9 insertions(+), 5 deletions(-) (limited to 'lib/active_relation') diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb index f992fca8be..7776fd3d18 100644 --- a/lib/active_relation/relations.rb +++ b/lib/active_relation/relations.rb @@ -1,5 +1,6 @@ require 'active_relation/relations/relation' require 'active_relation/relations/compound' +require 'active_relation/relations/writing' require 'active_relation/relations/table' require 'active_relation/relations/join' require 'active_relation/relations/aggregation' diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index a10b7588e7..fac0939c6f 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -1,5 +1,7 @@ module ActiveRelation class Compound < Relation + abstract :==, :descend + attr_reader :relation delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, diff --git a/lib/active_relation/relations/deletion.rb b/lib/active_relation/relations/deletion.rb index c5906e8bc8..0fcf523efe 100644 --- a/lib/active_relation/relations/deletion.rb +++ b/lib/active_relation/relations/deletion.rb @@ -1,5 +1,5 @@ module ActiveRelation - class Deletion < Compound + class Deletion < Writing def initialize(relation) @relation = relation end diff --git a/lib/active_relation/relations/insertion.rb b/lib/active_relation/relations/insertion.rb index 036db1a319..ce065b64c1 100644 --- a/lib/active_relation/relations/insertion.rb +++ b/lib/active_relation/relations/insertion.rb @@ -1,5 +1,5 @@ module ActiveRelation - class Insertion < Compound + class Insertion < Writing attr_reader :record def initialize(relation, record) diff --git a/lib/active_relation/relations/update.rb b/lib/active_relation/relations/update.rb index 0914cda035..a51f248866 100644 --- a/lib/active_relation/relations/update.rb +++ b/lib/active_relation/relations/update.rb @@ -1,5 +1,5 @@ module ActiveRelation - class Update < Compound + class Update < Writing attr_reader :assignments def initialize(relation, assignments) diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index e5a4eed08b..0d233b307e 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -5,10 +5,11 @@ module ActiveRelation end class Formatter + abstract :attribute, :select, :value + attr_reader :engine - include Quoting - abstract :attribute, :select, :value + include Quoting def initialize(engine) @engine = engine -- cgit v1.2.3 From cd9efb9eec6198f08b4fd5d754a33aa1b1669b37 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Thu, 13 Mar 2008 22:56:00 -0700 Subject: annotated abstract methods on relation --- lib/active_relation/relations/relation.rb | 17 +++++++++++------ lib/active_relation/relations/writing.rb | 5 +++++ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 lib/active_relation/relations/writing.rb (limited to 'lib/active_relation') diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 9847cf2b7c..15782cde06 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -1,5 +1,7 @@ module ActiveRelation class Relation + abstract :attributes, :selects, :orders, :inserts, :groupings, :joins, :limit, :offset, :alias + def session Session.new end @@ -119,14 +121,17 @@ module ActiveRelation def call(connection = engine.connection) connection.select_all(to_sql) end - - def attribute_for_name(name) - attributes.detect { |a| a.alias_or_name.to_s == name.to_s } - end + + module AttributeAccessors + def attribute_for_name(name) + attributes.detect { |a| a.alias_or_name.to_s == name.to_s } + end - def attribute_for_attribute(attribute) - attributes.detect { |a| a =~ attribute } + def attribute_for_attribute(attribute) + attributes.detect { |a| a =~ attribute } + end end + include AttributeAccessors def bind(relation) self diff --git a/lib/active_relation/relations/writing.rb b/lib/active_relation/relations/writing.rb new file mode 100644 index 0000000000..3054e9b72f --- /dev/null +++ b/lib/active_relation/relations/writing.rb @@ -0,0 +1,5 @@ +module ActiveRelation + class Writing < Compound + abstract :call, :to_sql + end +end \ No newline at end of file -- cgit v1.2.3