smart_config 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 025b4afefa2ea50c75c77001460628df27241849a6ccb090dd948a4926db80ce
4
- data.tar.gz: 1ca3075c54be54c5a4c11eb0da918c2e7fe1947691bdc9ffc79f55dc70798fa2
3
+ metadata.gz: 5baec6d3e93cb78128a09ddf0448b50b291c72d41002f161f0adbfcda8f0471d
4
+ data.tar.gz: 98b529930e526149c73a85080acb0b2b008db88d02e8a75f9720db195721e835
5
5
  SHA512:
6
- metadata.gz: 152cd53fb92b3e3912acff22748bc17eea6d64523c9759aa6f31dc84e3c16f715a55fd506f9f07e85e412ff9e6091c0b01d06b2411806ef6524cb98dfaffb4da
7
- data.tar.gz: da08fd3f31fe6c0b1829ef08642f788c72f0726cc1a9b82c0fae06b96579f03c2dc119292d2780ae74338c9f8fb75b5862ecd69769f96964ea7f6c28899eef56
6
+ metadata.gz: 83b4657ecdfe488d740ab0494f391cbc8c9436339f0c794790da0e7f376c0a1383d80f9a20768977ca3a70eaa7edf2b3089a3605b148f2a28e5af0c6e14d7554
7
+ data.tar.gz: 321702e528aa873f3bb7953c72b8b805b75b497dfac2b7aa7060fb914c48f35f528367e5b4b538946db91bd61dd8cda5d5dd467bab5f28e8fb2f5f28afdf6607
data/README.md CHANGED
@@ -80,7 +80,37 @@ value :hostname, default: 'localhost'
80
80
 
81
81
  All available options are:
82
82
 
83
- | name | description |
84
- |-----------|----------------------------------------------------------------------------------------------------------------------------------------------------|
85
- | default | Sets a default value for the field, if no configuration could be found. If this option is not set, getting an unset field will raise an exception. |
86
- | format | Sets the format of the field. If this option is not set, the field will be formatted as string. |
83
+ | name | description |
84
+ |-------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
85
+ | default | Sets a default value for the field, if no configuration could be found. If this option is not set, getting an unset field will raise an exception. |
86
+ | format | Sets the format of the field. If this option is not set, the field will be formatted as string. |
87
+ | description | A free-form text description of the field. Optional. Used for documentation generation. |
88
+
89
+ The `description` option is also accepted on `group`:
90
+
91
+ ```ruby
92
+ group :smtp, description: 'SMTP server settings' do
93
+ value :hostname, description: 'The SMTP server hostname'
94
+ value :port, format: :integer, description: 'The SMTP server port'
95
+ end
96
+ ```
97
+
98
+ #### Generating Documentation
99
+
100
+ `SmartConfig::Markdown` renders a Markdown table documenting all values in a config module, including their descriptions, defaults, and formats:
101
+
102
+ ```ruby
103
+ puts SmartConfig::Markdown.new(Config).render
104
+ ```
105
+
106
+ Output:
107
+
108
+ ```
109
+ | Key | Description | Default | Format |
110
+ |----------------|--------------------------|---------|---------|
111
+ | app_name | | My App | string |
112
+ | smtp.hostname | The SMTP server hostname | | string |
113
+ | smtp.port | The SMTP server port | | integer |
114
+ ```
115
+
116
+ Values from nested groups are listed with their full dotted key path. Groups themselves are not rendered as rows.
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartConfig
4
+ #
5
+ # Generates a markdown table documenting all config values in a SmartConfig module.
6
+ #
7
+ class Markdown
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def render
13
+ rows = collect_entries(@config, [])
14
+ return '' if rows.empty?
15
+
16
+ lines = []
17
+ lines << '| Key | Description | Default | Format |'
18
+ lines << '|-----|-------------|---------|--------|'
19
+ rows.each do |row|
20
+ cells = [row[:path], row[:description], row[:default], row[:format]]
21
+ lines << "| #{cells.map { |c| escape(c) }.join(' | ')} |"
22
+ end
23
+ lines.join("\n")
24
+ end
25
+
26
+ private
27
+
28
+ def escape(value)
29
+ value.to_s.gsub('|', '\|')
30
+ end
31
+
32
+ def collect_entries(config, prefix)
33
+ raw = config.instance_variable_get(:@config) || {}
34
+ raw.flat_map do |key, entry|
35
+ next_prefix = prefix + [key.to_s]
36
+ if entry.key?(:group)
37
+ collect_entries(entry[:group], next_prefix)
38
+ else
39
+ [entry_for(entry, next_prefix)]
40
+ end
41
+ end
42
+ end
43
+
44
+ def entry_for(entry, prefix)
45
+ {
46
+ path: prefix.join('.'),
47
+ description: entry.fetch(:description, ''),
48
+ default: entry.key?(:default) ? entry[:default].to_s : '',
49
+ format: entry[:format].to_s
50
+ }
51
+ end
52
+ end
53
+ end
@@ -13,15 +13,18 @@ module SmartConfig
13
13
  @config ||= {}
14
14
  key = name.to_sym
15
15
  config = opts.reduce({}, :merge)
16
- config[:formatter] = SmartConfig::Formatters.find(config.fetch(:format, :string))
16
+ config[:format] = config.fetch(:format, :string)
17
+ config[:formatter] = SmartConfig::Formatters.find(config[:format])
17
18
  @config[key] = config
18
19
  define_singleton_method(name) { format_value(key, get_value(key)) }
19
20
  end
20
21
 
21
- def group(name, &)
22
+ def group(name, *opts, &)
22
23
  @config ||= {}
23
24
  key = name.to_sym
25
+ config = opts.reduce({}, :merge)
24
26
  @config[key] ||= {}
27
+ @config[key].merge!(config)
25
28
  @config[key][:group] = SmartConfig::Group.new([namespace, name].compact.flatten, method(:walker), &)
26
29
  define_singleton_method(name) { get_value(key) }
27
30
  end
@@ -30,6 +33,10 @@ module SmartConfig
30
33
  (@config || {}).keys
31
34
  end
32
35
 
36
+ def description_for(name)
37
+ (@config || {})[name.to_sym]&.fetch(:description, '') || ''
38
+ end
39
+
33
40
  def get_value(name)
34
41
  return @config[name][:group] if @config[name].key?(:group)
35
42
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmartConfig
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/smart_config.rb CHANGED
@@ -12,4 +12,5 @@ require 'smart_config/walker'
12
12
  require 'smart_config/values'
13
13
  require 'smart_config/group'
14
14
  require 'smart_config/config'
15
+ require 'smart_config/markdown'
15
16
  require 'smart_config/version'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien MATHIEU
@@ -27,6 +27,7 @@ files:
27
27
  - lib/smart_config/formatters/integer.rb
28
28
  - lib/smart_config/formatters/string.rb
29
29
  - lib/smart_config/group.rb
30
+ - lib/smart_config/markdown.rb
30
31
  - lib/smart_config/values.rb
31
32
  - lib/smart_config/version.rb
32
33
  - lib/smart_config/walker.rb
@@ -49,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
50
  - !ruby/object:Gem::Version
50
51
  version: '0'
51
52
  requirements: []
52
- rubygems_version: 4.0.6
53
+ rubygems_version: 4.0.16
53
54
  specification_version: 4
54
55
  summary: A DLS for reading and accessing static configuration
55
56
  test_files: []