mimi-config 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/Gemfile +1 -1
- data/lib/mimi/config/manifest.rb +86 -0
- data/lib/mimi/config/version.rb +1 -1
- data/lib/mimi/config.rb +33 -39
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cca2e6a1670a7c4f5721347e52617840139f84c3
|
4
|
+
data.tar.gz: 744fb11b98ed3ddbb7524a73a0a58b8156b0ddb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c7b0f8d48b89bfa92f24aea94ab620a692d8b67bc170bc89f1f75f0b2264cf2cdcfd82d06732681cff6c9f4527373f1be51448de19164d5eb9977848bca2a97
|
7
|
+
data.tar.gz: 4181ee303e70a2751fe026d89b639afc28ba6384e795f9f0f6c231dc855f4152b1ff63ea9cc023823bc855f51679fcabcfae35db82e4d29605acb229038fa5e9
|
data/Gemfile
CHANGED
@@ -0,0 +1,86 @@
|
|
1
|
+
module Mimi
|
2
|
+
class Config
|
3
|
+
class Manifest < Hash
|
4
|
+
# Reads manifest file and merges it with the current manifest.
|
5
|
+
#
|
6
|
+
# @return [Mimi::Config::Manifest] current manifest merged with the new one
|
7
|
+
#
|
8
|
+
def load(filename_or_hash, _opts = {})
|
9
|
+
new_manifest = nil
|
10
|
+
if filename_or_hash.is_a?(String) || filename_or_hash.is_a?(Pathname)
|
11
|
+
new_manifest = YAML.load(File.read(filename_or_hash))
|
12
|
+
end
|
13
|
+
new_manifest = filename_or_hash.dup if filename_or_hash.is_a?(Hash)
|
14
|
+
return self unless new_manifest
|
15
|
+
raise 'Invalid manifest file format' unless new_manifest.is_a?(Hash)
|
16
|
+
new_manifest.each do |k, v|
|
17
|
+
merge_key(k, v)
|
18
|
+
end
|
19
|
+
self
|
20
|
+
rescue StandardError => e
|
21
|
+
raise "Failed to load manifest file: #{e}"
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns YAML representation of the manifest
|
25
|
+
#
|
26
|
+
# @return [String]
|
27
|
+
#
|
28
|
+
def to_yaml
|
29
|
+
out = []
|
30
|
+
self.each do |k, v|
|
31
|
+
if v.key?(:const)
|
32
|
+
out << "#{k}:"
|
33
|
+
out << " desc: #{value_to_yaml(v[:desc])}" if v.key?(:desc)
|
34
|
+
if v[:const]
|
35
|
+
out << " const: #{value_to_yaml(v[:default])}"
|
36
|
+
elsif v.key?(:default)
|
37
|
+
out << " default: #{value_to_yaml(v[:default])}"
|
38
|
+
end
|
39
|
+
elsif v.key?(:desc)
|
40
|
+
out << "#{k}: #{value_to_yaml(v[:desc])}"
|
41
|
+
else
|
42
|
+
out << "#{k}:"
|
43
|
+
end
|
44
|
+
out << ''
|
45
|
+
end
|
46
|
+
out.join("\n")
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def value_to_yaml(v)
|
52
|
+
v.nil? ? '# nil' : v.inspect
|
53
|
+
end
|
54
|
+
|
55
|
+
def merge_key(k, v)
|
56
|
+
k = k.to_sym
|
57
|
+
self[k] ||= {}
|
58
|
+
if v.nil?
|
59
|
+
# var:
|
60
|
+
elsif v.is_a?(String)
|
61
|
+
# var: A description
|
62
|
+
self[k][:desc] = v
|
63
|
+
elsif v.is_a?(Hash)
|
64
|
+
merge_key_hash(k, v)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def merge_key_hash(k, v)
|
69
|
+
v = v.dup.stringify_keys
|
70
|
+
self[k][:desc] = v['desc'] if v.key?('desc')
|
71
|
+
|
72
|
+
if v.key?('const') && v.key?('default')
|
73
|
+
raise "Invalid mix of 'const' and 'default' in parameter definition '#{k}'"
|
74
|
+
end
|
75
|
+
|
76
|
+
if v.key?('default')
|
77
|
+
self[k][:const] = false
|
78
|
+
self[k][:default] = v['default']
|
79
|
+
elsif v.key?('const')
|
80
|
+
self[k][:const] = true
|
81
|
+
self[k][:default] = v['const']
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end # class Manifest
|
85
|
+
end # class Config
|
86
|
+
end # module Mimi
|
data/lib/mimi/config/version.rb
CHANGED
data/lib/mimi/config.rb
CHANGED
@@ -4,6 +4,13 @@ require 'dotenv'
|
|
4
4
|
require 'mimi/core'
|
5
5
|
|
6
6
|
module Mimi
|
7
|
+
# Returns all loaded modules combined configuration manifest
|
8
|
+
#
|
9
|
+
def self.loaded_modules_manifest
|
10
|
+
loaded_modules.reduce(Mimi::Config::Manifest.new) { |a, e| a.load(e.module_manifest) }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
7
14
|
#
|
8
15
|
# Config stores the manifest and reads and stores configurable parameters from ENV.
|
9
16
|
#
|
@@ -30,7 +37,7 @@ module Mimi
|
|
30
37
|
# @param manifest_filename [String,nil] path to the manifest.yml or nil to skip loading manifest
|
31
38
|
#
|
32
39
|
def initialize(manifest_filename = nil, opts = {})
|
33
|
-
@manifest =
|
40
|
+
@manifest = Mimi::Config::Manifest.new
|
34
41
|
@params = {}
|
35
42
|
load(manifest_filename, opts) if manifest_filename
|
36
43
|
end
|
@@ -70,6 +77,12 @@ module Mimi
|
|
70
77
|
end
|
71
78
|
end
|
72
79
|
|
80
|
+
# Returns raw manifest
|
81
|
+
#
|
82
|
+
def manifest_raw
|
83
|
+
@manifest
|
84
|
+
end
|
85
|
+
|
73
86
|
# Returns true if the config manifest includes the parameter with the given name.
|
74
87
|
#
|
75
88
|
# If manifest includes the parameter name, it is safe to access paramter
|
@@ -107,20 +120,29 @@ module Mimi
|
|
107
120
|
include?(name) || super
|
108
121
|
end
|
109
122
|
|
123
|
+
# Returns Hash representation of the config.
|
124
|
+
# All Hash keys are Symbol
|
125
|
+
#
|
126
|
+
# @return [Hash]
|
127
|
+
#
|
128
|
+
def to_h
|
129
|
+
@manifest.keys.map do |k|
|
130
|
+
[k, self[k]]
|
131
|
+
end.to_h
|
132
|
+
end
|
133
|
+
|
134
|
+
# Returns to_h.to_s
|
135
|
+
#
|
136
|
+
def to_s
|
137
|
+
to_h.to_s
|
138
|
+
end
|
139
|
+
|
110
140
|
private
|
111
141
|
|
112
142
|
# Reads manifest file and merges it with the current manifest.
|
113
143
|
#
|
114
144
|
def load_manifest(filename, _opts = {})
|
115
|
-
|
116
|
-
return manifest unless new_manifest
|
117
|
-
raise 'Invalid manifest file format' unless new_manifest.is_a?(Hash)
|
118
|
-
new_manifest.each do |k, v|
|
119
|
-
merge_manifest_key(k, v)
|
120
|
-
end
|
121
|
-
manifest
|
122
|
-
rescue StandardError => e
|
123
|
-
raise "Failed to load manifest file: #{e}"
|
145
|
+
@manifest.load(filename)
|
124
146
|
end
|
125
147
|
|
126
148
|
# Reads parameters from the ENV according to the current manifest
|
@@ -142,36 +164,8 @@ module Mimi
|
|
142
164
|
end
|
143
165
|
@params
|
144
166
|
end
|
145
|
-
|
146
|
-
def merge_manifest_key(k, v)
|
147
|
-
k = k.to_sym
|
148
|
-
@manifest[k] ||= {}
|
149
|
-
if v.nil?
|
150
|
-
# var:
|
151
|
-
elsif v.is_a?(String)
|
152
|
-
# var: A description
|
153
|
-
@manifest[k][:desc] = v
|
154
|
-
elsif v.is_a?(Hash)
|
155
|
-
merge_manifest_key_hash(k, v)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
def merge_manifest_key_hash(k, v)
|
160
|
-
@manifest[k][:desc] = v['desc'] if v.key?('desc')
|
161
|
-
|
162
|
-
if v.key?('const') && v.key?('default')
|
163
|
-
raise "Invalid mix of 'const' and 'default' in parameter definition '#{k}'"
|
164
|
-
end
|
165
|
-
|
166
|
-
if v.key?('default')
|
167
|
-
@manifest[k][:const] = false
|
168
|
-
@manifest[k][:default] = v['default']
|
169
|
-
elsif v.key?('const')
|
170
|
-
@manifest[k][:const] = true
|
171
|
-
@manifest[k][:default] = v['const']
|
172
|
-
end
|
173
|
-
end
|
174
167
|
end # class Config
|
175
168
|
end # module Mimi
|
176
169
|
|
177
170
|
require_relative 'config/version'
|
171
|
+
require_relative 'config/manifest'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mimi-config
|
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
|
- Alex Kukushkin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mimi-core
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- bin/console
|
113
113
|
- bin/setup
|
114
114
|
- lib/mimi/config.rb
|
115
|
+
- lib/mimi/config/manifest.rb
|
115
116
|
- lib/mimi/config/version.rb
|
116
117
|
- mimi-config.gemspec
|
117
118
|
homepage: https://github.com/kukushkin/mimi-config
|