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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18d8a80239ecbfcc6b948865773902b355f964763d1c48970abdd15cd0fd776b
4
- data.tar.gz: 10b66b81f593f48ddbcfc3d9d2dcf53869c018b30ea2253e14b399973c12abb4
3
+ metadata.gz: 58e421790894c41c5608d79fcfb86c604b18e9f98f0a1591e76589358a5f31c9
4
+ data.tar.gz: 7916f3b61f1d53cd56ee4db704ea5424dd9b5b764dfefd2946eadd8eea3c2452
5
5
  SHA512:
6
- metadata.gz: 5af3e66863206152ad3c01518e383ee02e377b8e26be7706986c7761831e7d0ed1a6fa08f8284cb6707fddb38e982b6cb120d3f020c47b489667f08755b51bc5
7
- data.tar.gz: 8d9b376526c263a57d7bd8691ec847aeeca35a8b32e72098d763d965c8e3d707345b0c65495ccafc4d3d2b03e9048cb463629c0a4ec85b7a623c34d370bcffeb
6
+ metadata.gz: 41edbc48494258f1c55db913009e6cb722f2f70bbcba721afbc6d0a86b17f507b919dd58086aecbf2f6b8a78a00b474e12786c1bfca1aaa29cf62bcfcff1f844
7
+ data.tar.gz: 9a9b0280c67b828262442b27bebc28e70b77922facd2115e5dd19d5091c0959f705cb938cae5432d2888a1a4a3dbcb2ca37a0e653c0e902163c3266498e5abc2
data/.travis.yml CHANGED
@@ -7,4 +7,4 @@ rvm:
7
7
  - 2.4.5
8
8
  - 2.5.3
9
9
  - 2.6.0
10
- before_install: gem install bundler -v 1.17.2
10
+ before_install: gem install bundler -v 2.0.1
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 adrian@setyadi.pro. All
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/register"
7
- require_relative "smart_kv/errors"
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
- extend Register
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
- def method_missing(m, *args)
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
@@ -2,6 +2,4 @@ module DidYouMean
2
2
  SPELL_CHECKERS.merge!({
3
3
  'SmartKv::KeyError' => KeyErrorChecker
4
4
  })
5
-
6
- ::SmartKv::KeyError.prepend DidYouMean::Correctable
7
5
  end
@@ -1,7 +1,7 @@
1
1
  class SmartKv
2
2
  InitializationError = Class.new(StandardError)
3
3
 
4
- class KeyError < ::StandardError
4
+ class KeyError < ::KeyError
5
5
  attr_reader :key, :receiver
6
6
 
7
7
  def initialize(message, key: nil, receiver: {})
@@ -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
@@ -1,3 +1,3 @@
1
1
  class SmartKv
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.6"
3
3
  end
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", "~> 1.17"
27
- spec.add_development_dependency "rake", "~> 10.0"
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.3
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-02-24 00:00:00.000000000 Z
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: '1.17'
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: '1.17'
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: '10.0'
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: '10.0'
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: '0'
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: '0'
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/register.rb
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
@@ -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