diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/active_relation/extensions/object.rb | 6 | ||||
-rw-r--r-- | lib/active_relation/predicates.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 5 | ||||
-rw-r--r-- | lib/active_relation/relations/compound.rb | 1 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 10 | ||||
-rw-r--r-- | lib/active_relation/relations/table.rb | 15 | ||||
-rw-r--r-- | lib/active_relation/sessions/session.rb | 28 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 3 |
8 files changed, 53 insertions, 17 deletions
diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index 0cc87bf262..c8a0ff49e5 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -14,4 +14,10 @@ class Object def strategy ActiveRelation::Sql::Scalar.new end + + def metaclass + class << self + self + end + end end
\ No newline at end of file diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index 2a36e65042..ddc6eab02b 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -17,7 +17,7 @@ module ActiveRelation end def bind(relation) - descend{ |x| x.bind(relation) } + descend { |x| x.bind(relation) } end def qualify diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index 75f19605c7..a72acb0c34 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -49,13 +49,10 @@ module ActiveRelation def self.included(klass) klass.class_eval do alias_method :eql?, :== + delegate :hash, :to => :name end end - def hash - relation.hash + name.hash - end - def history [self] + (ancestor ? [ancestor, ancestor.send(:history)].flatten : []) end diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 115315a76a..4e583e61e8 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -3,6 +3,7 @@ module ActiveRelation attr_reader :relation delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, + :hash, :to => :relation def attributes diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 7536390aca..889cf59d3a 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -3,7 +3,7 @@ module ActiveRelation include Sql::Quoting def session - Session.instance + Session.new end module Enumerable @@ -103,6 +103,10 @@ module ActiveRelation def alias? false end + + def eql?(other) + self == other + end def to_sql(strategy = Sql::Relation.new) strategy.select [ @@ -117,10 +121,6 @@ module ActiveRelation ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql - - def descend - yield self - end def connection ActiveRecord::Base.connection diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index 5720be30e3..271e76f362 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -1,7 +1,8 @@ module ActiveRelation class Table < Relation attr_reader :name - + delegate :hash, :to => :name + def initialize(name) @name = name.to_s end @@ -21,17 +22,25 @@ module ActiveRelation end def column_for(attribute) - self[attribute] and columns.detect { |c| c.name == attribute.name } + self[attribute] and columns.detect { |c| c.name == attribute.name.to_s } end def ==(other) self.class == other.class and name == other.name end - + def columns @columns ||= connection.columns(name, "#{name} Columns") end + + def descend + yield self + end + + def reset + @attributes = @columns = nil + end protected def table_sql diff --git a/lib/active_relation/sessions/session.rb b/lib/active_relation/sessions/session.rb index d6e2ae7169..7eb66a1395 100644 --- a/lib/active_relation/sessions/session.rb +++ b/lib/active_relation/sessions/session.rb @@ -2,8 +2,29 @@ require 'singleton' module ActiveRelation class Session - include Singleton - + class << self + def start + if @started + yield + else + begin + @started = true + @instance = new + manufacture = method(:new) + metaclass.class_eval do + define_method(:new) { @instance } + end + yield + ensure + metaclass.class_eval do + define_method(:new, &manufacture) + end + @started = false + end + end + end + end + module CRUD def connection ActiveRecord::Base.connection @@ -14,7 +35,8 @@ module ActiveRelation end def read(select) - connection.select_all(select.to_sql) + @read ||= {} + @read.has_key?(select) ? @read[select] : (@read[select] = connection.select_all(select.to_sql)) end def update(update) diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 49ced3cbaa..0fc8b3e1be 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -2,12 +2,13 @@ module ActiveRelation module Sql module Quoting def connection - Session.instance.connection + Session.new.connection end delegate :quote_table_name, :quote_column_name, :quote, :to => :connection end + # module Formatting Context / Strategy # unit test me!!! class Strategy include Quoting end |