rowdb 0.1.2 → 0.4.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: f4f0a6574146b856137a147b38f1206a5f9881f3b11cef23fa541fbc00b77e41
4
- data.tar.gz: 3e94c7830dd13dea3f7efdd64218ab1d6124a83077970547d0159411d33d101a
3
+ metadata.gz: e568763a556dbe43873ac35b8475ad9cf22f4970f58d835de63e5013bd585dc4
4
+ data.tar.gz: 7cccfab82d55dd47d9707f1ed750be9e36ca9c52234f51b9fb0b95bf752ee3fa
5
5
  SHA512:
6
- metadata.gz: 122ea5b0d1c86f97d1245acf76ea5ddaaf9c088c01e7ed99c5efb14262cf449a69f587d09301e24e457e686807ea7a0ac90328f591491c38ca6c3b3f8e78146e
7
- data.tar.gz: bf657e8e3f1fd007af9f4841d1845d23fd5575d761ef2744139a95eb76f7608b1ecf236567792fc9c6c56c62a3f6a2f6c6bcaa736977c134705e461d04932587
6
+ metadata.gz: 7d302bc472aead69cde7eda497a61654d2bb53b17e15fd3f3dde8c3b6dc0d1e43276bc6f0916701039d2b84f360e1bd0a0c2217e598dc6e37fdd5e0678130bd7
7
+ data.tar.gz: d514edcb1de1cd39445f5fb25ae7fce7b892802afa7bd7ad6cb710b044c7210f88d08e5103454901143f0f8a7f27f4437892bbd8eb0c559f215cb68dacd687f9
@@ -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,31 +1,84 @@
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
- p filepath
8
- p Dir.pwd
9
- p __FILE__
10
- p __dir__
11
- @adapter = self.send(adapter, filepath)
6
+ # Use Reflekt when available.
7
+ if Gem::Specification::find_by_name('reflekt')
8
+ require 'reflekt'
9
+ prepend Reflekt
12
10
  end
13
11
 
14
- def defaults(hash)
12
+ def initialize(file_path, adapter = :sync)
13
+ @adapter = self.send(adapter, normalize_path(file_path))
14
+ @data = R_.chain(@adapter.read())
15
+ @get_path = nil
16
+ end
17
+
18
+ def defaults(data)
19
+ if @data.value().nil?
20
+ # Load default data.
21
+ @data = R_.chain(data)
22
+ # Save data to disk.
23
+ @adapter.write(data)
24
+ end
25
+ self
26
+ end
15
27
 
16
- json = Oj.dump(hash)
17
- @adapter.write(json)
28
+ def get(path)
29
+ @get_path = path
30
+ @data.get(path)
31
+ self
32
+ end
18
33
 
34
+ def set(path, value)
35
+ @data.set(path, value)
36
+ self
19
37
  end
20
38
 
21
- def get(item)
39
+ def value()
40
+ @data.value()
41
+ end
42
+
43
+ def push(value)
44
+ if @get_path.nil?
45
+ raise StandardError.new "You must get() before push()."
46
+ end
47
+
48
+ index = @data.get(@get_path).value().count
49
+ @data.set("#{@get_path}[#{index}]", value)
22
50
 
51
+ self
52
+ end
53
+
54
+ def write()
55
+ @adapter.write(@data.value())
56
+ self
23
57
  end
24
58
 
25
59
  private
26
60
 
27
- def file_system(filepath)
28
- FileSystem.new(filepath)
61
+ def sync(file_path)
62
+ Sync.new(file_path)
63
+ end
64
+
65
+ ##
66
+ # Normalize path.
67
+ #
68
+ # @param file_path - An absolute or relative path.
69
+ # @return An absolute path.
70
+ ##
71
+ def normalize_path(file_path)
72
+
73
+ # Absolute path.
74
+ if file_path.start_with? '/'
75
+ return file_path
76
+ # Relative path.
77
+ else
78
+ # Get directory the script executed from.
79
+ return File.join(Dir.pwd, '/' + file_path)
80
+ end
81
+
29
82
  end
30
83
 
31
84
  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.2
4
+ version: 0.4.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
@@ -38,16 +38,17 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: A local JSON database for Ruby inspired by lowdb.
41
+ description: A local JSON database for Ruby inspired by lowdb, using Rudash for easy
42
+ Hash traversal.
42
43
  email: maediprichard@gmailcom
43
44
  executables: []
44
45
  extensions: []
45
46
  extra_rdoc_files: []
46
47
  files:
47
48
  - lib/adapters/Adapter.rb
48
- - lib/adapters/FileSystem.rb
49
+ - lib/adapters/Sync.rb
49
50
  - lib/rowdb.rb
50
- homepage: https://github.com/maedi/rowdb
51
+ homepage: https://github.com/rowdb/rowdb
51
52
  licenses:
52
53
  - MPL-2.0
53
54
  metadata: {}
@@ -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