rowdb 0.5.1 → 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 +4 -4
- data/lib/adapters/Adapter.rb +70 -2
- data/lib/adapters/Sync.rb +13 -1
- data/lib/rowdb.rb +10 -19
- 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: 002f4d85a85041fb086b84afc4afca0d2f69a23db592296a9e43c0bc87e42429
|
4
|
+
data.tar.gz: 7e9dffa6eacb363d53735498b791b2ddf63ecf40ad5433a910ff057703695d24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c8fc6df80d12dad2a8cce4cb7e9be9cda0346fb1d346e80f1d1515e6013d9fd59e09186847ba76aa6a978d9b0a398370382b7919e2e3ea0f98d4f5bfd77422b
|
7
|
+
data.tar.gz: f35a9d0e10fba829d3c3ec3e77eae8ab9aab547f94f0cb4a770c51e70741176a649eabc83290d37b9d0c49111975074f2757f4b307fbc822487b893a31415333
|
data/lib/adapters/Adapter.rb
CHANGED
@@ -1,7 +1,75 @@
|
|
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(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
|
data/lib/adapters/Sync.rb
CHANGED
@@ -16,8 +16,15 @@ class Sync < Adapter
|
|
16
16
|
json = Oj.load_file(@source)
|
17
17
|
|
18
18
|
unless json.nil?
|
19
|
+
|
20
|
+
if @format == :js
|
21
|
+
unwrap(json)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Parse JSON.
|
19
25
|
data = Oj.load(json)
|
20
26
|
return data.transform_keys(&:to_sym)
|
27
|
+
|
21
28
|
end
|
22
29
|
|
23
30
|
return nil
|
@@ -25,13 +32,18 @@ class Sync < Adapter
|
|
25
32
|
end
|
26
33
|
|
27
34
|
##
|
28
|
-
# Save a Hash to a file as JSON.
|
35
|
+
# Save a Hash to a file as a JSON string or JS.
|
29
36
|
#
|
30
37
|
# @param Hash data
|
31
38
|
##
|
32
39
|
def write(data)
|
33
40
|
|
34
41
|
json = Oj.dump(data, mode: :compat)
|
42
|
+
|
43
|
+
if @format == :js
|
44
|
+
wrap(json)
|
45
|
+
end
|
46
|
+
|
35
47
|
Oj.to_file(@source, json)
|
36
48
|
|
37
49
|
end
|
data/lib/rowdb.rb
CHANGED
@@ -3,10 +3,14 @@ require_relative 'adapters/Sync.rb'
|
|
3
3
|
|
4
4
|
class Rowdb
|
5
5
|
|
6
|
-
def initialize(file_path, adapter = :sync)
|
7
|
-
|
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
|
+
|
8
11
|
@chain = R_.chain(@adapter.read())
|
9
12
|
@get_path = nil
|
13
|
+
|
10
14
|
end
|
11
15
|
|
12
16
|
# Set default data.
|
@@ -54,27 +58,14 @@ class Rowdb
|
|
54
58
|
|
55
59
|
private
|
56
60
|
|
57
|
-
def sync(file_path)
|
58
|
-
Sync.new(file_path)
|
59
|
-
end
|
60
|
-
|
61
61
|
##
|
62
|
-
#
|
62
|
+
# Adapters.
|
63
63
|
#
|
64
|
-
#
|
65
|
-
# @return An absolute path.
|
64
|
+
# The chosen adapter is initialized by the constructor.
|
66
65
|
##
|
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
66
|
|
67
|
+
def sync(file_path, js_var)
|
68
|
+
Sync.new(file_path, js_var)
|
78
69
|
end
|
79
70
|
|
80
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.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-
|
11
|
+
date: 2020-10-14 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: []
|