Class: Rowdb

Inherits:
Object
  • Object
show all
Defined in:
lib/rowdb.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Rowdb.



6
7
8
9
10
11
12
13
14
# File 'lib/rowdb.rb', line 6

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.



17
18
19
20
21
22
# File 'lib/rowdb.rb', line 17

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

#get(path) ⇒ Object



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

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

#push(value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rowdb.rb', line 39

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



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

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

#valueObject



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

def value()
  @chain.value()
end

#writeObject



54
55
56
57
# File 'lib/rowdb.rb', line 54

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