smart_kv 0.2.7 → 0.2.8.1
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/README.md +23 -1
- data/lib/smart_kv.rb +4 -4
- data/lib/smart_kv/{meat.rb → core.rb} +14 -17
- data/lib/smart_kv/errors.rb +1 -1
- data/lib/smart_kv/{convert.rb → helper.rb} +11 -1
- data/lib/smart_kv/version.rb +1 -1
- metadata +5 -7
- data/lib/smart_kv/check.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2afa3abc6fd5ba511ca89207c137cdb000db8951a9ee102f60436eddc7c93e1f
|
4
|
+
data.tar.gz: b8afd1a1fc115af8bccd6ce4b78396eb38beb83f245ed1716e9d80dc25284ba8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76915e48518766aece378cfb2c9bb293f5a8001055b3606f7c415a49571ec2c27efb063a8c8d546c589927050a008b1d3bc96b576016731e34ceaeca7fdf1198
|
7
|
+
data.tar.gz: f5e2f791d15a128d681a302ad6f49b2bf7b37a876eaa8fcf51b2170d33899ef386c4946a13627d1c74ba301f67851c23cf34be139f41cfbff6b658c60baba769
|
data/README.md
CHANGED
@@ -41,7 +41,7 @@ So, when you do this `d.change(hour: 1, minute: 5)`, it will yell:
|
|
41
41
|
KeyError: unrecognized key: :minute in ChangeOptions
|
42
42
|
```
|
43
43
|
|
44
|
-
|
44
|
+
That's better. But, how do you know all the right options?
|
45
45
|
Type: `ChangeOptions.keys`
|
46
46
|
|
47
47
|
|
@@ -102,6 +102,23 @@ c.abcd #=> 123
|
|
102
102
|
```
|
103
103
|
|
104
104
|
|
105
|
+
### Get suggestions
|
106
|
+
|
107
|
+
When you make typo when writing a key of an options, you can get suggestions.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
class LanguageScore < SmartKv
|
111
|
+
required :ruby
|
112
|
+
optional :rust, :kotlin, :elixir
|
113
|
+
end
|
114
|
+
|
115
|
+
LanguageScore.check({ruby: 100, korting: 90})
|
116
|
+
|
117
|
+
# SmartKv::KeyError: unrecognized key: :korting in LanguageScore.
|
118
|
+
# Did you mean? :kotlin
|
119
|
+
```
|
120
|
+
|
121
|
+
|
105
122
|
### Not using it for options or configs?
|
106
123
|
|
107
124
|
You can choose not to use it for options or configs. Maybe for strict request body keys?
|
@@ -115,6 +132,11 @@ end
|
|
115
132
|
request.set_form_data(PostBody.check({app_key: "abc", secret_key: "def"}))
|
116
133
|
```
|
117
134
|
|
135
|
+
### Alternative
|
136
|
+
|
137
|
+
One close alternative that is quite similar in objective is probably
|
138
|
+
[dry-schema](https://github.com/dry-rb/dry-schema).
|
139
|
+
|
118
140
|
|
119
141
|
## Installation
|
120
142
|
|
data/lib/smart_kv.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require_relative "smart_kv/version"
|
2
|
-
require_relative "smart_kv/
|
3
|
-
require_relative "smart_kv/
|
2
|
+
require_relative "smart_kv/helper"
|
3
|
+
require_relative "smart_kv/core"
|
4
4
|
|
5
5
|
class SmartKv
|
6
|
-
if
|
6
|
+
if Helper.has_did_you_mean_key_error?
|
7
7
|
require_relative "smart_kv/did_you_mean"
|
8
8
|
end
|
9
9
|
|
10
|
-
extend
|
10
|
+
extend Core
|
11
11
|
end
|
@@ -1,10 +1,9 @@
|
|
1
|
+
require 'set'
|
1
2
|
require_relative "errors"
|
2
|
-
require_relative "
|
3
|
+
require_relative "helper"
|
3
4
|
|
4
|
-
module SmartKv::
|
5
|
-
|
6
|
-
|
7
|
-
include SmartKv::Convert
|
5
|
+
module SmartKv::Core
|
6
|
+
include SmartKv::Helper
|
8
7
|
|
9
8
|
def required(*args)
|
10
9
|
init_required
|
@@ -19,7 +18,7 @@ module SmartKv::Meat
|
|
19
18
|
end
|
20
19
|
|
21
20
|
def init_required
|
22
|
-
@required ||= superclass == SmartKv ? Set.new : superclass.required.dup
|
21
|
+
@required ||= superclass == SmartKv ? ::Set.new : superclass.required.dup
|
23
22
|
end
|
24
23
|
|
25
24
|
def optional(*args)
|
@@ -35,33 +34,31 @@ module SmartKv::Meat
|
|
35
34
|
end
|
36
35
|
|
37
36
|
def init_optional
|
38
|
-
@optional ||= superclass == SmartKv ? Set.new : superclass.optional.dup
|
37
|
+
@optional ||= superclass == SmartKv ? ::Set.new : superclass.optional.dup
|
39
38
|
end
|
40
39
|
|
41
40
|
def keys
|
42
|
-
|
41
|
+
init_required; init_optional
|
42
|
+
@required + @optional
|
43
43
|
end
|
44
44
|
|
45
45
|
def check(kv={})
|
46
|
-
|
46
|
+
prevent_direct_usage
|
47
47
|
|
48
48
|
object_class = callable_class || kv.class
|
49
49
|
kv = kv.dup
|
50
50
|
|
51
|
-
unless SmartKv::
|
52
|
-
required_keys = Array(@required)
|
53
|
-
optional_keys = Array(@optional)
|
54
|
-
|
51
|
+
unless SmartKv::Helper.production?
|
55
52
|
hash = kv.to_h
|
56
53
|
missing_keys = required_keys - hash.keys
|
57
54
|
unless missing_keys.empty?
|
58
55
|
raise SmartKv::KeyError, "missing required key(s): #{missing_keys.map{|k| k.to_sym.inspect }.join(', ')} in #{self.class}"
|
59
56
|
end
|
60
57
|
|
61
|
-
unrecognized_keys = hash.keys -
|
58
|
+
unrecognized_keys = hash.keys - keys.to_a
|
62
59
|
unless unrecognized_keys.empty?
|
63
60
|
key = unrecognized_keys.first
|
64
|
-
raise SmartKv::KeyError.new("unrecognized key: #{key.inspect}
|
61
|
+
raise SmartKv::KeyError.new("unrecognized key: #{key.inspect} in #{self}.", key: key, receiver: (keys - hash.keys).map {|k| [k, nil] }.to_h)
|
65
62
|
end
|
66
63
|
end
|
67
64
|
|
@@ -79,9 +76,9 @@ module SmartKv::Meat
|
|
79
76
|
|
80
77
|
private
|
81
78
|
|
82
|
-
def
|
79
|
+
def prevent_direct_usage
|
83
80
|
if self == SmartKv
|
84
|
-
raise SmartKv::
|
81
|
+
raise SmartKv::DirectUsageError, "only subclass of SmartKv is meant to be used".freeze
|
85
82
|
end
|
86
83
|
end
|
87
84
|
end
|
data/lib/smart_kv/errors.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module SmartKv::
|
1
|
+
module SmartKv::Helper
|
2
2
|
def to_callable_object(object_class, kv)
|
3
3
|
if object_class == Struct
|
4
4
|
Struct.new(*kv.to_h.keys).new(*kv.to_h.values)
|
@@ -10,4 +10,14 @@ module SmartKv::Convert
|
|
10
10
|
object_class.new(kv.to_h)
|
11
11
|
end
|
12
12
|
end
|
13
|
+
|
14
|
+
module_function
|
15
|
+
|
16
|
+
def has_did_you_mean_key_error?
|
17
|
+
defined?(DidYouMean::KeyErrorChecker)
|
18
|
+
end
|
19
|
+
|
20
|
+
def production?
|
21
|
+
(ENV['RAILS_ENV'] || ENV['RACK_ENV']) == "production"
|
22
|
+
end
|
13
23
|
end
|
data/lib/smart_kv/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_kv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Setyadi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -127,11 +127,10 @@ files:
|
|
127
127
|
- bin/console
|
128
128
|
- bin/setup
|
129
129
|
- lib/smart_kv.rb
|
130
|
-
- lib/smart_kv/
|
131
|
-
- lib/smart_kv/convert.rb
|
130
|
+
- lib/smart_kv/core.rb
|
132
131
|
- lib/smart_kv/did_you_mean.rb
|
133
132
|
- lib/smart_kv/errors.rb
|
134
|
-
- lib/smart_kv/
|
133
|
+
- lib/smart_kv/helper.rb
|
135
134
|
- lib/smart_kv/version.rb
|
136
135
|
- smart_kv.gemspec
|
137
136
|
homepage: https://github.com/styd/smart_kv
|
@@ -153,8 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
152
|
- !ruby/object:Gem::Version
|
154
153
|
version: '0'
|
155
154
|
requirements: []
|
156
|
-
|
157
|
-
rubygems_version: 2.7.6
|
155
|
+
rubygems_version: 3.1.2
|
158
156
|
signing_key:
|
159
157
|
specification_version: 4
|
160
158
|
summary: Smart checks for your options or configurations.
|
data/lib/smart_kv/check.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
module SmartKv::Check
|
2
|
-
def has_did_you_mean_key_error?
|
3
|
-
defined?(DidYouMean::KeyErrorChecker)
|
4
|
-
end
|
5
|
-
module_function :has_did_you_mean_key_error?
|
6
|
-
|
7
|
-
def production?
|
8
|
-
(ENV['RAILS_ENV'] || ENV['RACK_ENV']) == "production"
|
9
|
-
end
|
10
|
-
module_function :production?
|
11
|
-
end
|