smart_kv 0.1.0 → 0.1.1

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: 4208fdf9d213d96387a105205361aef760652dce3da67f1e282d5a9eb6132d30
4
- data.tar.gz: 0c84eb3e3bad64d9e934b3cbc996092f24960be346770c5da690745b1555ab06
3
+ metadata.gz: 59cbc75d757bbaef98c0cae7421817974595775723a7510d98eb6946eb120174
4
+ data.tar.gz: 1e7bcd734e68334b130854c56a07c6c342217d56b2ab04f19c67e3ced4200c98
5
5
  SHA512:
6
- metadata.gz: 8621c06a03951e97723466f53e2c293ccb096c3bf504ad58a0cb16e6154c38220e9c87ee97a2d2912ba7299da517aa5192e21097cd5316d3b599bb6474e999a4
7
- data.tar.gz: 6befe669b9f541dd998a64a8db3b0c3a9f501fff11268417fcbea09f8fa73813496af1cf11bb0aa3f646e8a47b52f783487a00dac18839ae4c0d052a1521206f
6
+ metadata.gz: d398b42576e2649f7a9ea61bac210e236ecb2f7acaec0cdd1f0ff4b3b0b2a972aad6af3615d5b22655b8d6eeca31451aea325b59d2a0468b14b29208d3a57de9
7
+ data.tar.gz: b94d548a1ee5839495826c0c7e4c12c53a9dddb55b0aa679ccf6738ba0c45ff9843d4f1dda5e330a494d6036076d606a2279cab8194c5f6980c3accfb03cb626
data/.travis.yml CHANGED
@@ -6,7 +6,5 @@ rvm:
6
6
  - 2.3.8
7
7
  - 2.4.5
8
8
  - 2.5.3
9
- - jruby-9.2.5.0
10
9
  - rbx-3.107
11
- - truffleruby-1.0.0-rc10
12
10
  before_install: gem install bundler -v 1.17.2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smart_kv (0.1.0)
4
+ smart_kv (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # SmartKv
2
2
 
3
+ [![Build Status](https://travis-ci.org/styd/smart_kv.svg?branch=master)](https://travis-ci.org/styd/smart_kv)
4
+
3
5
  Best practice of writing configurations by strictly allowing and requiring keys.
4
6
 
5
7
  It doesn't have to be a configuration.
@@ -55,6 +57,20 @@ c2 = Config.new(OpenStruct.new({some_key: "val", second_key: "val 2"}))
55
57
  c2.second_key
56
58
  ```
57
59
 
60
+ ### Override callable object
61
+
62
+ You can change the callable object to any class that accepts hash as input of its new class method.
63
+
64
+ ```ruby
65
+ class Convertable < SmartKv
66
+ required :abcd
67
+ callable_as OpenStruct
68
+ end
69
+
70
+ c = Convertable.new({abcd: 123})
71
+ c.abcd #=> 123
72
+ ```
73
+
58
74
  ### Not using it for config
59
75
 
60
76
  You can choose not to use it for config. Maybe for strict request body keys?
@@ -71,7 +87,7 @@ request.set_form_data(PostBody.new({app_key: "abc", secret_key: "def"}))
71
87
  ## Coming Soon
72
88
 
73
89
  - [ ] Make it serializable
74
- - [ ] Convertable from hash (as input) to OpenStruct (the resulting object) or another object and vice versa
90
+ - [X] Convertable from hash (as input) to OpenStruct (the resulting object) or another object and vice versa
75
91
  - [ ] Accept config file (e.g. `json`, `yaml`, etc.) or file path as input
76
92
  - [ ] Support nested/deep key value object as input
77
93
  - [ ] Make some nested keys from the same parent key required and some others optional
@@ -0,0 +1,9 @@
1
+ module SmartKv::Macro
2
+ def callable_as(klass)
3
+ @callable_as = superclass == SmartKv ? klass : superclass.callable_class
4
+ end
5
+
6
+ def callable_class
7
+ @callable_as
8
+ end
9
+ end
@@ -1,6 +1,6 @@
1
1
  module SmartKv::Register
2
2
  def required(*args)
3
- @required ||= self.superclass == SmartKv ? Set.new : superclass.required_keys.dup
3
+ @required ||= superclass == SmartKv ? Set.new : superclass.required_keys.dup
4
4
  @required += args
5
5
  end
6
6
 
@@ -9,7 +9,7 @@ module SmartKv::Register
9
9
  end
10
10
 
11
11
  def optional(*args)
12
- @optional ||= self.superclass == SmartKv ? Set.new : superclass.optional_keys.dup
12
+ @optional ||= superclass == SmartKv ? Set.new : superclass.optional_keys.dup
13
13
  @optional += args
14
14
  @required -= @optional
15
15
  @optional
@@ -20,6 +20,6 @@ module SmartKv::Register
20
20
  end
21
21
 
22
22
  def new(*args)
23
- super(@required.to_a, @optional.to_a, *args)
23
+ super(@required.to_a, @optional.to_a, @callable_as, *args)
24
24
  end
25
25
  end
@@ -1,3 +1,3 @@
1
1
  class SmartKv
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/smart_kv.rb CHANGED
@@ -1,15 +1,20 @@
1
1
  require "smart_kv/version"
2
2
  require "smart_kv/register"
3
+ require "smart_kv/macro"
3
4
 
4
5
  SmartKvInitializationError = Class.new(StandardError)
5
6
 
6
7
  class SmartKv
7
8
  extend Register
9
+ extend Macro
8
10
 
9
- def initialize(required_keys = [], optional_keys = [], kv = {})
11
+ attr_reader :object_class
12
+
13
+ def initialize(required_keys = [], optional_keys = [], object_class = nil, kv = {})
10
14
  prevent_direct_instantiation
11
15
 
12
- @kv = kv
16
+ @object_class = object_class || kv.class
17
+ @kv = kv.dup
13
18
  hash = kv.to_h.dup
14
19
 
15
20
  missing_keys = required_keys - hash.keys
@@ -24,7 +29,8 @@ class SmartKv
24
29
  end
25
30
 
26
31
  def method_missing(m, *args)
27
- @kv.send(m, *args)
32
+ @object ||= @object_class <= Hash ? @kv : @object_class.new(@kv)
33
+ @object.send(m, *args)
28
34
  end
29
35
 
30
36
  protected
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.1.0
4
+ version: 0.1.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: 2018-12-16 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,7 @@ files:
86
86
  - bin/console
87
87
  - bin/setup
88
88
  - lib/smart_kv.rb
89
+ - lib/smart_kv/macro.rb
89
90
  - lib/smart_kv/register.rb
90
91
  - lib/smart_kv/version.rb
91
92
  - smart_kv.gemspec