openvox-strings 6.1.0 → 7.0.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: e110d114b1600a477064071570a4999be41d69c8a9dc239686507dafe2cb8087
4
- data.tar.gz: 0a3c79360b241f4693d5cd3850a83b4363efb0f30c1b14fc2b0ed0bb8519ae3e
3
+ metadata.gz: 8a4305998c36c2f258decc43a46de8b89ebbd52492e9f4dd003909af5b657abe
4
+ data.tar.gz: 741e317d5ee54b75e63ebef4abe8795d1752046cd733dbae1b3abe436499fd1e
5
5
  SHA512:
6
- metadata.gz: 2fb4c34bb55826086049bf9ec865d5f31bcbe832135598f3fcdc843a12527e0697d5e51cda0d95a1ea7e53312babc92cea788d070e44d61597c41763ccd3cd09
7
- data.tar.gz: 73c68a76542d8ef8b0362f1ac868a563656eef01ce0cc7d012a3f8bae6648c05672577ce03f21fa7a2a9b026bde0480ba739ffd73deeb96201962f544dcf1dcd
6
+ metadata.gz: bda5adbbf34a9f57bdc095bb28968a65e0c62bbbfefbfdd213db22015a387b246ab94f71ae3661dc8bea734f0bd0355b5b1f724c75317a7aff649eee626d8ea5
7
+ data.tar.gz: 3361070dfc9cf61198cfc8162a40bbe83c0f0a1b72be9bedbd962a85eb18846a2a88bcffaf0e4da6fc83c1dbdb35bf3d54ab2b80f12d115b60a16caeb497b1be
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [v7.0.0](https://github.com/voxpupuli/openvox-strings/tree/v7.0.0) (2026-02-19)
6
+
7
+ [Full Changelog](https://github.com/voxpupuli/openvox-strings/compare/v6.1.0...v7.0.0)
8
+
9
+ **Breaking changes:**
10
+
11
+ - Add Hiera defaults support for module documentation [\#27](https://github.com/voxpupuli/openvox-strings/pull/27) ([slauger](https://github.com/slauger))
12
+
5
13
  ## [v6.1.0](https://github.com/voxpupuli/openvox-strings/tree/v6.1.0) (2026-02-19)
6
14
 
7
15
  [Full Changelog](https://github.com/voxpupuli/openvox-strings/compare/v6.0.0...v6.1.0)
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module OpenvoxStrings
6
+ # Parser for Hiera configuration and data
7
+ class Hiera
8
+ attr_reader :hiera_config, :common_data
9
+
10
+ # Initializes a Hiera parser for a given module path
11
+ # @param [String] module_path The path to the Puppet module root directory
12
+ def initialize(module_path)
13
+ @module_path = module_path
14
+ @hiera_config = load_hiera_config
15
+ @common_data = load_common_data
16
+ end
17
+
18
+ # Checks if Hiera is configured for this module
19
+ # @return [Boolean] true if hiera.yaml exists and is valid
20
+ def hiera_enabled?
21
+ !@hiera_config.nil?
22
+ end
23
+
24
+ # Gets the default value for a parameter from Hiera data
25
+ # @param [String] class_name The fully qualified class name (e.g., 'github_actions_runner')
26
+ # @param [String] param_name The parameter name
27
+ # @return [String, nil] The default value as a string, or nil if not found
28
+ def lookup_default(class_name, param_name)
29
+ return nil unless hiera_enabled?
30
+ return nil if @common_data.nil?
31
+
32
+ # Try to lookup with class prefix: modulename::parametername
33
+ key = "#{class_name}::#{param_name}"
34
+ return nil unless @common_data.key?(key)
35
+
36
+ value = @common_data[key]
37
+
38
+ # Convert value to Puppet-compatible string representation
39
+ value_to_puppet_string(value)
40
+ end
41
+
42
+ private
43
+
44
+ # Loads and parses hiera.yaml from the module root
45
+ # @return [Hash, nil] The parsed hiera configuration, or nil if not found/invalid
46
+ def load_hiera_config
47
+ hiera_file = File.join(@module_path, 'hiera.yaml')
48
+ load_yaml_data(hiera_file)
49
+ end
50
+
51
+ # Finds the path to the first static hierarchy layer (without interpolations)
52
+ # @return [String, nil] The full path to the data file, or nil if not found
53
+ def find_first_static_layer_path
54
+ return nil unless hiera_enabled?
55
+
56
+ # Get datadir from hiera config (defaults to 'data')
57
+ datadir = @hiera_config.dig('defaults', 'datadir') || 'data'
58
+
59
+ # Find first hierarchy entry without interpolations
60
+ hierarchy = @hiera_config['hierarchy']
61
+ return nil unless hierarchy
62
+
63
+ first_static = hierarchy.find do |entry|
64
+ path_or_paths = entry['path'] || entry['paths']
65
+ next false unless path_or_paths
66
+
67
+ # Check if path(s) contain interpolations like %{...}
68
+ case path_or_paths
69
+ when String
70
+ !path_or_paths.include?('%{')
71
+ when Array
72
+ path_or_paths.none? { |p| p.include?('%{') }
73
+ else
74
+ false
75
+ end
76
+ end
77
+
78
+ return nil unless first_static
79
+
80
+ # Get the path from the hierarchy entry
81
+ data_file_path = first_static['path'] || first_static['paths']&.first
82
+ return nil unless data_file_path
83
+
84
+ # Build and return full path
85
+ File.join(@module_path, datadir, data_file_path)
86
+ end
87
+
88
+ # Loads and parses a YAML data file
89
+ # @param [String] file_path The full path to the YAML file to load
90
+ # @return [Hash, nil] The parsed YAML data, or nil if not found/invalid
91
+ def load_yaml_data(file_path)
92
+ return nil unless File.exist?(file_path)
93
+
94
+ begin
95
+ YAML.load_file(file_path)
96
+ rescue StandardError => e
97
+ YARD::Logger.instance.warn "Failed to parse #{File.basename(file_path)}: #{e.message}"
98
+ nil
99
+ end
100
+ end
101
+
102
+ # Loads and parses the first static hierarchy layer (without interpolations)
103
+ # @return [Hash, nil] The parsed data, or nil if not found/invalid
104
+ def load_common_data
105
+ data_file = find_first_static_layer_path
106
+ return nil unless data_file
107
+
108
+ load_yaml_data(data_file)
109
+ end
110
+
111
+ # Converts a Ruby value to a Puppet-compatible string representation
112
+ # @param [Object] value The value to convert
113
+ # @return [String] The Puppet-compatible string representation
114
+ def value_to_puppet_string(value)
115
+ case value
116
+ when String
117
+ # Empty strings from YAML nil (~) should be undef
118
+ return 'undef' if value.empty?
119
+
120
+ # Strings should be quoted
121
+ "'#{value}'"
122
+ when Integer, Float, TrueClass, FalseClass
123
+ # Numbers and booleans are unquoted/lowercase
124
+ value.to_s
125
+ when NilClass, :undef
126
+ # Puppet undef
127
+ 'undef'
128
+ when Hash
129
+ # Convert hash to Puppet hash syntax (no spaces to match code defaults format)
130
+ return '{}' if value.empty?
131
+
132
+ pairs = value.map { |k, v| "'#{k}' => #{value_to_puppet_string(v)}" }
133
+ "{ #{pairs.join(', ')} }"
134
+ when Array
135
+ # Convert array to Puppet array syntax (no spaces to match code defaults format)
136
+ return '[]' if value.empty?
137
+
138
+ elements = value.map { |v| value_to_puppet_string(v) }
139
+ "[#{elements.join(', ')}]"
140
+ else
141
+ # Fallback: convert to string and quote
142
+ "'#{value}'"
143
+ end
144
+ end
145
+ end
146
+ end
@@ -4,6 +4,7 @@ require 'openvox-strings'
4
4
  require 'openvox-strings/json'
5
5
  require 'openvox-strings/yard'
6
6
  require 'openvox-strings/markdown/helpers'
7
+ require 'openvox-strings/hiera'
7
8
 
8
9
  # Implements classes that make elements in a YARD::Registry hash easily accessible for template.
9
10
  module OpenvoxStrings::Markdown
@@ -86,6 +87,7 @@ module OpenvoxStrings::Markdown
86
87
  @type = component_type
87
88
  @registry = registry
88
89
  @tags = registry[:docstring][:tags] || []
90
+ @hiera = initialize_hiera
89
91
  end
90
92
 
91
93
  # generate 1:1 tag methods
@@ -174,7 +176,13 @@ module OpenvoxStrings::Markdown
174
176
 
175
177
  # @return [Hash] any defaults found for the component
176
178
  def defaults
177
- @registry[:defaults] unless @registry[:defaults].nil?
179
+ # Start with code defaults from the Puppet class
180
+ code_defaults = @registry[:defaults] || {}
181
+
182
+ # Try to merge with Hiera defaults if available
183
+ merged_defaults = merge_hiera_defaults(code_defaults)
184
+
185
+ merged_defaults.empty? ? nil : merged_defaults
178
186
  end
179
187
 
180
188
  # @return [Hash] information needed for the table of contents
@@ -216,6 +224,59 @@ module OpenvoxStrings::Markdown
216
224
 
217
225
  private
218
226
 
227
+ # Initializes Hiera integration for this component
228
+ # @return [OpenvoxStrings::Hiera, nil] Hiera instance or nil if not available
229
+ def initialize_hiera
230
+ return nil unless @registry[:file]
231
+
232
+ # Find the module root directory from the file path
233
+ # Puppet modules have manifests/, lib/, data/ etc. at the root
234
+ module_path = find_module_root(@registry[:file])
235
+ return nil unless module_path
236
+
237
+ OpenvoxStrings::Hiera.new(module_path)
238
+ rescue StandardError => e
239
+ YARD::Logger.instance.debug "Failed to initialize Hiera: #{e.message}"
240
+ nil
241
+ end
242
+
243
+ # Finds the module root directory from a file path
244
+ # @param [String] file_path The path to a file in the module
245
+ # @return [String, nil] The module root path or nil if not found
246
+ def find_module_root(file_path)
247
+ names = %w[metadata.json manifests hiera.yaml]
248
+ Pathname.new(file_path).expand_path.parent.ascend do |current_path|
249
+ names.each do |name|
250
+ return current_path if (current_path / name).exist?
251
+ end
252
+ end
253
+ end
254
+
255
+ # Merges code defaults with Hiera defaults
256
+ # @param [Hash] code_defaults The defaults from the Puppet code
257
+ # @return [Hash] Merged defaults with code defaults taking precedence
258
+ def merge_hiera_defaults(code_defaults)
259
+ return code_defaults unless @hiera&.hiera_enabled?
260
+
261
+ # Start with Hiera defaults
262
+ merged = {}
263
+
264
+ # Get all parameters from the docstring
265
+ param_tags = @tags.select { |tag| tag[:tag_name] == 'param' }
266
+
267
+ param_tags.each do |param_tag|
268
+ param_name = param_tag[:name]
269
+ next unless param_name
270
+
271
+ # Try to get default from Hiera
272
+ hiera_default = @hiera.lookup_default(name, param_name)
273
+ merged[param_name] = hiera_default if hiera_default
274
+ end
275
+
276
+ # Code defaults override Hiera defaults
277
+ merged.merge(code_defaults)
278
+ end
279
+
219
280
  def select_tags(name)
220
281
  tags = @tags.select { |tag| tag[:tag_name] == name }
221
282
  tags.empty? ? nil : tags
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenvoxStrings
4
- VERSION = '6.1.0'
4
+ VERSION = '7.0.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openvox-strings
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0
4
+ version: 7.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet Inc.
@@ -105,6 +105,7 @@ files:
105
105
  - README.md
106
106
  - lib/openvox-strings.rb
107
107
  - lib/openvox-strings/describe.rb
108
+ - lib/openvox-strings/hiera.rb
108
109
  - lib/openvox-strings/json.rb
109
110
  - lib/openvox-strings/markdown.rb
110
111
  - lib/openvox-strings/markdown/base.rb