Class: ParseUtil::HandleSwissmedicErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/oddb2xml/parslet_compositions.rb

Defined Under Namespace

Classes: ErrorEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_entries) ⇒ HandleSwissmedicErrors

error_entries should be a hash of pattern, replacement



32
33
34
35
# File 'lib/oddb2xml/parslet_compositions.rb', line 32

def initialize(error_entries)
  reset_errors
  error_entries.each { |pattern, replacement| @errors << ErrorEntry.new(pattern, replacement, 0) }
end

Instance Attribute Details

#nrParsingErrorsObject

Returns the value of attribute nrParsingErrors.



21
22
23
# File 'lib/oddb2xml/parslet_compositions.rb', line 21

def nrParsingErrors
  @nrParsingErrors
end

Instance Method Details

#apply_fixes(string) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/oddb2xml/parslet_compositions.rb', line 45

def apply_fixes(string)
  result = string.clone
  @errors.each { |entry|
    intermediate = result.clone
    result = result.gsub(entry.pattern, entry.replacement)
    unless result.eql?(intermediate)
      entry.nr_occurrences += 1
      puts "#{File.basename(__FILE__)}:#{__LINE__}: fixed \nbefore: #{intermediate}\nafter:  #{result}" if $VERBOSE
    end
  }
  @nr_lines += 1
  result
end

#reportObject



37
38
39
40
41
42
43
# File 'lib/oddb2xml/parslet_compositions.rb', line 37

def report
  s = ["Report of changed compositions in #{@nr_lines} lines. Had #{@nr_parsing_errors} parsing errors"]
  @errors.each { |entry|
    s << "  replaced #{entry.nr_occurrences} times '#{entry.pattern}'  by '#{entry.replacement}'"
  }
  s
end

#reset_errorsObject



25
26
27
28
29
# File 'lib/oddb2xml/parslet_compositions.rb', line 25

def reset_errors
  @errors = []
  @nr_lines = 0
  @nr_parsing_errors = 0
end