rowdb 0.1.3 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dabd84303018d21818c1930fe77d0612cd50a84699507c1913f5b21ba74d7713
4
- data.tar.gz: '038bba35d11a83ec5214fa8ef9c44c9538a604809981955c24504697590329a0'
3
+ metadata.gz: 5ad64b04f574bb4bf24939928f1c85534ac368f4304b70e4d91dd85fe9c23b84
4
+ data.tar.gz: 3845d4ce910bb3e853d7723348dc3471034aa90febfe21cc07128e2c09bcc340
5
5
  SHA512:
6
- metadata.gz: b057c1358d2b4d96ed52b15ad5375563aa869b16a2ebe5d894ca10e78e718f35f581ff559b5b5b01681ec6bcbfd511e82bc9b7a2cc60bf2c4ac8bb6bccbb1e7a
7
- data.tar.gz: eb0699415e0f7d1a398f393f88efbcadb1165b4e225677ac36a2616280da273aeebb5031de1d3d33a393bec76dcf0b7ae78c909b1ef7c075122268d35022e9c0
6
+ metadata.gz: 8d013e350e375819a84b73e6103c4e4313098b2218f397d8ab16465695f74d7212c711690fd23c9d8d0df1c2e786041b5581a0472ba7a4babb63c17d89610674
7
+ data.tar.gz: 18643fcf1fbe0c294bf88757dcc375c00239470856a92e8380726c99f88f8131e5482a9d5731555b364c83e711210c95e523d545b14a936bfff71473acfe0f7f
@@ -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,30 +1,59 @@
1
1
  require 'rudash'
2
- require_relative 'adapters/FileSystem.rb'
2
+ require_relative 'adapters/Sync.rb'
3
3
 
4
4
  class Rowdb
5
5
 
6
- def initialize(adapter = :file_system, file_path)
6
+ def initialize(file_path, adapter = :sync)
7
7
  @adapter = self.send(adapter, normalize_path(file_path))
8
- @data = @adapter.read()
8
+ @data = R_.chain(@adapter.read())
9
+ @get_path = nil
9
10
  end
10
11
 
11
- def defaults(hash)
12
- json = Oj.dump(hash)
13
- @adapter.write(json)
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
14
20
  end
15
21
 
16
22
  def get(path)
17
- R_.get(@adapter.source, path)
23
+ @get_path = path
24
+ @data.get(path)
25
+ self
18
26
  end
19
27
 
20
28
  def set(path, value)
21
- @data = R_.set(@data, path, value)
29
+ @data.set(path, value)
30
+ self
31
+ end
32
+
33
+ def value()
34
+ @data.value()
35
+ end
36
+
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)
44
+
45
+ self
46
+ end
47
+
48
+ def write()
49
+ @adapter.write(@data.value())
50
+ self
22
51
  end
23
52
 
24
53
  private
25
54
 
26
- def file_system(file_path)
27
- FileSystem.new(file_path)
55
+ def sync(file_path)
56
+ Sync.new(file_path)
28
57
  end
29
58
 
30
59
  ##
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.3
4
+ version: 0.4.1
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-19 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,18 +0,0 @@
1
- require 'oj'
2
- require_relative 'Adapter.rb'
3
-
4
- class FileSystem < Adapter
5
-
6
- def read()
7
-
8
- Oj.load_file(@source)
9
-
10
- end
11
-
12
- def write(json)
13
-
14
- Oj.to_file(@source, json)
15
-
16
- end
17
-
18
- end