Class: JSONdb::Db

Inherits:
Object
  • Object
show all
Includes:
Commands, Tables
Defined in:
lib/jsondb/db.rb

Constant Summary

Constants included from Validations::Records

Validations::Records::FieldsToNotVerify

Constants included from Validations::Types

Validations::Types::AllowedTypes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#delete, #delete_by_id, #insert_into, #select_from, #update_set

Methods included from Validations::Records

#record_validates?

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.expand_path(folder)
  @created_at = nil
  @updated_at = nil

  open_database(create_folder_if_not_exists)
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



5
6
7
# File 'lib/jsondb/db.rb', line 5

def created_at
  @created_at
end

#folderObject (readonly)

Returns the value of attribute folder.



5
6
7
# File 'lib/jsondb/db.rb', line 5

def folder
  @folder
end

#updated_atObject (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

#persistObject



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

#writableObject



23
24
25
# File 'lib/jsondb/db.rb', line 23

def writable
  JSONdb.settings.writeable
end