rowdb 0.2.0 → 0.6.0

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: 3cf7404b803a91a97b9559d4505e3473bfc473ce9d8263611126df0a1769fb63
4
- data.tar.gz: '08f244e70a79daeea781893beac76fbc6cdfba71f5ba1745c086daf21de55553'
3
+ metadata.gz: 002f4d85a85041fb086b84afc4afca0d2f69a23db592296a9e43c0bc87e42429
4
+ data.tar.gz: 7e9dffa6eacb363d53735498b791b2ddf63ecf40ad5433a910ff057703695d24
5
5
  SHA512:
6
- metadata.gz: fae4984ed663c29927c3b0116fdc73bd0b04f8c0668f6a2b69bfc7d98568167d7d0e238dee39db06377b23b2f8d5eb9a422b89e4617b429c600901fcb437cecd
7
- data.tar.gz: ede01a4637d98a31b6956058c1069b0562edc5faa3e01c6b907754006797407d0b27b3e2ca2f6792a33567334bc386c079e234a42f64ede63aa0aa6f46863ed2
6
+ metadata.gz: 9c8fc6df80d12dad2a8cce4cb7e9be9cda0346fb1d346e80f1d1515e6013d9fd59e09186847ba76aa6a978d9b0a398370382b7919e2e3ea0f98d4f5bfd77422b
7
+ data.tar.gz: f35a9d0e10fba829d3c3ec3e77eae8ab9aab547f94f0cb4a770c51e70741176a649eabc83290d37b9d0c49111975074f2757f4b307fbc822487b893a31415333
@@ -1,7 +1,75 @@
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(json)
57
+
58
+ json.prepend(@prefix)
59
+ json << @suffix
60
+
61
+ end
62
+
63
+ ##
64
+ # Unwrap JSON from a Javascript variable.
65
+ #
66
+ # @param String json
67
+ ##
68
+ def unwrap(json)
69
+
70
+ json.delete_prefix!(@prefix)
71
+ json.delete_suffix!(@suffix)
72
+
5
73
  end
6
74
 
7
75
  end
@@ -16,7 +16,15 @@ class Sync < Adapter
16
16
  json = Oj.load_file(@source)
17
17
 
18
18
  unless json.nil?
19
- return Oj.load(json)
19
+
20
+ if @format == :js
21
+ unwrap(json)
22
+ end
23
+
24
+ # Parse JSON.
25
+ data = Oj.load(json)
26
+ return data.transform_keys(&:to_sym)
27
+
20
28
  end
21
29
 
22
30
  return nil
@@ -24,13 +32,18 @@ class Sync < Adapter
24
32
  end
25
33
 
26
34
  ##
27
- # Save a Hash to a file as JSON.
35
+ # Save a Hash to a file as a JSON string or JS.
28
36
  #
29
37
  # @param Hash data
30
38
  ##
31
39
  def write(data)
32
40
 
33
- json = Oj.dump(data)
41
+ json = Oj.dump(data, mode: :compat)
42
+
43
+ if @format == :js
44
+ wrap(json)
45
+ end
46
+
34
47
  Oj.to_file(@source, json)
35
48
 
36
49
  end
@@ -3,62 +3,69 @@ require_relative 'adapters/Sync.rb'
3
3
 
4
4
  class Rowdb
5
5
 
6
- def initialize(adapter = :file_system, 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())
12
+ @get_path = nil
13
+
9
14
  end
10
15
 
16
+ # Set default data.
11
17
  def defaults(data)
12
- if @data.value().nil?
13
- # Load default data.
14
- @data = R_.chain(data)
15
- # Save data to disk.
16
- @adapter.write(data)
18
+ if @chain.value().nil?
19
+ @chain = R_.chain(data.transform_keys(&:to_sym))
17
20
  end
18
21
  self
19
22
  end
20
23
 
21
24
  def get(path)
22
- @data.get(path)
25
+ @get_path = path
26
+ @chain.get(path)
23
27
  self
24
28
  end
25
29
 
26
30
  def set(path, value)
27
- @data.set(path, value)
31
+ @chain.set(path, value)
28
32
  self
29
33
  end
30
34
 
31
35
  def value()
32
- @data.value()
36
+ @chain.value()
37
+ end
38
+
39
+ def push(value)
40
+
41
+ if @get_path.nil?
42
+ raise StandardError.new "You must get() before push()."
43
+ end
44
+
45
+ # Add value to end of array.
46
+ adder = -> (items) {
47
+ [*items, value]
48
+ }
49
+ R_.update(@chain.value(), @get_path, adder)
50
+
51
+ self
33
52
  end
34
53
 
35
54
  def write()
36
- @adapter.write(@data.value())
55
+ @adapter.write(@chain.value())
56
+ self
37
57
  end
38
58
 
39
59
  private
40
60
 
41
- def sync(file_path)
42
- Sync.new(file_path)
43
- end
44
-
45
61
  ##
46
- # Normalize path.
62
+ # Adapters.
47
63
  #
48
- # @param file_path - An absolute or relative path.
49
- # @return An absolute path.
64
+ # The chosen adapter is initialized by the constructor.
50
65
  ##
51
- def normalize_path(file_path)
52
-
53
- # Absolute path.
54
- if file_path.start_with? '/'
55
- return file_path
56
- # Relative path.
57
- else
58
- # Get directory the script executed from.
59
- return File.join(Dir.pwd, '/' + file_path)
60
- end
61
66
 
67
+ def sync(file_path, js_var)
68
+ Sync.new(file_path, js_var)
62
69
  end
63
70
 
64
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.2.0
4
+ version: 0.6.0
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-29 00:00:00.000000000 Z
11
+ date: 2020-10-14 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: {}