Module: Ast::Merge::Healer
- Defined in:
- lib/ast/merge/healer.rb
Overview
Shared handling contract for suspected corruption / healing sites.
Constant Summary collapse
- HANDLINGS =
%i[heal warn error skip].freeze
Class Method Summary collapse
- .filter_items(items, mode:, kind:, message:, prefix:, error_class: CorruptionDetectedError, warner: nil, on_filter: nil) ⇒ Object
- .format_message(prefix:, kind:, message:) ⇒ Object
- .handle(mode:, kind:, message:, prefix:, error_class: CorruptionDetectedError, warner: nil) ⇒ Object
- .normalize_mode(value) ⇒ Object
Class Method Details
.filter_items(items, mode:, kind:, message:, prefix:, error_class: CorruptionDetectedError, warner: nil, on_filter: nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ast/merge/healer.rb', line 41 def filter_items(items, mode:, kind:, message:, prefix:, error_class: CorruptionDetectedError, warner: nil, on_filter: nil) matches = Array(items).map { |item| [item, yield(item)] } return items unless matches.any? { |_, matched| matched } return items unless handle( mode: mode, kind: kind, message: , prefix: prefix, error_class: error_class, warner: warner ) matches.each_with_object([]) do |(item, matched), filtered| if matched on_filter&.call(item) else filtered << item end end end |
.format_message(prefix:, kind:, message:) ⇒ Object
37 38 39 |
# File 'lib/ast/merge/healer.rb', line 37 def (prefix:, kind:, message:) "#{prefix} Suspected corruption (#{kind}): #{}" end |
.handle(mode:, kind:, message:, prefix:, error_class: CorruptionDetectedError, warner: nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ast/merge/healer.rb', line 18 def handle(mode:, kind:, message:, prefix:, error_class: CorruptionDetectedError, warner: nil) normalized = normalize_mode(mode) formatted = (prefix: prefix, kind: kind, message: ) case normalized when :heal true when :warn (warner || Kernel.method(:warn)).call(formatted) false when :error raise error_class, formatted when :skip false else raise ArgumentError, "Unknown corruption handling mode: #{normalized.inspect}" end end |
.normalize_mode(value) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/ast/merge/healer.rb', line 11 def normalize_mode(value) normalized = value.to_sym return normalized if HANDLINGS.include?(normalized) raise ArgumentError, "Unknown corruption handling mode: #{value.inspect}" end |