smart_kv 0.2.3 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/CODE_OF_CONDUCT.md +1 -1
- data/lib/smart_kv.rb +5 -55
- data/lib/smart_kv/check.rb +11 -0
- data/lib/smart_kv/convert.rb +13 -0
- data/lib/smart_kv/did_you_mean.rb +0 -2
- data/lib/smart_kv/errors.rb +1 -1
- data/lib/smart_kv/meat.rb +77 -0
- data/lib/smart_kv/version.rb +1 -1
- data/smart_kv.gemspec +3 -3
- metadata +13 -11
- data/lib/smart_kv/register.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58e421790894c41c5608d79fcfb86c604b18e9f98f0a1591e76589358a5f31c9
|
4
|
+
data.tar.gz: 7916f3b61f1d53cd56ee4db704ea5424dd9b5b764dfefd2946eadd8eea3c2452
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41edbc48494258f1c55db913009e6cb722f2f70bbcba721afbc6d0a86b17f507b919dd58086aecbf2f6b8a78a00b474e12786c1bfca1aaa29cf62bcfcff1f844
|
7
|
+
data.tar.gz: 9a9b0280c67b828262442b27bebc28e70b77922facd2115e5dd19d5091c0959f705cb938cae5432d2888a1a4a3dbcb2ca37a0e653c0e902163c3266498e5abc2
|
data/.travis.yml
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at
|
58
|
+
reported by contacting the project team at a.styd@yahoo.com. All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
data/lib/smart_kv.rb
CHANGED
@@ -1,61 +1,11 @@
|
|
1
|
-
def has_did_you_mean_key_error?
|
2
|
-
!ENV['TRAVIS'] && Gem::Version.new(RUBY_VERSION) > Gem::Version.new("2.5.0") && defined?(DidYouMean)
|
3
|
-
end
|
4
|
-
|
5
1
|
require_relative "smart_kv/version"
|
6
|
-
require_relative "smart_kv/
|
7
|
-
require_relative "smart_kv/
|
8
|
-
|
9
|
-
if has_did_you_mean_key_error?
|
10
|
-
require_relative "smart_kv/did_you_mean"
|
11
|
-
end
|
2
|
+
require_relative "smart_kv/check"
|
3
|
+
require_relative "smart_kv/meat"
|
12
4
|
|
13
5
|
class SmartKv
|
14
|
-
|
15
|
-
|
16
|
-
attr_reader :object_class
|
17
|
-
|
18
|
-
def initialize(required_keys = [], optional_keys = [], object_class = nil, kv = {})
|
19
|
-
prevent_direct_instantiation
|
20
|
-
|
21
|
-
@object_class = object_class || kv.class
|
22
|
-
@kv = kv.dup
|
23
|
-
|
24
|
-
hash = kv.to_h
|
25
|
-
missing_keys = required_keys - hash.keys
|
26
|
-
unless missing_keys.empty?
|
27
|
-
raise KeyError, "missing required key(s): #{missing_keys.map{|k| k.to_sym.inspect }.join(', ')} in #{self.class}"
|
28
|
-
end
|
29
|
-
|
30
|
-
unrecognized_keys = hash.keys - required_keys - optional_keys
|
31
|
-
unless unrecognized_keys.empty?
|
32
|
-
key = unrecognized_keys.first
|
33
|
-
raise KeyError.new("key not found: #{key.inspect}.", key: key, receiver: (keys - hash.keys).map {|k| [k, nil] }.to_h)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def keys
|
38
|
-
Array(self.class.required) + Array(self.class.optional)
|
6
|
+
if Check.has_did_you_mean_key_error?
|
7
|
+
require_relative "smart_kv/did_you_mean"
|
39
8
|
end
|
40
9
|
|
41
|
-
|
42
|
-
@object ||= if @object_class == Struct
|
43
|
-
Struct.new(*@kv.to_h.keys).new(*@kv.to_h.values)
|
44
|
-
elsif @object_class < Struct
|
45
|
-
@object_class.new(*@kv.to_h.values)
|
46
|
-
elsif @object_class <= Hash
|
47
|
-
@kv
|
48
|
-
else
|
49
|
-
@object_class.new(@kv.to_h)
|
50
|
-
end
|
51
|
-
@object.send(m, *args)
|
52
|
-
end
|
53
|
-
|
54
|
-
protected
|
55
|
-
|
56
|
-
def prevent_direct_instantiation
|
57
|
-
if self.class == SmartKv
|
58
|
-
raise InitializationError, "only subclass of SmartConfig can be instantiated"
|
59
|
-
end
|
60
|
-
end
|
10
|
+
extend Meat
|
61
11
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module SmartKv::Check
|
2
|
+
def has_did_you_mean_key_error?
|
3
|
+
!ENV['TRAVIS'] && 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
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SmartKv::Convert
|
2
|
+
def to_callable_object(object_class, kv)
|
3
|
+
if object_class == Struct
|
4
|
+
Struct.new(*kv.to_h.keys).new(*kv.to_h.values)
|
5
|
+
elsif object_class < Struct
|
6
|
+
object_class.new(*kv.to_h.values)
|
7
|
+
elsif object_class <= Hash
|
8
|
+
kv.to_h
|
9
|
+
else
|
10
|
+
object_class.new(kv.to_h)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/smart_kv/errors.rb
CHANGED
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative "errors"
|
2
|
+
require_relative "convert"
|
3
|
+
|
4
|
+
module SmartKv::Meat
|
5
|
+
# The meat of the gem
|
6
|
+
|
7
|
+
include SmartKv::Convert
|
8
|
+
|
9
|
+
def required(*args)
|
10
|
+
@required ||= superclass == SmartKv ? Set.new : superclass.required_keys.dup
|
11
|
+
@required += args
|
12
|
+
@optional -= @required if @optional
|
13
|
+
@required
|
14
|
+
end
|
15
|
+
|
16
|
+
def required_keys
|
17
|
+
@required.to_a
|
18
|
+
end
|
19
|
+
|
20
|
+
def optional(*args)
|
21
|
+
@optional ||= superclass == SmartKv ? Set.new : superclass.optional_keys.dup
|
22
|
+
@optional += args
|
23
|
+
@required -= @optional if @required
|
24
|
+
@optional
|
25
|
+
end
|
26
|
+
|
27
|
+
def optional_keys
|
28
|
+
@optional.to_a
|
29
|
+
end
|
30
|
+
|
31
|
+
def keys
|
32
|
+
Array(@required) + Array(@optional)
|
33
|
+
end
|
34
|
+
|
35
|
+
def check(kv={})
|
36
|
+
prevent_direct_instantiation
|
37
|
+
|
38
|
+
object_class = callable_class || kv.class
|
39
|
+
kv = kv.dup
|
40
|
+
|
41
|
+
unless SmartKv::Check.production?
|
42
|
+
required_keys = Array(@required)
|
43
|
+
optional_keys = Array(@optional)
|
44
|
+
|
45
|
+
hash = kv.to_h
|
46
|
+
missing_keys = required_keys - hash.keys
|
47
|
+
unless missing_keys.empty?
|
48
|
+
raise SmartKv::KeyError, "missing required key(s): #{missing_keys.map{|k| k.to_sym.inspect }.join(', ')} in #{self.class}"
|
49
|
+
end
|
50
|
+
|
51
|
+
unrecognized_keys = hash.keys - required_keys - optional_keys
|
52
|
+
unless unrecognized_keys.empty?
|
53
|
+
key = unrecognized_keys.first
|
54
|
+
raise SmartKv::KeyError.new("key not found: #{key.inspect}.", key: key, receiver: (keys - hash.keys).map {|k| [k, nil] }.to_h)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
return to_callable_object(object_class, kv)
|
59
|
+
end
|
60
|
+
alias_method :new, :check
|
61
|
+
|
62
|
+
def callable_as(klass)
|
63
|
+
@callable_as = superclass == SmartKv ? klass : superclass.callable_class
|
64
|
+
end
|
65
|
+
|
66
|
+
def callable_class
|
67
|
+
@callable_as
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def prevent_direct_instantiation
|
73
|
+
if self == SmartKv
|
74
|
+
raise SmartKv::InitializationError, "only subclass of SmartConfig can be instantiated".freeze
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/smart_kv/version.rb
CHANGED
data/smart_kv.gemspec
CHANGED
@@ -23,10 +23,10 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
|
26
|
-
spec.add_development_dependency "bundler", "~>
|
27
|
-
spec.add_development_dependency "rake", "~>
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 12.3"
|
28
28
|
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
-
spec.add_development_dependency "coveralls"
|
29
|
+
spec.add_development_dependency "coveralls", "~> 0.8.22"
|
30
30
|
spec.add_development_dependency "pry", "~> 0.12"
|
31
31
|
spec.add_development_dependency "pry-byebug", "~> 3.6"
|
32
32
|
spec.add_development_dependency "pry-doc"
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Setyadi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '12.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '12.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: coveralls
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 0.8.22
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 0.8.22
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: pry
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,9 +128,11 @@ files:
|
|
128
128
|
- bin/console
|
129
129
|
- bin/setup
|
130
130
|
- lib/smart_kv.rb
|
131
|
+
- lib/smart_kv/check.rb
|
132
|
+
- lib/smart_kv/convert.rb
|
131
133
|
- lib/smart_kv/did_you_mean.rb
|
132
134
|
- lib/smart_kv/errors.rb
|
133
|
-
- lib/smart_kv/
|
135
|
+
- lib/smart_kv/meat.rb
|
134
136
|
- lib/smart_kv/version.rb
|
135
137
|
- smart_kv.gemspec
|
136
138
|
homepage: https://github.com/styd/smart_kv
|
data/lib/smart_kv/register.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module SmartKv::Register
|
2
|
-
def required(*args)
|
3
|
-
@required ||= superclass == SmartKv ? Set.new : superclass.required_keys.dup
|
4
|
-
@required += args
|
5
|
-
@optional -= @required if @optional
|
6
|
-
@required
|
7
|
-
end
|
8
|
-
|
9
|
-
def required_keys
|
10
|
-
@required.to_a
|
11
|
-
end
|
12
|
-
|
13
|
-
def optional(*args)
|
14
|
-
@optional ||= superclass == SmartKv ? Set.new : superclass.optional_keys.dup
|
15
|
-
@optional += args
|
16
|
-
@required -= @optional if @required
|
17
|
-
@optional
|
18
|
-
end
|
19
|
-
|
20
|
-
def optional_keys
|
21
|
-
@optional.to_a
|
22
|
-
end
|
23
|
-
|
24
|
-
def new(*args)
|
25
|
-
super(@required.to_a, @optional.to_a, @callable_as, *args)
|
26
|
-
end
|
27
|
-
|
28
|
-
def callable_as(klass)
|
29
|
-
@callable_as = superclass == SmartKv ? klass : superclass.callable_class
|
30
|
-
end
|
31
|
-
|
32
|
-
def callable_class
|
33
|
-
@callable_as
|
34
|
-
end
|
35
|
-
end
|