Class: Uniword::Warnings::WarningCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/warnings/warning_collector.rb

Overview

Collects warnings during document processing.

Responsibility: Track unsupported elements and features. Single Responsibility: Only collects warnings.

This class is used during deserialization to track all unsupported elements and attributes encountered, allowing users to understand what features may be lost in round-trip operations.

External configuration: config/warning_rules.yml

Examples:

Use during deserialization

collector = WarningCollector.new
collector.record_unsupported('chart', context: 'In paragraph 5')
report = collector.report
puts report.summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file: nil) ⇒ WarningCollector

Initialize a new warning collector.

Examples:

Create with default config

collector = WarningCollector.new

Create with custom config

collector = WarningCollector.new(
  config_file: 'custom_warnings.yml'
)

Parameters:

  • config_file (String) (defaults to: nil)

    Path to configuration file



39
40
41
42
43
# File 'lib/uniword/warnings/warning_collector.rb', line 39

def initialize(config_file: nil)
  @config = load_config(config_file)
  @warnings = []
  @element_counts = Hash.new(0)
end

Instance Attribute Details

#element_countsHash<String, Integer> (readonly)

Returns Element occurrence counts.

Returns:

  • (Hash<String, Integer>)

    Element occurrence counts



26
27
28
# File 'lib/uniword/warnings/warning_collector.rb', line 26

def element_counts
  @element_counts
end

#warningsArray<Warning> (readonly)

Returns Collected warnings.

Returns:

  • (Array<Warning>)

    Collected warnings



23
24
25
# File 'lib/uniword/warnings/warning_collector.rb', line 23

def warnings
  @warnings
end

Instance Method Details

#any?Boolean

Check if warnings were collected.

Returns:

  • (Boolean)

    true if warnings exist



126
127
128
# File 'lib/uniword/warnings/warning_collector.rb', line 126

def any?
  @warnings.any?
end

#clearvoid

This method returns an undefined value.

Clear all collected warnings.



133
134
135
136
# File 'lib/uniword/warnings/warning_collector.rb', line 133

def clear
  @warnings.clear
  @element_counts.clear
end

#record_unsupported(element_tag, context:, location: nil) ⇒ void

This method returns an undefined value.

Record an unsupported element.

Examples:

Record unsupported element

collector.record_unsupported(
  'chart',
  context: 'While parsing paragraph',
  location: '/document/body/p[5]'
)

Parameters:

  • element_tag (String)

    Element tag name

  • context (String)

    Context where element was found

  • location (String) (defaults to: nil)

    Location in document (optional)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/uniword/warnings/warning_collector.rb', line 58

def record_unsupported(element_tag, context:, location: nil)
  return unless enabled?
  return if max_warnings_reached?

  @element_counts[element_tag] += 1

  warning = Warning.new(
    type: :unsupported_element,
    severity: determine_severity(element_tag),
    element: element_tag,
    message: "Unsupported element: #{element_tag}",
    context: context,
    location: location,
    suggestion: get_suggestion(element_tag),
  )

  @warnings << warning
  log_warning(warning) if should_log?
end

#record_unsupported_attribute(element_tag, attribute_name, context:) ⇒ void

This method returns an undefined value.

Record an unsupported attribute.

Examples:

Record unsupported attribute

collector.record_unsupported_attribute(
  'w:p',
  'customAttr',
  context: 'In paragraph properties'
)

Parameters:

  • element_tag (String)

    Element tag name

  • attribute_name (String)

    Attribute name

  • context (String)

    Context where attribute was found



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/uniword/warnings/warning_collector.rb', line 91

def record_unsupported_attribute(element_tag, attribute_name, context:)
  return unless enabled?
  return if max_warnings_reached?

  warning = Warning.new(
    type: :unsupported_attribute,
    severity: :info,
    element: element_tag,
    attribute: attribute_name,
    message: "Unsupported attribute: #{element_tag}/@#{attribute_name}",
    context: context,
  )

  @warnings << warning
  log_warning(warning) if should_log?
end

#reportWarningReport

Generate warning report.

Examples:

Get report

report = collector.report
puts "Total warnings: #{report.total_count}"

Returns:



115
116
117
118
119
120
121
# File 'lib/uniword/warnings/warning_collector.rb', line 115

def report
  WarningReport.new(
    warnings: @warnings,
    element_counts: @element_counts,
    config: @config,
  )
end