Module: JSONdb::Records

Included in:
Table
Defined in:
lib/jsondb/records.rb

Instance Method Summary collapse

Instance Method Details

#delete_record(record) ⇒ Object Also known as: drop_record



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

def delete_record(record)
  begin
    @persisted = false
    JSONdb.records[@name][record.id] = nil
    JSONdb.records[@name].delete(record.id)
    record = nil
    return true
  rescue
    return false
  end
end

#exists?(record) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/jsondb/records.rb', line 46

def exists?(record)
  !JSONdb.records[@name][record.id].nil?
end

#insert_record(record) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/jsondb/records.rb', line 17

def insert_record(record)
  @persisted = false
  @updated_at = Time.now.to_i # updated table updated_at time
  
  record.id = new_id
  record.created_at = Time.now.to_i
  record.updated_at = Time.now.to_i
  JSONdb.records[@name][record.id] = record
end

#new_recordObject



13
14
15
# File 'lib/jsondb/records.rb', line 13

def new_record
  return JSONdb::Record.new(@name)
end

#record(key) ⇒ Object



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

def record(key)
  JSONdb.records[@name][key]
end

#record_countObject Also known as: count



50
51
52
# File 'lib/jsondb/records.rb', line 50

def record_count
  JSONdb.records[@name].keys.count
end

#recordsObject



9
10
11
# File 'lib/jsondb/records.rb', line 9

def records
  JSONdb.records[@name]
end

#records_to_hashObject



56
57
58
59
60
61
62
63
64
# File 'lib/jsondb/records.rb', line 56

def records_to_hash
  to_hash = Hash.new
  JSONdb.records[@name].each do |key, values|
    to_hash = to_hash.merge({
      key => values.to_hash
    })
  end
  return to_hash
end

#update_record(record) ⇒ Object



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

def update_record(record)
  @persisted = false
  JSONdb.records[@name][record.id] = record
end