Class: RnDB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/rndb/database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed = Time.now.to_i) ⇒ Database

Opens a new fake database. A seed for the PRNG may be optionally supplied.



9
10
11
12
13
14
# 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

Instance Attribute Details

#prngObject

Returns the value of attribute prng.



5
6
7
# File 'lib/rndb/database.rb', line 5

def prng
  @prng
end

#seedObject (readonly)

Returns the value of attribute seed.



6
7
8
# File 'lib/rndb/database.rb', line 6

def seed
  @seed
end

Class Method Details

.connObject

Get a connection to the database



47
48
49
# File 'lib/rndb/database.rb', line 47

def conn
  Thread.current[:rndb_database]
end

Instance Method Details

#add_table(klass, size) ⇒ Object

Add a Table to the database, specifying the number of records to simulate.



17
18
19
# File 'lib/rndb/database.rb', line 17

def add_table(klass, size)
  klass.send(:_migrate, size.to_i)
end

#load(state) ⇒ Object

Load state from the given hash.



39
40
41
42
43
# File 'lib/rndb/database.rb', line 39

def load(state)
  state.each do |name, value|
    schema[name][:state] = value
  end
end

#resetObject

Clear overridden state.



27
28
29
# File 'lib/rndb/database.rb', line 27

def reset
  schema.each_value { |table| table[:state] = {} }
end

#schemaObject

Dump the table schemas as a hash.



22
23
24
# File 'lib/rndb/database.rb', line 22

def schema
  Thread.current[:rndb_tables]
end

#stateObject

Dump just the overridden state as a hash.



32
33
34
35
36
# File 'lib/rndb/database.rb', line 32

def state
  schema.transform_values do |table|
    table[:state]
  end
end