rowdb 0.5.1 → 0.6.4
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 +93 -0
- data/lib/adapters/sync.rb +62 -0
- data/lib/rowdb.rb +17 -21
- metadata +10 -11
- data/lib/adapters/Adapter.rb +0 -7
- data/lib/adapters/Sync.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac3c5afe3b28edfd757cb704048e0c0a6d194a2392991997b5952ce021d31f92
|
4
|
+
data.tar.gz: 3e77f6c857320a74e9294fcfd1116ebd3107bb0ea9c96b526fbe06f6249d96b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/rowdb.rb
CHANGED
@@ -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/
|
8
|
+
require_relative 'adapters/sync.rb'
|
3
9
|
|
4
10
|
class Rowdb
|
5
11
|
|
6
|
-
def initialize(file_path, adapter = :sync)
|
7
|
-
|
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
|
-
#
|
67
|
+
# Adapters.
|
63
68
|
#
|
64
|
-
#
|
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.
|
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:
|
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
|
42
|
-
|
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/
|
49
|
-
- lib/adapters/
|
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.
|
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: []
|
data/lib/adapters/Adapter.rb
DELETED
data/lib/adapters/Sync.rb
DELETED
@@ -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
|