polykv 0.1.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 +7 -0
- data/lib/polykv.rb +112 -0
- metadata +42 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 44b562946ad1444889fbc840fdc67fbc20a0030723bd683a637ab542e351c024
|
|
4
|
+
data.tar.gz: 3ae7b468864c85712cbc05f9a33e94048c6ddcc03acd552dad4c9977dfd261ec
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7cf323b00f6cad7c60a3cef565b936b301db11d7c5cc413b0e6cc68a4c270bb7907549fd12f319bea219b493e12769dc47ab5f3bb562daeaeb6a0855f05ac7b3
|
|
7
|
+
data.tar.gz: 179dbc0ba7c7cf40e5fa153d91cd73599ca03a7b810982f6569fa21582fcf8bc741cb3931253e6dea74a858b021aac575f3f050dbc9ecdc82caa0e50d2f8c65d
|
data/lib/polykv.rb
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
|
|
4
|
+
class PolyKV
|
|
5
|
+
def initialize(app_name = 'polyKV')
|
|
6
|
+
@app_name = app_name
|
|
7
|
+
|
|
8
|
+
home = Dir.home
|
|
9
|
+
if Gem.win_platform?
|
|
10
|
+
config_dir = File.join(ENV['APPDATA'], app_name)
|
|
11
|
+
else
|
|
12
|
+
config_dir = File.join(home, '.config', app_name)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
FileUtils.mkdir_p(config_dir)
|
|
16
|
+
@file_path = File.join(config_dir, "#{app_name}.json")
|
|
17
|
+
@cache = {}
|
|
18
|
+
load
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def load
|
|
22
|
+
if File.exist?(@file_path)
|
|
23
|
+
@cache = JSON.parse(File.read(@file_path)) rescue {}
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def save
|
|
28
|
+
tmp_path = "#{@file_path}.tmp"
|
|
29
|
+
# Write to temp file with 0600 permissions immediately
|
|
30
|
+
File.open(tmp_path, 'w', 0600) do |f|
|
|
31
|
+
f.write(JSON.pretty_generate(@cache))
|
|
32
|
+
f.fsync
|
|
33
|
+
end
|
|
34
|
+
# Atomic rename
|
|
35
|
+
File.rename(tmp_path, @file_path)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def set_string(key, value)
|
|
39
|
+
@cache[key] = value
|
|
40
|
+
save
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def get_string(key)
|
|
44
|
+
@cache[key]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def set_number(key, value)
|
|
48
|
+
@cache[key] = value
|
|
49
|
+
save
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def get_number(key)
|
|
53
|
+
@cache[key]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def set_bool(key, value)
|
|
57
|
+
@cache[key] = value
|
|
58
|
+
save
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def get_bool(key)
|
|
62
|
+
@cache[key]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# --- Validation Helpers ---
|
|
66
|
+
|
|
67
|
+
def validate_value(value)
|
|
68
|
+
unless value.is_a?(String) || value.is_a?(Integer) || value.is_a?(Float) || value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
|
69
|
+
raise "PolyKV: Invalid type '#{value.class}'. Only String, Integer, Float, Boolean are allowed."
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def validate_map(map)
|
|
74
|
+
raise "PolyKV: Value must be a Hash (Map)." unless map.is_a?(Hash)
|
|
75
|
+
map.each_value { |v| validate_value(v) }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def validate_list(list)
|
|
79
|
+
raise "PolyKV: Value must be an Array (List)." unless list.is_a?(Array)
|
|
80
|
+
list.each { |v| validate_value(v) }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def set_map(key, value)
|
|
84
|
+
validate_map(value)
|
|
85
|
+
@cache[key] = value
|
|
86
|
+
save
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def get_map(key)
|
|
90
|
+
@cache[key]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def set_list(key, value)
|
|
94
|
+
validate_list(value)
|
|
95
|
+
@cache[key] = value
|
|
96
|
+
save
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def get_list(key)
|
|
100
|
+
@cache[key]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def remove(key)
|
|
104
|
+
@cache.delete(key)
|
|
105
|
+
save
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def clear
|
|
109
|
+
@cache = {}
|
|
110
|
+
save
|
|
111
|
+
end
|
|
112
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: polykv
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- PolyKV Team
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-12-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/polykv.rb
|
|
20
|
+
homepage:
|
|
21
|
+
licenses: []
|
|
22
|
+
metadata: {}
|
|
23
|
+
post_install_message:
|
|
24
|
+
rdoc_options: []
|
|
25
|
+
require_paths:
|
|
26
|
+
- lib
|
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
28
|
+
requirements:
|
|
29
|
+
- - ">="
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '0'
|
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
requirements: []
|
|
38
|
+
rubygems_version: 3.0.3.1
|
|
39
|
+
signing_key:
|
|
40
|
+
specification_version: 4
|
|
41
|
+
summary: Native PolyKV for Ruby
|
|
42
|
+
test_files: []
|