rowdb 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91f721f4999dd1fa1fc295b15621450c99db1efb13aba1bb8773751eb0915ebb
4
- data.tar.gz: 6242c387859ae8cf0be00bbc830591cc7c94c84b1b616dd7c303f2125d77860d
3
+ metadata.gz: 25dccfb912a9743e255f553123f0a8ce576a5a3a308436b84ed17b4f3587eebd
4
+ data.tar.gz: 5f44e3b3dcafda35fe3d1b2abd6514f72ae584f04200f2efab070c52c93feeda
5
5
  SHA512:
6
- metadata.gz: 9841eaf9c7c0b1454f23f43d1e96473060cbb52f75b5e7d4919d05f0f90e386398c6b9dadc5773988f0ccbb3a46aea8da786e2bbf0d7b2a091cffe72a6fc1223
7
- data.tar.gz: 86021aaf57766cdbb6a68d22df6713847fd20e12b116772001099d4ce8a32461a3146558f3c04b617ae42efdf2a3914e4bd022915cece44210e24aac8fe51d39
6
+ metadata.gz: 4051e0fdb7d774021c6c1d71c577c690bcf26ba1abfecd5d869ed0460c416862f4f50164a51f9941d445c84277ac0b0195318a1f7c9e50613f966809f033c3d2
7
+ data.tar.gz: de22fab9cafb0a8d9dcf00b031ad11204e5b4ef64d67ccf0e9b6102c327cc20970c72d5bbc6794cc4ec5db5b5cb8efb14598d3c2d13360b9e0cecb045a2a9397
@@ -0,0 +1,7 @@
1
+ class Adapter
2
+
3
+ def initialize(source)
4
+ @source = source
5
+ end
6
+
7
+ end
@@ -0,0 +1,38 @@
1
+ require 'oj'
2
+ require_relative 'Adapter.rb'
3
+
4
+ ##
5
+ # Synchronous file system.
6
+ ##
7
+ class Sync < Adapter
8
+
9
+ ##
10
+ # Load a JSON string from a file.
11
+ #
12
+ # @return Hash data
13
+ ##
14
+ def read()
15
+
16
+ json = Oj.load_file(@source)
17
+
18
+ unless json.nil?
19
+ return Oj.load(json)
20
+ end
21
+
22
+ return nil
23
+
24
+ end
25
+
26
+ ##
27
+ # Save a Hash to a file as JSON.
28
+ #
29
+ # @param Hash data
30
+ ##
31
+ def write(data)
32
+
33
+ json = Oj.dump(data)
34
+ Oj.to_file(@source, json)
35
+
36
+ end
37
+
38
+ end
@@ -1,27 +1,78 @@
1
- require 'oj'
2
- require_relative 'adapters/FileSystem.rb'
1
+ require 'rudash'
2
+ require_relative 'adapters/Sync.rb'
3
3
 
4
4
  class Rowdb
5
5
 
6
- def initialize(adapter = :file_system, filepath)
7
- @adapter = self.send(adapter, filepath)
6
+ def initialize(adapter = :sync, file_path)
7
+ @adapter = self.send(adapter, normalize_path(file_path))
8
+ @data = R_.chain(@adapter.read())
9
+ @get_path = nil
8
10
  end
9
11
 
10
- def defaults(hash)
12
+ def defaults(data)
13
+ if @data.value().nil?
14
+ # Load default data.
15
+ @data = R_.chain(data)
16
+ # Save data to disk.
17
+ @adapter.write(data)
18
+ end
19
+ self
20
+ end
21
+
22
+ def get(path)
23
+ @get_path = path
24
+ @data.get(path)
25
+ self
26
+ end
11
27
 
12
- json = Oj.dump(hash)
13
- @adapter.write(json)
28
+ def set(path, value)
29
+ @data.set(path, value)
30
+ self
31
+ end
14
32
 
33
+ def value()
34
+ @data.value()
15
35
  end
16
36
 
17
- def get(item)
37
+ def push(value)
38
+ if @get_path.nil?
39
+ raise StandardError.new "You must get() before push()."
40
+ end
41
+
42
+ index = @data.get(@get_path).value().count
43
+ @data.set("#{@get_path}[#{index}]", value)
18
44
 
45
+ self
46
+ end
47
+
48
+ def write()
49
+ @adapter.write(@data.value())
50
+ self
19
51
  end
20
52
 
21
53
  private
22
54
 
23
- def file_system(filepath)
24
- FileSystem.new(filepath)
55
+ def sync(file_path)
56
+ Sync.new(file_path)
57
+ end
58
+
59
+ ##
60
+ # Normalize path.
61
+ #
62
+ # @param file_path - An absolute or relative path.
63
+ # @return An absolute path.
64
+ ##
65
+ def normalize_path(file_path)
66
+
67
+ # Absolute path.
68
+ if file_path.start_with? '/'
69
+ return file_path
70
+ # Relative path.
71
+ else
72
+ # Get directory the script executed from.
73
+ return File.join(Dir.pwd, '/' + file_path)
74
+ end
75
+
25
76
  end
26
77
 
27
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rowdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maedi Prichard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-23 00:00:00.000000000 Z
11
+ date: 2020-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -44,7 +44,8 @@ executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
- - lib/adapters/FileSystem.rb
47
+ - lib/adapters/Adapter.rb
48
+ - lib/adapters/Sync.rb
48
49
  - lib/rowdb.rb
49
50
  homepage: https://github.com/maedi/rowdb
50
51
  licenses:
@@ -1,17 +0,0 @@
1
- require_relative 'Adapter.rb'
2
-
3
- class FileSystem < Adapter
4
-
5
- def read()
6
-
7
- p Oj.load_file(@source)
8
-
9
- end
10
-
11
- def write(json)
12
-
13
- Oj.to_file(@source, json)
14
-
15
- end
16
-
17
- end