rowdb 0.6.3 → 0.6.4

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: f4af7bf755b47e63af755ac16150648b479b011762eb14b967c48fd8e4347107
4
- data.tar.gz: 33b9e69ca1b509960b476a65d12eef78ba81426330558e52a7f8b68afe9bdb90
3
+ metadata.gz: ac3c5afe3b28edfd757cb704048e0c0a6d194a2392991997b5952ce021d31f92
4
+ data.tar.gz: 3e77f6c857320a74e9294fcfd1116ebd3107bb0ea9c96b526fbe06f6249d96b4
5
5
  SHA512:
6
- metadata.gz: 007f875740d29b424e4133786fbcb6c12273bbcc8f0165c535fc10b88ba48a77ccc295a240cc37b909766e645f58ebc10b201d0cbfb6e7f4519a154105d6c4cd
7
- data.tar.gz: 879f35928d80afccb5e60b0e9549cd5e90aa1df3a509b933b2896ec25f48911f0152f4c308e6eda50dd8d4113e1c64d7b9b608503fec49b796528e2590855439
6
+ metadata.gz: 444cc6e4d2b537e194c195bbeffe681966c19c159c0922c3c9db53eb9da4f5b93afa2d39fe01285539ef9fa674d1490e9666ad608af618d3e547578d2351fdbc
7
+ data.tar.gz: 8b0b6812b2bb219f288f80c512f2c737552d822e151c44c5fdba6ca77dd197bf95b7921fa35c9c4a99a1c60faeca08b5921b694b7cff0c3c108cbe964fca9b79
@@ -0,0 +1,93 @@
1
+ class Rowdb
2
+ class Adapter
3
+
4
+ def initialize(file_path, js_var)
5
+
6
+ @source = normalize_path(file_path)
7
+ @format = find_format(file_path)
8
+ @prefix = "var #{js_var} = "
9
+ @suffix = ";"
10
+
11
+ end
12
+
13
+ ##
14
+ # Normalize path to absolute path.
15
+ #
16
+ # @param file_path [String] An absolute or relative path.
17
+ # @return [String] An absolute path.
18
+ ##
19
+ def normalize_path(file_path)
20
+
21
+ # Absolute path.
22
+ if file_path.start_with? '/'
23
+ return file_path
24
+ # Relative path.
25
+ else
26
+ # Get directory the script executed from.
27
+ return File.join(Dir.pwd, '/' + file_path)
28
+ end
29
+
30
+ end
31
+
32
+ ##
33
+ # Find the format of a file based on its extension.
34
+ #
35
+ # @parm file_path [String] The path to the file.
36
+ ##
37
+ def find_format(file_path)
38
+
39
+ extension = File.extname(file_path)
40
+
41
+ case extension
42
+ when ".json"
43
+ return :json
44
+ when ".js"
45
+ return :js
46
+ end
47
+
48
+ :json
49
+
50
+ end
51
+
52
+ ##
53
+ # Wrap JSON in a Javascript variable.
54
+ #
55
+ # @param json [String] The stringified JSON.
56
+ ##
57
+ def wrap()
58
+
59
+ new_file = ""
60
+
61
+ # Open file.
62
+ File.open(@source, 'r') do |file|
63
+ new_file = file.read
64
+ new_file.prepend(@prefix)
65
+ new_file << @suffix
66
+ end
67
+
68
+ # Overwrite file.
69
+ File.open(@source, 'w') do |file|
70
+ file.write(new_file)
71
+ end
72
+
73
+ end
74
+
75
+ ##
76
+ # Unwrap JSON from a Javascript variable.
77
+ #
78
+ # @param json [String] The stringified JSON.
79
+ ##
80
+ def unwrap(json)
81
+
82
+ # Deletes: var data = \"
83
+ json.delete_prefix!(@prefix + '"')
84
+
85
+ # Deletes: \";
86
+ # Deletes: \";\n
87
+ json.delete_suffix!('"' + @suffix)
88
+ json.delete_suffix!('"' + @suffix + "\n")
89
+
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,62 @@
1
+ require 'oj'
2
+ require_relative 'adapter.rb'
3
+
4
+ ##
5
+ # Synchronous file system.
6
+ ##
7
+ class Rowdb
8
+ class Sync < Adapter
9
+
10
+ ##
11
+ # Load a JSON string from a file.
12
+ #
13
+ # @return Hash data
14
+ ##
15
+ def read()
16
+
17
+ json = nil
18
+
19
+ # Load JSON inside a Javascript variable.
20
+ if @format == :js && File.exist?(@source)
21
+ File.open(@source, 'r') do |file|
22
+ json = file.read
23
+ # Fix double encoding issue due to JSON string becoming Ruby string.
24
+ json.gsub!('\\"', '"')
25
+ unwrap(json)
26
+ end
27
+ # Load JSON string.
28
+ else
29
+ json = Oj.load_file(@source)
30
+ end
31
+
32
+ unless json.nil?
33
+
34
+ # Parse JSON.
35
+ data = Oj.load(json)
36
+ return data.transform_keys(&:to_sym)
37
+
38
+ end
39
+
40
+ return nil
41
+
42
+ end
43
+
44
+ ##
45
+ # Save a Hash to a file as a JSON string or JS.
46
+ #
47
+ # @param Hash data
48
+ ##
49
+ def write(data)
50
+
51
+ json = Oj.dump(data, mode: :compat)
52
+
53
+ Oj.to_file(@source, json)
54
+
55
+ if @format == :js
56
+ wrap()
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+ end
@@ -1,5 +1,11 @@
1
+ ################################################################################
2
+ # A local JSON database using Rudash for easy Hash traversal.
3
+ #
4
+ # @author Maedi Prichard
5
+ ################################################################################
6
+
1
7
  require 'rudash'
2
- require_relative 'adapters/Sync.rb'
8
+ require_relative 'adapters/sync.rb'
3
9
 
4
10
  class Rowdb
5
11
 
@@ -37,7 +43,6 @@ class Rowdb
37
43
  end
38
44
 
39
45
  def push(value)
40
-
41
46
  if @get_path.nil?
42
47
  raise StandardError.new "You must get() before push()."
43
48
  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.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maedi Prichard
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-26 00:00:00.000000000 Z
11
+ date: 2021-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -38,21 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: A local JSON database built in Ruby inspired by lowdb, using Rudash for
42
- easy Hash traversal.
41
+ description: A local JSON database using Rudash for easy Hash traversal.
43
42
  email: maediprichard@gmail.com
44
43
  executables: []
45
44
  extensions: []
46
45
  extra_rdoc_files: []
47
46
  files:
48
- - lib/adapters/Adapter.rb
49
- - lib/adapters/Sync.rb
47
+ - lib/adapters/adapter.rb
48
+ - lib/adapters/sync.rb
50
49
  - lib/rowdb.rb
51
50
  homepage: https://github.com/rowdb/rowdb
52
51
  licenses:
53
52
  - MPL-2.0
54
53
  metadata: {}
55
- post_install_message:
54
+ post_install_message:
56
55
  rdoc_options: []
57
56
  require_paths:
58
57
  - lib
@@ -67,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
66
  - !ruby/object:Gem::Version
68
67
  version: '0'
69
68
  requirements: []
70
- rubygems_version: 3.0.2
71
- signing_key:
69
+ rubygems_version: 3.0.3
70
+ signing_key:
72
71
  specification_version: 4
73
72
  summary: A local JSON database.
74
73
  test_files: []
@@ -1,91 +0,0 @@
1
- class Adapter
2
-
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
-
89
- end
90
-
91
- end
@@ -1,60 +0,0 @@
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 = 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
30
-
31
- unless json.nil?
32
-
33
- # Parse JSON.
34
- data = Oj.load(json)
35
- return data.transform_keys(&:to_sym)
36
-
37
- end
38
-
39
- return nil
40
-
41
- end
42
-
43
- ##
44
- # Save a Hash to a file as a JSON string or JS.
45
- #
46
- # @param Hash data
47
- ##
48
- def write(data)
49
-
50
- json = Oj.dump(data, mode: :compat)
51
-
52
- Oj.to_file(@source, json)
53
-
54
- if @format == :js
55
- wrap()
56
- end
57
-
58
- end
59
-
60
- end