rowdb 0.4.1 → 0.6.3

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: 5ad64b04f574bb4bf24939928f1c85534ac368f4304b70e4d91dd85fe9c23b84
4
- data.tar.gz: 3845d4ce910bb3e853d7723348dc3471034aa90febfe21cc07128e2c09bcc340
3
+ metadata.gz: f4af7bf755b47e63af755ac16150648b479b011762eb14b967c48fd8e4347107
4
+ data.tar.gz: 33b9e69ca1b509960b476a65d12eef78ba81426330558e52a7f8b68afe9bdb90
5
5
  SHA512:
6
- metadata.gz: 8d013e350e375819a84b73e6103c4e4313098b2218f397d8ab16465695f74d7212c711690fd23c9d8d0df1c2e786041b5581a0472ba7a4babb63c17d89610674
7
- data.tar.gz: 18643fcf1fbe0c294bf88757dcc375c00239470856a92e8380726c99f88f8131e5482a9d5731555b364c83e711210c95e523d545b14a936bfff71473acfe0f7f
6
+ metadata.gz: 007f875740d29b424e4133786fbcb6c12273bbcc8f0165c535fc10b88ba48a77ccc295a240cc37b909766e645f58ebc10b201d0cbfb6e7f4519a154105d6c4cd
7
+ data.tar.gz: 879f35928d80afccb5e60b0e9549cd5e90aa1df3a509b933b2896ec25f48911f0152f4c308e6eda50dd8d4113e1c64d7b9b608503fec49b796528e2590855439
@@ -1,7 +1,91 @@
1
1
  class Adapter
2
2
 
3
- def initialize(source)
4
- @source = source
3
+ def initialize(file_path, js_var)
4
+
5
+ @source = normalize_path(file_path)
6
+ @format = find_format(file_path)
7
+ @prefix = "var #{js_var} = "
8
+ @suffix = ";"
9
+
10
+ end
11
+
12
+ ##
13
+ # Normalize path.
14
+ #
15
+ # @param file_path - An absolute or relative path.
16
+ # @return An absolute path.
17
+ ##
18
+ def normalize_path(file_path)
19
+
20
+ # Absolute path.
21
+ if file_path.start_with? '/'
22
+ return file_path
23
+ # Relative path.
24
+ else
25
+ # Get directory the script executed from.
26
+ return File.join(Dir.pwd, '/' + file_path)
27
+ end
28
+
29
+ end
30
+
31
+ ##
32
+ # Find format.
33
+ #
34
+ # Find the format of a file based on its extension.
35
+ ##
36
+ def find_format(file_path)
37
+
38
+ extension = File.extname(file_path)
39
+
40
+ case extension
41
+ when ".json"
42
+ return :json
43
+ when ".js"
44
+ return :js
45
+ end
46
+
47
+ :json
48
+
49
+ end
50
+
51
+ ##
52
+ # Wrap JSON in a Javascript variable.
53
+ #
54
+ # @param String json
55
+ ##
56
+ def wrap()
57
+
58
+ new_file = ""
59
+
60
+ # Open file.
61
+ File.open(@source, 'r') do |file|
62
+ new_file = file.read
63
+ new_file.prepend(@prefix)
64
+ new_file << @suffix
65
+ end
66
+
67
+ # Overwrite file.
68
+ File.open(@source, 'w') do |file|
69
+ file.write(new_file)
70
+ end
71
+
72
+ end
73
+
74
+ ##
75
+ # Unwrap JSON from a Javascript variable.
76
+ #
77
+ # @param String json
78
+ ##
79
+ def unwrap(json)
80
+
81
+ # Deletes: var data = \"
82
+ json.delete_prefix!(@prefix + '"')
83
+
84
+ # Deletes: \";
85
+ # Deletes: \";\n
86
+ json.delete_suffix!('"' + @suffix)
87
+ json.delete_suffix!('"' + @suffix + "\n")
88
+
5
89
  end
6
90
 
7
91
  end
@@ -13,10 +13,27 @@ class Sync < Adapter
13
13
  ##
14
14
  def read()
15
15
 
16
- json = Oj.load_file(@source)
16
+ json = nil
17
+
18
+ # Load JSON inside a Javascript variable.
19
+ if @format == :js && File.exist?(@source)
20
+ File.open(@source, 'r') do |file|
21
+ json = file.read
22
+ # Fix double encoding issue due to JSON string becoming Ruby string.
23
+ json.gsub!('\\"', '"')
24
+ unwrap(json)
25
+ end
26
+ # Load JSON string.
27
+ else
28
+ json = Oj.load_file(@source)
29
+ end
17
30
 
18
31
  unless json.nil?
19
- return Oj.load(json)
32
+
33
+ # Parse JSON.
34
+ data = Oj.load(json)
35
+ return data.transform_keys(&:to_sym)
36
+
20
37
  end
21
38
 
22
39
  return nil
@@ -24,15 +41,20 @@ class Sync < Adapter
24
41
  end
25
42
 
26
43
  ##
27
- # Save a Hash to a file as JSON.
44
+ # Save a Hash to a file as a JSON string or JS.
28
45
  #
29
46
  # @param Hash data
30
47
  ##
31
48
  def write(data)
32
49
 
33
- json = Oj.dump(data)
50
+ json = Oj.dump(data, mode: :compat)
51
+
34
52
  Oj.to_file(@source, json)
35
53
 
54
+ if @format == :js
55
+ wrap()
56
+ end
57
+
36
58
  end
37
59
 
38
60
  end
@@ -3,76 +3,69 @@ require_relative 'adapters/Sync.rb'
3
3
 
4
4
  class Rowdb
5
5
 
6
- def initialize(file_path, adapter = :sync)
7
- @adapter = self.send(adapter, normalize_path(file_path))
8
- @data = R_.chain(@adapter.read())
6
+ def initialize(file_path, adapter = :sync, js_var = "db")
7
+
8
+ # Initialize the chosen adapter.
9
+ @adapter = self.send(adapter, file_path, js_var)
10
+
11
+ @chain = R_.chain(@adapter.read())
9
12
  @get_path = nil
13
+
10
14
  end
11
15
 
16
+ # Set default data.
12
17
  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
+ if @chain.value().nil?
19
+ @chain = R_.chain(data.transform_keys(&:to_sym))
18
20
  end
19
21
  self
20
22
  end
21
23
 
22
24
  def get(path)
23
25
  @get_path = path
24
- @data.get(path)
26
+ @chain.get(path)
25
27
  self
26
28
  end
27
29
 
28
30
  def set(path, value)
29
- @data.set(path, value)
31
+ @chain.set(path, value)
30
32
  self
31
33
  end
32
34
 
33
35
  def value()
34
- @data.value()
36
+ @chain.value()
35
37
  end
36
38
 
37
39
  def push(value)
40
+
38
41
  if @get_path.nil?
39
42
  raise StandardError.new "You must get() before push()."
40
43
  end
41
44
 
42
- index = @data.get(@get_path).value().count
43
- @data.set("#{@get_path}[#{index}]", value)
45
+ # Add value to end of array.
46
+ adder = -> (items) {
47
+ [*items, value]
48
+ }
49
+ R_.update(@chain.value(), @get_path, adder)
44
50
 
45
51
  self
46
52
  end
47
53
 
48
54
  def write()
49
- @adapter.write(@data.value())
55
+ @adapter.write(@chain.value())
50
56
  self
51
57
  end
52
58
 
53
59
  private
54
60
 
55
- def sync(file_path)
56
- Sync.new(file_path)
57
- end
58
-
59
61
  ##
60
- # Normalize path.
62
+ # Adapters.
61
63
  #
62
- # @param file_path - An absolute or relative path.
63
- # @return An absolute path.
64
+ # The chosen adapter is initialized by the constructor.
64
65
  ##
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
66
 
67
+ def sync(file_path, js_var)
68
+ Sync.new(file_path, js_var)
76
69
  end
77
70
 
78
71
  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.4.1
4
+ version: 0.6.3
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-08-19 00:00:00.000000000 Z
11
+ date: 2020-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -38,9 +38,9 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: A local JSON database for Ruby inspired by lowdb, using Rudash for easy
42
- Hash traversal.
43
- email: maediprichard@gmailcom
41
+ description: A local JSON database built in Ruby inspired by lowdb, using Rudash for
42
+ easy Hash traversal.
43
+ email: maediprichard@gmail.com
44
44
  executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []