Class: RnDB::Database
- Inherits:
-
Object
- Object
- RnDB::Database
- Defined in:
- lib/rndb/database.rb
Instance Attribute Summary collapse
-
#prng ⇒ Object
Returns the value of attribute prng.
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
Class Method Summary collapse
-
.conn ⇒ Object
Get a connection to the database.
Instance Method Summary collapse
-
#add_table(klass, size) ⇒ Object
Add a Table to the database, specifying the number of records to simulate.
-
#initialize(seed = Time.now.to_i) ⇒ Database
constructor
Opens a new fake database.
-
#load(state) ⇒ Object
Load state from the given hash.
-
#reset ⇒ Object
Clear overridden state.
-
#schema ⇒ Object
Dump the table schemas as a hash.
-
#state ⇒ Object
Dump just the overridden state as a hash.
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
#prng ⇒ Object
Returns the value of attribute prng.
5 6 7 |
# File 'lib/rndb/database.rb', line 5 def prng @prng end |
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
6 7 8 |
# File 'lib/rndb/database.rb', line 6 def seed @seed end |
Class Method Details
.conn ⇒ Object
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 |
#reset ⇒ Object
Clear overridden state.
27 28 29 |
# File 'lib/rndb/database.rb', line 27 def reset schema.each_value { |table| table[:state] = {} } end |
#schema ⇒ Object
Dump the table schemas as a hash.
22 23 24 |
# File 'lib/rndb/database.rb', line 22 def schema Thread.current[:rndb_tables] end |
#state ⇒ Object
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 |