Method: Doing::Configuration#resolve_key_path
- Defined in:
- lib/doing/configuration.rb
#resolve_key_path(keypath, create: false, distance: 2) ⇒ Array
Resolve a fuzzy-matched key path
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/doing/configuration.rb', line 203 def resolve_key_path(keypath, create: false, distance: 2) cfg = @settings real_path = [] unless keypath =~ /^[.*]?$/ paths = keypath.split(/[:.]/) element_count = paths.count while paths.length.positive? && !cfg.nil? path = paths.shift new_cfg = nil if cfg.is_a?(Hash) matches = cfg.select { |key, val| key =~ path.to_rx(distance: distance) } if matches.count.positive? shortest = matches.keys.group_by(&:length).min.last[0] real_path << shortest new_cfg = matches[shortest] end else new_cfg = cfg end if new_cfg.nil? return real_path if real_path[-1] == path && real_path.count == element_count if distance < 5 && !create return resolve_key_path(keypath, create: false, distance: distance + 1) else return nil unless create end resolved = real_path.count.positive? ? "Resolved #{real_path.join('.')}, but " : '' Doing.logger.log_now(:warn, "#{resolved}#{path} is unknown") new_path = [*real_path, path, *paths].compact.join('.') Doing.logger.log_now(:warn, "Continuing will create the path #{new_path}") res = Prompt.yn('Key path not found, create it?', default_response: true) raise InvalidArgument, 'Invalid key path' unless res real_path.push(path).concat(paths).compact! Doing.logger.debug('Config:', "translated key path #{keypath} to #{real_path.join('.')}") return real_path end cfg = new_cfg end end Doing.logger.debug('Config:', "translated key path #{keypath} to #{real_path.join('.')}") real_path end |