smart_config 0.1.0 → 0.3.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: 6c77b3cfa7c43568ea7791ab092681b4849013a52736c6a86b58520dfc65ebbd
4
+ data.tar.gz: 05fa38afe44718cf69416dbc3dc54d188c5089e82f9ebfa7bdfca38171dc14a6
5
5
  SHA512:
6
- metadata.gz: 152cd53fb92b3e3912acff22748bc17eea6d64523c9759aa6f31dc84e3c16f715a55fd506f9f07e85e412ff9e6091c0b01d06b2411806ef6524cb98dfaffb4da
7
- data.tar.gz: da08fd3f31fe6c0b1829ef08642f788c72f0726cc1a9b82c0fae06b96579f03c2dc119292d2780ae74338c9f8fb75b5862ecd69769f96964ea7f6c28899eef56
6
+ metadata.gz: 80126e552dd05d1837b65d5cedecef13dddb21239222e913a256719264fcf9495019d78b617d76ca994598af5ff94d14f8ed017ffa0cd60bbb34d40747cf46ab
7
+ data.tar.gz: 63d1884eac87f5f04f898fb0fc317fda3bd95423390a0604587c91ee63208de275b027061ececdad3be79994ede65456b7c87dba4206a671613e1cc6b4f9662e
data/README.md CHANGED
@@ -80,7 +80,40 @@ 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
+ | example | A free-form example value for the field. Optional. Used for documentation generation. |
89
+ | no_docs | When set to `true`, excludes the field from documentation generation. |
90
+
91
+ The `description`, `example`, and `no_docs` options are also accepted on `group`. When `no_docs: true` is set on a group, the entire group and all its nested values are excluded from documentation.
92
+
93
+ ```ruby
94
+ group :smtp, description: 'SMTP server settings' do
95
+ value :hostname, description: 'The SMTP server hostname', example: 'smtp.example.com'
96
+ value :port, format: :integer, description: 'The SMTP server port', example: '587'
97
+ value :password, no_docs: true
98
+ end
99
+ ```
100
+
101
+ #### Generating Documentation
102
+
103
+ `SmartConfig::Markdown` renders a Markdown table documenting all values in a config module, including their descriptions, examples, defaults, and formats:
104
+
105
+ ```ruby
106
+ puts SmartConfig::Markdown.new(Config).render
107
+ ```
108
+
109
+ Output:
110
+
111
+ ```
112
+ | Key | Description | Example | Default | Format |
113
+ |----------------|--------------------------|------------------|---------|---------|
114
+ | app_name | | | My App | string |
115
+ | smtp.hostname | The SMTP server hostname | smtp.example.com | | string |
116
+ | smtp.port | The SMTP server port | 587 | | integer |
117
+ ```
118
+
119
+ Values from nested groups are listed with their full dotted key path. Groups themselves are not rendered as rows. Values and groups with `no_docs: true` are omitted entirely.
@@ -0,0 +1,56 @@
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 | Example | Default | Format |'
18
+ lines << '|-----|-------------|---------|---------|--------|'
19
+ rows.each do |row|
20
+ cells = [row[:path], row[:description], row[:example], 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 [] if entry[:no_docs]
36
+
37
+ next_prefix = prefix + [key.to_s]
38
+ if entry.key?(:group)
39
+ collect_entries(entry[:group], next_prefix)
40
+ else
41
+ [entry_for(entry, next_prefix)]
42
+ end
43
+ end
44
+ end
45
+
46
+ def entry_for(entry, prefix)
47
+ {
48
+ path: prefix.join('.'),
49
+ description: entry.fetch(:description, ''),
50
+ example: entry.fetch(:example, ''),
51
+ default: entry.key?(:default) ? entry[:default].to_s : '',
52
+ format: entry[:format].to_s
53
+ }
54
+ end
55
+ end
56
+ 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,14 @@ 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
+
40
+ def example_for(name)
41
+ (@config || {})[name.to_sym]&.fetch(:example, '') || ''
42
+ end
43
+
33
44
  def get_value(name)
34
45
  return @config[name][:group] if @config[name].key?(:group)
35
46
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmartConfig
4
- VERSION = '0.1.0'
4
+ VERSION = '0.3.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.3.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: []