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 +4 -4
- data/.travis.yml +0 -2
- data/Gemfile.lock +1 -1
- data/README.md +17 -1
- data/lib/smart_kv/macro.rb +9 -0
- data/lib/smart_kv/register.rb +3 -3
- data/lib/smart_kv/version.rb +1 -1
- data/lib/smart_kv.rb +9 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59cbc75d757bbaef98c0cae7421817974595775723a7510d98eb6946eb120174
|
4
|
+
data.tar.gz: 1e7bcd734e68334b130854c56a07c6c342217d56b2ab04f19c67e3ced4200c98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d398b42576e2649f7a9ea61bac210e236ecb2f7acaec0cdd1f0ff4b3b0b2a972aad6af3615d5b22655b8d6eeca31451aea325b59d2a0468b14b29208d3a57de9
|
7
|
+
data.tar.gz: b94d548a1ee5839495826c0c7e4c12c53a9dddb55b0aa679ccf6738ba0c45ff9843d4f1dda5e330a494d6036076d606a2279cab8194c5f6980c3accfb03cb626
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
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
|
-
- [
|
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
|
data/lib/smart_kv/register.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module SmartKv::Register
|
2
2
|
def required(*args)
|
3
|
-
@required ||=
|
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 ||=
|
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
|
data/lib/smart_kv/version.rb
CHANGED
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
|
-
|
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
|
-
@
|
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.
|
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.
|
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-
|
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
|