loadable_config 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/loadable_config/version.rb +1 -1
- data/lib/loadable_config.rb +15 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bffe07293caf6b245b39f3bbc70f1479637da1ac7c256e4c0617d1df04e04b11
|
4
|
+
data.tar.gz: '087964aedac5fa2f165c71de56b9bd2091f0176470a28a5362448bca10affaf2'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69e8768f08257f568b6f1462f60ea50dee8d3c2849b3ea59aab6ab9033a7f2695e9559d0e5804444a59c9a9a1fa7e9595c5802e56ce6f481105ac9cec1bbe1e2
|
7
|
+
data.tar.gz: 8e64feae5ff2bdf907a7890c7f493ac2f540acf7e7e73384877fbca571c7a9aa8417b1258650c71a6f73cbd956056c56f1d59731315eed3dc1267f5f427079b2
|
data/lib/loadable_config.rb
CHANGED
@@ -7,12 +7,13 @@ require 'yaml'
|
|
7
7
|
require 'singleton'
|
8
8
|
|
9
9
|
class LoadableConfig
|
10
|
-
Attribute = Struct.new(:name, :type, :schema, :optional, :serializer)
|
10
|
+
Attribute = Struct.new(:name, :type, :schema, :optional, :serializer, :default)
|
11
11
|
|
12
12
|
class << self
|
13
13
|
attr_reader :_attributes, :_config_file
|
14
14
|
|
15
15
|
def inherited(subclass)
|
16
|
+
super
|
16
17
|
subclass.send(:include, Singleton)
|
17
18
|
end
|
18
19
|
|
@@ -20,7 +21,7 @@ class LoadableConfig
|
|
20
21
|
@_config_file = path
|
21
22
|
end
|
22
23
|
|
23
|
-
def attribute(attr, type: :string, schema: {}, optional: false, serializer: nil)
|
24
|
+
def attribute(attr, type: :string, schema: {}, optional: false, serializer: nil, default: nil)
|
24
25
|
@_attributes ||= []
|
25
26
|
attr = attr.to_sym
|
26
27
|
if ATTRIBUTE_BLACKLIST.include?(attr)
|
@@ -28,11 +29,12 @@ class LoadableConfig
|
|
28
29
|
'attributes must not collide with class methods of LoadableConfig')
|
29
30
|
end
|
30
31
|
|
31
|
-
type = [type] unless type.
|
32
|
+
type = [type] unless type.is_a?(Array)
|
32
33
|
type.map! { |t| t.to_s }
|
33
34
|
|
34
|
-
_attributes << Attribute.new(attr.to_s, type, schema, optional, serializer)
|
35
|
+
_attributes << Attribute.new(attr.to_s, type, schema, optional, serializer, default)
|
35
36
|
attr_accessor attr
|
37
|
+
|
36
38
|
define_singleton_method(attr) { instance.send(attr) }
|
37
39
|
end
|
38
40
|
|
@@ -50,6 +52,7 @@ class LoadableConfig
|
|
50
52
|
if @@_configuration.frozen?
|
51
53
|
raise ArgumentError.new('Cannot configure LoadableConfig: already configured')
|
52
54
|
end
|
55
|
+
|
53
56
|
yield(@@_configuration)
|
54
57
|
@@_configuration.freeze
|
55
58
|
end
|
@@ -111,10 +114,15 @@ class LoadableConfig
|
|
111
114
|
end
|
112
115
|
|
113
116
|
self.class._attributes.each do |attr|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
+
if config.has_key?(attr.name)
|
118
|
+
value = config[attr.name]
|
119
|
+
if attr.serializer
|
120
|
+
value = attr.serializer.load(value)
|
121
|
+
end
|
122
|
+
else
|
123
|
+
value = attr.default
|
117
124
|
end
|
125
|
+
|
118
126
|
self.public_send(:"#{attr.name}=", value)
|
119
127
|
end
|
120
128
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loadable_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- iKnow Team
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json_schema
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
- !ruby/object:Gem::Version
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
|
-
rubygems_version: 3.0.3
|
109
|
+
rubygems_version: 3.0.3.1
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Simple declarative configuration files
|