konfig-config 1.0.0 → 2.0.0

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: 75a53b2df0604086dbbec0e28d797572149247f999d16875948c0ceec940ccb5
4
- data.tar.gz: 5dcc54bb9a313c53c26ce7b3070ef69e3a9ea9a805aa9a3513d5406be7297c64
3
+ metadata.gz: 1ad4223735db5a3060899308630e5381f8366e6540e5cd377fa9482d0ba72029
4
+ data.tar.gz: 52f936cf81e41df19f076e0807c7a13f20bdc4b385efebee49645bcfef0869bf
5
5
  SHA512:
6
- metadata.gz: 6536edc14535469c53978a368ddc3fb8e73dbf8058c7c79cb4737894e73645d30275e86551a16fcfc5374acc8eb40e94c1c0af64ab5c45ab8775d341c8971273
7
- data.tar.gz: c0f45ef59ea643d7d1a1d1342d6bda7ff88edb4e50530e53a009eb46efdbf6f3d64c42ece69d98cf959150fc74f0abedf6422d8f249815e5ff8370c5635cbec8
6
+ metadata.gz: 86cb227104473211276c12e8e82f186260058d4eccb9c8ecf109417735ccb6018370667a7a1a9cf6143e4936318ea9321680c7b7582f861275c56376e438dd94
7
+ data.tar.gz: 59cebceb30b34f4d9cd4d9ad5696a4da5559b6535619913f38f33d2ff8e9c10eb356ce8bd40b6c479dfc5d00e3955b631e60374bce0319f77c4b4eca7e416267
@@ -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
- attr_reader :default
11
- attr_reader :transformer
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: false, default: nil, transformer: nil)
15
+ def initialize(name, type: :string, array: nil, default: nil, description: nil, transformer: nil)
14
16
  @name = name
15
- @type = type
16
- @array = array
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
- raise InvalidAttributeTypeError, "Invalid type #{type} for attribute #{name}" unless TYPES.include?(type)
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.map { |v| cast(v) } if value.is_a?(Array)
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
- transformer.call(casted)
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
@@ -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
@@ -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, &block)
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 = path.map { |p| p.to_s.upcase }.join('_')
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.0
4
+ version: 2.0.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-03-16 00:00:00.000000000 Z
11
+ date: 2023-03-20 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