Class: TADB::Table
- Inherits:
-
Object
- Object
- TADB::Table
- Defined in:
- lib/tadb/table.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(id) ⇒ Object
- #entries ⇒ Object
-
#initialize(name, clear_if_content = false) ⇒ Table
constructor
A new instance of Table.
- #insert(_entry) ⇒ Object
Constructor Details
#initialize(name, clear_if_content = false) ⇒ Table
Returns a new instance of Table.
6 7 8 9 |
# File 'lib/tadb/table.rb', line 6 def initialize(name, clear_if_content = false) @name = name clear_file_contents if clear_if_content end |
Instance Method Details
#clear ⇒ Object
41 42 43 44 45 |
# File 'lib/tadb/table.rb', line 41 def clear File.open(db_path, 'w') do |f| f.sync = true end end |
#delete(id) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/tadb/table.rb', line 31 def delete(id) remaining = entries.reject {|entry| entry[:id] === id} file('w') do |f| remaining.each do |entry| f.puts(JSON.generate(entry)) end end end |
#entries ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/tadb/table.rb', line 11 def entries file('r') do |f| f.readlines.map do |line| JSON.parse(line, symbolize_names: true) end end end |
#insert(_entry) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tadb/table.rb', line 19 def insert(_entry) entry = _entry.clone entry[:id] ||= SecureRandom.uuid validate_types(entry) file('a') do |f| f.puts(JSON.generate(entry)) end entry[:id] end |