Class: JSONdb::Table

Inherits:
Object
  • Object
show all
Includes:
Fields, Logger, Records, Validations::Naming
Defined in:
lib/jsondb/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Records

#delete_record, #exists?, #insert_record, #new_record, #record, #record_count, #records, #records_to_hash, #update_record

Methods included from Fields

#add_main_fields, #create_field, #drop_field, #field, #fields, #fields_to_hash

Methods included from Logger

#allowed_log_level?, #log, #log_enabled?, #log_this?

Methods included from Validations::Naming

#allowed_name?

Constructor Details

#initialize(name, new_table = true) ⇒ Table

Returns a new instance of Table.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jsondb/table.rb', line 13

def initialize(name, new_table = true)
  @name = name
  
  @persisted = false

  JSONdb.records[@name] = JSONdb::PaginatedHash.new 
  JSONdb.fields[@name] = Hash.new

  set_defaults
  add_main_fields
  init_table(new_table)
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



11
12
13
# File 'lib/jsondb/table.rb', line 11

def created_at
  @created_at
end

#last_idObject

Returns the value of attribute last_id.



11
12
13
# File 'lib/jsondb/table.rb', line 11

def last_id
  @last_id
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/jsondb/table.rb', line 10

def name
  @name
end

#persistedObject (readonly)

Returns the value of attribute persisted.



10
11
12
# File 'lib/jsondb/table.rb', line 10

def persisted
  @persisted
end

#timestampObject

Returns the value of attribute timestamp.



11
12
13
# File 'lib/jsondb/table.rb', line 11

def timestamp
  @timestamp
end

#updated_atObject

Returns the value of attribute updated_at.



11
12
13
# File 'lib/jsondb/table.rb', line 11

def updated_at
  @updated_at
end

Instance Method Details

#destroyObject



43
44
45
# File 'lib/jsondb/table.rb', line 43

def destroy
  @file.destroy
end

#persistObject



26
27
28
29
30
# File 'lib/jsondb/table.rb', line 26

def persist
  @persisted = true
  @file.contents = records_to_hash
  @file.write
end

#to_hashObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/jsondb/table.rb', line 32

def to_hash
  {
    @name => {
      "created_at" => @created_at,
      "updated_at" => @updated_at,
      "last_id" => @last_id,
      "fields" => fields_to_hash
    },
  }
end