Class: ParseGalenicForm

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

Class Method Summary collapse

Class Method Details

.from_string(string) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/oddb2xml/parslet_compositions.rb', line 479

def self.from_string(string)
  return nil if string.nil?
  stripped = string.gsub(/^"|["\n]+$/, "")
  return nil unless stripped
  puts "ParseGalenicForm.from_string #{string}" if VERBOSE_MESSAGES # /ng-tr/.match(Socket.gethostbyname(Socket.gethostname).first)

  parser = GalenicFormParser.new
  transf = GalenicFormTransformer.new
  begin
    if defined?(RSpec)
      ast = transf.apply(parser.parse_with_debug(string))
      puts "#{File.basename(__FILE__)}:#{__LINE__}: ==> " if VERBOSE_MESSAGES
      pp ast if VERBOSE_MESSAGES
    else
      ast = transf.apply(parser.parse(string))
    end
  rescue Parslet::ParseFailed => error
    @@error_handler.nrParsingErrors += 1
    puts "#{File.basename(__FILE__)}:#{__LINE__}: failed parsing ==>  #{string} #{error}"
    return nil
  end
  return [] unless ast
  form = ast[:galenic_form] ? ast[:galenic_form].to_s.sub(/^\/\s+/, "") : nil
  name = ast[:prepation_name] ? ast[:prepation_name].to_s.strip : nil
  [name, form]
end