rowdb 0.5.1 → 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: 39c1806858f1a8d306f1ee05349654b9d368a8236f67ac980263639a0ba799b5
4
- data.tar.gz: 128eb45b82377dc04fa2ea226c1f962f18a494e7778f8da83c2fd0eaa0fa18cb
3
+ metadata.gz: ac3c5afe3b28edfd757cb704048e0c0a6d194a2392991997b5952ce021d31f92
4
+ data.tar.gz: 3e77f6c857320a74e9294fcfd1116ebd3107bb0ea9c96b526fbe06f6249d96b4
5
5
  SHA512:
6
- metadata.gz: c3c28ad22face7a22ed466bfe3d61b2c82ef8aee4a22d8d275979b4d9bbbaa9bef9b7b1b32150449f321b3ed4ab745e52fe7cc48df27049b917d8f94c36da175
7
- data.tar.gz: dd270b13e87c11753d2cdc50baf277c45cdd9fb637448e740ba3caeca40be4c107797b38fa9073b6bd71f3316d6462779d4e051849b566ccef55bd25c454d45f
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,12 +1,22 @@
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
 
6
- def initialize(file_path, adapter = :sync)
7
- @adapter = self.send(adapter, normalize_path(file_path))
12
+ def initialize(file_path, adapter = :sync, js_var = "db")
13
+
14
+ # Initialize the chosen adapter.
15
+ @adapter = self.send(adapter, file_path, js_var)
16
+
8
17
  @chain = R_.chain(@adapter.read())
9
18
  @get_path = nil
19
+
10
20
  end
11
21
 
12
22
  # Set default data.
@@ -33,7 +43,6 @@ class Rowdb
33
43
  end
34
44
 
35
45
  def push(value)
36
-
37
46
  if @get_path.nil?
38
47
  raise StandardError.new "You must get() before push()."
39
48
  end
@@ -54,27 +63,14 @@ class Rowdb
54
63
 
55
64
  private
56
65
 
57
- def sync(file_path)
58
- Sync.new(file_path)
59
- end
60
-
61
66
  ##
62
- # Normalize path.
67
+ # Adapters.
63
68
  #
64
- # @param file_path - An absolute or relative path.
65
- # @return An absolute path.
69
+ # The chosen adapter is initialized by the constructor.
66
70
  ##
67
- def normalize_path(file_path)
68
-
69
- # Absolute path.
70
- if file_path.start_with? '/'
71
- return file_path
72
- # Relative path.
73
- else
74
- # Get directory the script executed from.
75
- return File.join(Dir.pwd, '/' + file_path)
76
- end
77
71
 
72
+ def sync(file_path, js_var)
73
+ Sync.new(file_path, js_var)
78
74
  end
79
75
 
80
76
  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.5.1
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-08-21 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 for Ruby inspired by lowdb, using Rudash for easy
42
- Hash traversal.
43
- email: maediprichard@gmailcom
41
+ description: A local JSON database using Rudash for easy Hash traversal.
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,7 +0,0 @@
1
- class Adapter
2
-
3
- def initialize(source)
4
- @source = source
5
- end
6
-
7
- end
@@ -1,39 +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 = Oj.load_file(@source)
17
-
18
- unless json.nil?
19
- data = Oj.load(json)
20
- return data.transform_keys(&:to_sym)
21
- end
22
-
23
- return nil
24
-
25
- end
26
-
27
- ##
28
- # Save a Hash to a file as JSON.
29
- #
30
- # @param Hash data
31
- ##
32
- def write(data)
33
-
34
- json = Oj.dump(data, mode: :compat)
35
- Oj.to_file(@source, json)
36
-
37
- end
38
-
39
- end