1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/test/support/fake_record.rb
2010-11-05 14:05:10 -07:00

91 lines
1.6 KiB
Ruby

module FakeRecord
class Column < Struct.new(:name, :type)
end
class Connection
attr_reader :tables
def initialize
@tables = %w{ users photos developers }
@columns = {
'users' => [
Column.new('id', :integer),
Column.new('name', :string),
Column.new('bool', :boolean),
Column.new('created_at', :date),
]
}
@primary_keys = {
'users' => 'id'
}
end
def primary_key name
@primary_keys[name.to_s]
end
def table_exists? name
@tables.include? name.to_s
end
def columns name, message = nil
@columns[name.to_s]
end
def quote_table_name name
"\"#{name.to_s}\""
end
def quote_column_name name
"\"#{name.to_s}\""
end
def quote thing, column = nil
if column && column.type == :integer
return 'NULL' if thing.nil?
return thing.to_i
end
case thing
when true
"'t'"
when false
"'f'"
when nil
'NULL'
when Numeric
thing
else
"'#{thing}'"
end
end
end
class ConnectionPool
class Spec < Struct.new(:config)
end
attr_reader :spec, :connection
def initialize
@spec = Spec.new(:adapter => 'america')
@connection = Connection.new
end
def with_connection
yield connection
end
end
class Base
attr_accessor :connection_pool
def initialize
@connection_pool = ConnectionPool.new
end
def connection
connection_pool.connection
end
end
end