rowdb 0.4.0 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/adapters/Adapter.rb +84 -2
- data/lib/adapters/Sync.rb +26 -4
- data/lib/rowdb.rb +23 -36
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2a31c07f4396d143f045c618fd8ad2aa0e5ef720106527672cd884fbe6e01f8
|
4
|
+
data.tar.gz: e51f5584cf508ff4bc1a821d01dd2c43d17103d6946a20219210fd6ec1f2597b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a344384ec5b4e6ccbb991b69da281c3a7660b877c7b84bdf38a85191d5520bb428e6abb2a0b343f2458284de038882d6772045c9be56cb5c5dc6d472898ebf5
|
7
|
+
data.tar.gz: 8ab7dd2603f247ec584f25ae5985c4ab27ea68a1ba1b9d3a79c266324defc67c407204e78afe45972f7b9e1ace5df2237ad1024e7c1dfcf0aa05da3aa4e064f6
|
data/lib/adapters/Adapter.rb
CHANGED
@@ -1,7 +1,89 @@
|
|
1
1
|
class Adapter
|
2
2
|
|
3
|
-
def initialize(
|
4
|
-
|
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
|
data/lib/adapters/Sync.rb
CHANGED
@@ -13,10 +13,27 @@ class Sync < Adapter
|
|
13
13
|
##
|
14
14
|
def read()
|
15
15
|
|
16
|
-
json =
|
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
|
-
|
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
|
data/lib/rowdb.rb
CHANGED
@@ -3,82 +3,69 @@ require_relative 'adapters/Sync.rb'
|
|
3
3
|
|
4
4
|
class Rowdb
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
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)
|
11
10
|
|
12
|
-
|
13
|
-
@adapter = self.send(adapter, normalize_path(file_path))
|
14
|
-
@data = R_.chain(@adapter.read())
|
11
|
+
@chain = R_.chain(@adapter.read())
|
15
12
|
@get_path = nil
|
13
|
+
|
16
14
|
end
|
17
15
|
|
16
|
+
# Set default data.
|
18
17
|
def defaults(data)
|
19
|
-
if @
|
20
|
-
|
21
|
-
@data = R_.chain(data)
|
22
|
-
# Save data to disk.
|
23
|
-
@adapter.write(data)
|
18
|
+
if @chain.value().nil?
|
19
|
+
@chain = R_.chain(data.transform_keys(&:to_sym))
|
24
20
|
end
|
25
21
|
self
|
26
22
|
end
|
27
23
|
|
28
24
|
def get(path)
|
29
25
|
@get_path = path
|
30
|
-
@
|
26
|
+
@chain.get(path)
|
31
27
|
self
|
32
28
|
end
|
33
29
|
|
34
30
|
def set(path, value)
|
35
|
-
@
|
31
|
+
@chain.set(path, value)
|
36
32
|
self
|
37
33
|
end
|
38
34
|
|
39
35
|
def value()
|
40
|
-
@
|
36
|
+
@chain.value()
|
41
37
|
end
|
42
38
|
|
43
39
|
def push(value)
|
40
|
+
|
44
41
|
if @get_path.nil?
|
45
42
|
raise StandardError.new "You must get() before push()."
|
46
43
|
end
|
47
44
|
|
48
|
-
|
49
|
-
|
45
|
+
# Add value to end of array.
|
46
|
+
adder = -> (items) {
|
47
|
+
[*items, value]
|
48
|
+
}
|
49
|
+
R_.update(@chain.value(), @get_path, adder)
|
50
50
|
|
51
51
|
self
|
52
52
|
end
|
53
53
|
|
54
54
|
def write()
|
55
|
-
@adapter.write(@
|
55
|
+
@adapter.write(@chain.value())
|
56
56
|
self
|
57
57
|
end
|
58
58
|
|
59
59
|
private
|
60
60
|
|
61
|
-
def sync(file_path)
|
62
|
-
Sync.new(file_path)
|
63
|
-
end
|
64
|
-
|
65
61
|
##
|
66
|
-
#
|
62
|
+
# Adapters.
|
67
63
|
#
|
68
|
-
#
|
69
|
-
# @return An absolute path.
|
64
|
+
# The chosen adapter is initialized by the constructor.
|
70
65
|
##
|
71
|
-
def normalize_path(file_path)
|
72
|
-
|
73
|
-
# Absolute path.
|
74
|
-
if file_path.start_with? '/'
|
75
|
-
return file_path
|
76
|
-
# Relative path.
|
77
|
-
else
|
78
|
-
# Get directory the script executed from.
|
79
|
-
return File.join(Dir.pwd, '/' + file_path)
|
80
|
-
end
|
81
66
|
|
67
|
+
def sync(file_path, js_var)
|
68
|
+
Sync.new(file_path, js_var)
|
82
69
|
end
|
83
70
|
|
84
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
|
+
version: 0.6.2
|
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-
|
11
|
+
date: 2020-10-15 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
|
42
|
-
Hash traversal.
|
43
|
-
email: maediprichard@
|
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: []
|