class RnDB::Database
Attributes
prng[RW]
seed[R]
Public Class Methods
conn()
click to toggle source
Get a connection to the database
# File lib/rndb/database.rb, line 47 def conn Thread.current[:rndb_database] end
new(seed=Time.now.to_i)
click to toggle source
Opens a new fake database. A seed for the PRNG may be optionally supplied.
# File lib/rndb/database.rb, line 9 def initialize(seed=Time.now.to_i) raise "database already open" unless Thread.current[:rndb_database].nil? Thread.current[:rndb_database] = self @prng = Random @seed = seed end
Public Instance Methods
add_table(klass, size)
click to toggle source
Add a Table
to the database, specifying the number of records to simulate.
# File lib/rndb/database.rb, line 17 def add_table(klass, size) klass.send(:_migrate, size.to_i) end
load(state)
click to toggle source
Load state from the given hash.
# File lib/rndb/database.rb, line 39 def load(state) state.each do |name, value| schema[name][:state] = value end end
reset()
click to toggle source
Clear overridden state.
# File lib/rndb/database.rb, line 27 def reset schema.each_value { |table| table[:state] = {} } end
schema()
click to toggle source
Dump the table schemas as a hash.
# File lib/rndb/database.rb, line 22 def schema Thread.current[:rndb_tables] end
state()
click to toggle source
Dump just the overridden state as a hash.
# File lib/rndb/database.rb, line 32 def state schema.transform_values do |table| table[:state] end end