konfig-config 1.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/konfig/exporters/abstract.rb +22 -0
- data/lib/konfig/exporters/env_vars_as_markdown.rb +40 -0
- data/lib/konfig/schema_attribute.rb +35 -9
- data/lib/konfig/schema_attribute_dsl.rb +34 -0
- data/lib/konfig/schema_group.rb +3 -1
- data/lib/konfig/schema_group_dsl.rb +9 -2
- data/lib/konfig/sources/environment.rb +9 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 607bd53ea2e3e6a9e7cdf483dfe532f99459e8a322e59315296b9befe2dfcc12
|
4
|
+
data.tar.gz: 2874b799bf9ae8770abd7ad7989415f31ff807c92995e2470c736d39d6c9a4fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 011a5a161270a6a5481eef45785ebcf8ac12f7b280de8e0f0cc33c034cc3d1efd5370381efe54dfc352de147bfd9e91b6cffdd53f0511e9f161d2cd891abab78
|
7
|
+
data.tar.gz: 6bd02077bdd358bc0050cf32a3e7c552c8ac9433f01481a572d7737b75c4fb58d22791d03b275c53bdb703064d4f60e0fd37ffb59b8d9533ed9e54080de97264
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Konfig
|
4
|
+
module Exporters
|
5
|
+
class Abstract
|
6
|
+
|
7
|
+
def initialize(schema, **options)
|
8
|
+
@schema = schema
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def export
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :options
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'konfig/exporters/abstract'
|
4
|
+
require 'konfig/sources/environment'
|
5
|
+
|
6
|
+
module Konfig
|
7
|
+
module Exporters
|
8
|
+
# This export will create a markdown document containing all configuration variables
|
9
|
+
# as defined the schema shown as environment variables.
|
10
|
+
class EnvVarsAsMarkdown < Abstract
|
11
|
+
|
12
|
+
# rubocop:disable Metrics/AbcSize
|
13
|
+
# rubocop:disable Metrics/MethodLength
|
14
|
+
def export
|
15
|
+
contents = []
|
16
|
+
contents << "# #{options[:title] || 'Environment Variables'}"
|
17
|
+
contents << ''
|
18
|
+
contents << (options[:introduction] ||
|
19
|
+
'This document contains all the environment variables which are available for this application.')
|
20
|
+
contents << ''
|
21
|
+
contents << '| Name | Type | Description | Default |'
|
22
|
+
contents << '| ---- | ---- | ----------- | ------- |'
|
23
|
+
path = []
|
24
|
+
@schema.groups.each do |group_name, group|
|
25
|
+
path << group_name
|
26
|
+
group.attributes.each do |name, attr|
|
27
|
+
env_var = Sources::Environment.path_to_env_var(path + [name])
|
28
|
+
type = attr.array? ? "Array of #{attr.type}s" : attr.type.to_s.capitalize
|
29
|
+
contents << "| `#{env_var}` | #{type} | #{attr.description} | #{attr.default} |"
|
30
|
+
end
|
31
|
+
path.pop
|
32
|
+
end
|
33
|
+
contents.join("\n") + "\n"
|
34
|
+
end
|
35
|
+
# rubocop:enable Metrics/AbcSize
|
36
|
+
# rubocop:enable Metrics/MethodLength
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -7,25 +7,37 @@ module Konfig
|
|
7
7
|
|
8
8
|
attr_reader :name
|
9
9
|
attr_reader :type
|
10
|
-
|
11
|
-
|
10
|
+
attr_accessor :default
|
11
|
+
attr_accessor :description
|
12
|
+
attr_accessor :array
|
13
|
+
attr_accessor :transformer
|
12
14
|
|
13
|
-
def initialize(name, type: :string, array:
|
15
|
+
def initialize(name, type: :string, array: nil, default: nil, description: nil, transformer: nil)
|
14
16
|
@name = name
|
15
|
-
@
|
16
|
-
|
17
|
+
@array = array unless array.nil?
|
18
|
+
self.type = type
|
17
19
|
@default = default
|
20
|
+
@description = description
|
18
21
|
@transformer = transformer
|
22
|
+
end
|
23
|
+
|
24
|
+
def type=(type)
|
25
|
+
if type.is_a?(Array) && type.size == 1
|
26
|
+
@array = true
|
27
|
+
type = type.first
|
28
|
+
end
|
29
|
+
|
30
|
+
raise InvalidAttributeTypeError, "Invalid type #{type} for attribute #{@name}" unless TYPES.include?(type)
|
19
31
|
|
20
|
-
|
32
|
+
@type = type
|
21
33
|
end
|
22
34
|
|
23
35
|
def array?
|
24
36
|
@array == true
|
25
37
|
end
|
26
38
|
|
27
|
-
def cast(value)
|
28
|
-
return value
|
39
|
+
def cast(value, handle_arrays: true)
|
40
|
+
return cast_array(value) if handle_arrays && array?
|
29
41
|
|
30
42
|
return nil if value.nil?
|
31
43
|
return nil if value.is_a?(String) && value.empty?
|
@@ -37,7 +49,11 @@ module Konfig
|
|
37
49
|
casted = cast(value)
|
38
50
|
return casted if transformer.nil?
|
39
51
|
|
40
|
-
|
52
|
+
if array?
|
53
|
+
casted.map { |v| transformer.call(v) }
|
54
|
+
else
|
55
|
+
transformer.call(casted)
|
56
|
+
end
|
41
57
|
end
|
42
58
|
|
43
59
|
private
|
@@ -67,5 +83,15 @@ module Konfig
|
|
67
83
|
%w[true 1 1.0].include?(input.to_s)
|
68
84
|
end
|
69
85
|
|
86
|
+
def cast_array(value)
|
87
|
+
# If we have an array, and we're expecting an array, then we can just
|
88
|
+
# cast all the values of that array
|
89
|
+
return value.map { |v| cast(v, handle_arrays: false) } if value.is_a?(Array)
|
90
|
+
|
91
|
+
# if we want an array but we don't have one, we will take the value
|
92
|
+
# and put it in the array and try casting again
|
93
|
+
[cast(value, handle_arrays: false)]
|
94
|
+
end
|
95
|
+
|
70
96
|
end
|
71
97
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'konfig/schema_attribute'
|
4
|
+
|
5
|
+
module Konfig
|
6
|
+
class SchemaAttributeDSL
|
7
|
+
|
8
|
+
def initialize(attribute)
|
9
|
+
@attribute = attribute
|
10
|
+
end
|
11
|
+
|
12
|
+
def type(value)
|
13
|
+
@attribute.type = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def default(value)
|
17
|
+
@attribute.default = value
|
18
|
+
end
|
19
|
+
|
20
|
+
def description(value)
|
21
|
+
@attribute.description = value
|
22
|
+
end
|
23
|
+
alias desc description
|
24
|
+
|
25
|
+
def array
|
26
|
+
@attribute.array = true
|
27
|
+
end
|
28
|
+
|
29
|
+
def transform(value = nil, &block)
|
30
|
+
@attribute.transformer = block || value
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/konfig/schema_group.rb
CHANGED
@@ -6,6 +6,8 @@ require 'konfig/schema_attribute'
|
|
6
6
|
module Konfig
|
7
7
|
class SchemaGroup
|
8
8
|
|
9
|
+
attr_reader :attributes
|
10
|
+
|
9
11
|
def initialize
|
10
12
|
@attributes = {}
|
11
13
|
end
|
@@ -28,7 +30,7 @@ module Konfig
|
|
28
30
|
@attributes.each_with_object({}) do |(name, attribute), hash|
|
29
31
|
attribute_path = path + [name]
|
30
32
|
if source.nil?
|
31
|
-
hash[name] = attribute.
|
33
|
+
hash[name] = attribute.default
|
32
34
|
else
|
33
35
|
begin
|
34
36
|
source_value = source.get(attribute_path, attribute: attribute)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'konfig/schema_attribute'
|
4
|
-
|
4
|
+
require 'konfig/schema_attribute_dsl'
|
5
5
|
module Konfig
|
6
6
|
class SchemaGroupDSL
|
7
7
|
|
@@ -11,7 +11,14 @@ module Konfig
|
|
11
11
|
|
12
12
|
SchemaAttribute::TYPES.each do |type|
|
13
13
|
define_method type do |name, **kwargs, &block|
|
14
|
-
@group.add_attribute(name, type: type, **kwargs
|
14
|
+
attribute = @group.add_attribute(name, type: type, **kwargs)
|
15
|
+
|
16
|
+
if block
|
17
|
+
dsl = SchemaAttributeDSL.new(attribute)
|
18
|
+
dsl.instance_eval(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
attribute
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
@@ -14,7 +14,7 @@ module Konfig
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def get(path, attribute: nil)
|
17
|
-
key =
|
17
|
+
key = self.class.path_to_env_var(path)
|
18
18
|
raise ValueNotPresentError unless @env.key?(key)
|
19
19
|
|
20
20
|
value = @env[key]
|
@@ -29,6 +29,14 @@ module Konfig
|
|
29
29
|
value.split(@array_separator).map(&:strip)
|
30
30
|
end
|
31
31
|
|
32
|
+
class << self
|
33
|
+
|
34
|
+
def path_to_env_var(path)
|
35
|
+
path.map { |p| p.to_s.upcase }.join('_')
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
32
40
|
end
|
33
41
|
end
|
34
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: konfig-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cooke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -37,8 +37,11 @@ files:
|
|
37
37
|
- lib/konfig/config_hash.rb
|
38
38
|
- lib/konfig/deep_merge.rb
|
39
39
|
- lib/konfig/error.rb
|
40
|
+
- lib/konfig/exporters/abstract.rb
|
41
|
+
- lib/konfig/exporters/env_vars_as_markdown.rb
|
40
42
|
- lib/konfig/schema.rb
|
41
43
|
- lib/konfig/schema_attribute.rb
|
44
|
+
- lib/konfig/schema_attribute_dsl.rb
|
42
45
|
- lib/konfig/schema_dsl.rb
|
43
46
|
- lib/konfig/schema_group.rb
|
44
47
|
- lib/konfig/schema_group_dsl.rb
|