Class: JSONdb::Db
Constant Summary
Constants included from Validations::Records
Validations::Records::FieldsToNotVerify
Constants included from Validations::Types
Validations::Types::AllowedTypes
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#folder ⇒ Object
readonly
Returns the value of attribute folder.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #db_to_hash(update = false) ⇒ Object
-
#initialize(folder, create_folder_if_not_exists = true, writeable = true) ⇒ Db
constructor
A new instance of Db.
- #persist ⇒ Object
- #writable ⇒ Object
Methods included from Commands
#delete, #delete_by_id, #insert_into, #select_from, #update_set
Methods included from Validations::Records
Methods included from Validations::Types
#allowed_type?, #validate_type
Methods included from Logger
#allowed_log_level?, #log, #log_enabled?, #log_this?
Methods included from Tables
#create_table, #drop_table, #load_table, #table, #table_count, #table_exists?, #table_names, #tables, #tables_to_hash
Constructor Details
#initialize(folder, create_folder_if_not_exists = true, writeable = true) ⇒ Db
Returns a new instance of Db.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/jsondb/db.rb', line 10 def initialize(folder, create_folder_if_not_exists = true, writeable = true) JSONdb.settings.folder = folder JSONdb.settings.log_folder = folder JSONdb.settings.writeable = writeable @folder = File.(folder) @created_at = nil @updated_at = nil open_database(create_folder_if_not_exists) end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
5 6 7 |
# File 'lib/jsondb/db.rb', line 5 def created_at @created_at end |
#folder ⇒ Object (readonly)
Returns the value of attribute folder.
5 6 7 |
# File 'lib/jsondb/db.rb', line 5 def folder @folder end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
5 6 7 |
# File 'lib/jsondb/db.rb', line 5 def updated_at @updated_at end |
Instance Method Details
#db_to_hash(update = false) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jsondb/db.rb', line 42 def db_to_hash(update = false) @updated_at = Time.now.to_i if update { "tables" => tables_to_hash, "db" => { "created_at" => @created_at, "updated_at" => @updated_at } } end |
#persist ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jsondb/db.rb', line 27 def persist begin JSONdb.tables.keys.each do |key| JSONdb.tables[key].persist if JSONdb.tables[key].persisted == false end @file.contents = db_to_hash(true) @file.write log("DB saved to disk", :debug) return true rescue log("DB NOT saved to disk!", :error, true) return false end end |