rowdb 0.3.0 → 0.6.1

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: 25dccfb912a9743e255f553123f0a8ce576a5a3a308436b84ed17b4f3587eebd
4
- data.tar.gz: 5f44e3b3dcafda35fe3d1b2abd6514f72ae584f04200f2efab070c52c93feeda
3
+ metadata.gz: 01b60ad701cb2c8829f6eb8266886e09206c7b5ae7701f6f65b457226b9801c1
4
+ data.tar.gz: fa0f8c8e78696053f1db4371958f2dad7a7676ffde40378e67677232290c9e08
5
5
  SHA512:
6
- metadata.gz: 4051e0fdb7d774021c6c1d71c577c690bcf26ba1abfecd5d869ed0460c416862f4f50164a51f9941d445c84277ac0b0195318a1f7c9e50613f966809f033c3d2
7
- data.tar.gz: de22fab9cafb0a8d9dcf00b031ad11204e5b4ef64d67ccf0e9b6102c327cc20970c72d5bbc6794cc4ec5db5b5cb8efb14598d3c2d13360b9e0cecb045a2a9397
6
+ metadata.gz: b4b886394e59f1f0bd01462ba938c18afeb017d94088a41a4b9c7147fae7f57069acde07c26fd30e302e1a2f367a024668abcfe50d82c84a7848a90caaba13c8
7
+ data.tar.gz: 7243829e5c96dcb28c7be102f4e913ad7bfae2662747df03ba83480b30d761d4b7b98b9bbc5cff64584cbf9473808c72d33a4075a3f8e5bd86f56dcb87b651db
@@ -1,7 +1,89 @@
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
+ json.delete_suffix!('"' + @suffix)
86
+
5
87
  end
6
88
 
7
89
  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(adapter = :sync, file_path)
7
- @adapter = self.send(adapter, normalize_path(file_path))
8
- @data = R_.chain(@adapter.read())
6
+ def initialize(file_path, adapter = :sync, js_var = "data")
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.3.0
4
+ version: 0.6.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-08-03 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -38,8 +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.
42
- 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
43
44
  executables: []
44
45
  extensions: []
45
46
  extra_rdoc_files: []
@@ -47,7 +48,7 @@ files:
47
48
  - lib/adapters/Adapter.rb
48
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: {}