Class: SvgConform::Fixer

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_conform/fixer.rb

Overview

Utility class for applying fixes to SVG documents

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Fixer

Returns a new instance of Fixer.



8
9
10
11
# File 'lib/svg_conform/fixer.rb', line 8

def initialize(document)
  @document = document
  @fixes_applied = []
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



6
7
8
# File 'lib/svg_conform/fixer.rb', line 6

def document
  @document
end

#fixes_appliedObject (readonly)

Returns the value of attribute fixes_applied.



6
7
8
# File 'lib/svg_conform/fixer.rb', line 6

def fixes_applied
  @fixes_applied
end

Instance Method Details

#apply_fix(fix) ⇒ Object

Apply a single fix



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/svg_conform/fixer.rb', line 14

def apply_fix(fix)
  return false unless fix.respond_to?(:call) || fix.respond_to?(:apply)

  begin
    result = if fix.respond_to?(:call)
               fix.call
             else
               fix.apply
             end

    @fixes_applied << fix if result
    result
  rescue StandardError
    false
  end
end

#apply_fixes(fixes) ⇒ Object

Apply multiple fixes



32
33
34
35
36
37
38
39
40
# File 'lib/svg_conform/fixer.rb', line 32

def apply_fixes(fixes)
  success_count = 0

  fixes.each do |fix|
    success_count += 1 if apply_fix(fix)
  end

  success_count
end

#apply_validation_fixes(validation_result) ⇒ Object

Apply all fixable issues from a validation result



43
44
45
46
# File 'lib/svg_conform/fixer.rb', line 43

def apply_validation_fixes(validation_result)
  fixable_issues = (validation_result.errors + validation_result.warnings).select(&:fixable?)
  apply_fixes(fixable_issues.map(&:fix))
end

#fixes_countObject



48
49
50
# File 'lib/svg_conform/fixer.rb', line 48

def fixes_count
  @fixes_applied.size
end

#to_xmlObject



52
53
54
# File 'lib/svg_conform/fixer.rb', line 52

def to_xml
  @document.to_xml
end