mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
basic functionality for simplest active record find(id)
- messy code, to be cleaned up this weekend
This commit is contained in:
parent
6647a1e08e
commit
98527c8f7d
3 changed files with 31 additions and 11 deletions
|
@ -1,4 +1,18 @@
|
||||||
module ActiveRelation
|
module ActiveRelation
|
||||||
|
# this file is currently just a hack to adapt between activerecord::base which holds the connection specification
|
||||||
|
# and active relation. ultimately, this file should be in effect what the connection specification is in active record;
|
||||||
|
# that is: a spec of the database (url, password, etc.), a quoting adapter layer, and a connection pool.
|
||||||
class Engine
|
class Engine
|
||||||
|
def initialize(ar = nil)
|
||||||
|
@ar = ar
|
||||||
|
end
|
||||||
|
|
||||||
|
def connection
|
||||||
|
@ar.connection
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(method, *args, &block)
|
||||||
|
@ar.connection.send(method, *args, &block)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -3,22 +3,20 @@ require 'singleton'
|
||||||
module ActiveRelation
|
module ActiveRelation
|
||||||
class Session
|
class Session
|
||||||
class << self
|
class << self
|
||||||
|
attr_accessor :instance
|
||||||
|
alias_method :manufacture, :new
|
||||||
|
|
||||||
def start
|
def start
|
||||||
if @started
|
if @started
|
||||||
yield
|
yield
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
@started = true
|
@started = true
|
||||||
@instance = new
|
@instance = manufacture
|
||||||
manufacture = method(:new)
|
metaclass.send :alias_method, :new, :instance
|
||||||
metaclass.class_eval do
|
|
||||||
define_method(:new) { @instance }
|
|
||||||
end
|
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
metaclass.class_eval do
|
metaclass.send :alias_method, :new, :manufacture
|
||||||
define_method(:new, &manufacture)
|
|
||||||
end
|
|
||||||
@started = false
|
@started = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -31,8 +29,10 @@ module ActiveRelation
|
||||||
end
|
end
|
||||||
|
|
||||||
def read(select)
|
def read(select)
|
||||||
@read ||= {}
|
@read ||= Hash.new do |hash, select|
|
||||||
@read.has_key?(select) ? @read[select] : (@read[select] = select.engine.select_all(select.to_sql))
|
hash[select] = select.engine.select_all(select.to_sql)
|
||||||
|
end
|
||||||
|
@read[select]
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(update)
|
def update(update)
|
||||||
|
|
|
@ -91,5 +91,11 @@ module ActiveRelation
|
||||||
Table.new(:users, engine = Engine.new).engine.should == engine
|
Table.new(:users, engine = Engine.new).engine.should == engine
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#reset' do
|
||||||
|
it "" do
|
||||||
|
pending
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue