Class: Rowdb

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/sync.rb,
lib/rowdb.rb,
lib/adapters/adapter.rb

Overview

Synchronous file system.

Defined Under Namespace

Classes: Adapter, Sync

Instance Method Summary collapse

Constructor Details

#initialize(file_path, adapter = :sync, js_var = "db") ⇒ Rowdb

Returns a new instance of Rowdb.



12
13
14
15
16
17
18
19
20
# File 'lib/rowdb.rb', line 12

def initialize(file_path, adapter = :sync, js_var = "db")

  # Initialize the chosen adapter.
  @adapter = self.send(adapter, file_path, js_var)

  @chain = R_.chain(@adapter.read())
  @get_path = nil

end

Instance Method Details

#defaults(data) ⇒ Object

Set default data.



23
24
25
26
27
28
# File 'lib/rowdb.rb', line 23

def defaults(data)
  if @chain.value().nil?
    @chain = R_.chain(data.transform_keys(&:to_sym))
  end
  self
end

#get(path) ⇒ Object



30
31
32
33
34
# File 'lib/rowdb.rb', line 30

def get(path)
  @get_path = path
  @chain.get(path)
  self
end

#push(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rowdb.rb', line 45

def push(value)
  if @get_path.nil?
    raise StandardError.new "You must get() before push()."
  end

  # Add value to end of array.
  adder = -> (items) {
    [*items, value]
  }
  R_.update(@chain.value(), @get_path, adder)

  self
end

#set(path, value) ⇒ Object



36
37
38
39
# File 'lib/rowdb.rb', line 36

def set(path, value)
  @chain.set(path, value)
  self
end

#valueObject



41
42
43
# File 'lib/rowdb.rb', line 41

def value()
  @chain.value()
end

#writeObject



59
60
61
62
# File 'lib/rowdb.rb', line 59

def write()
  @adapter.write(@chain.value())
  self
end