Class: Qa::Authorities::MeshTools::MeshDataParser

Inherits:
Object
  • Object
show all
Defined in:
lib/qa/authorities/mesh_tools/mesh_data_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ MeshDataParser

Returns a new instance of MeshDataParser.



7
8
9
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 7

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 5

def file
  @file
end

Class Method Details

.get_description(record) ⇒ Object



50
51
52
53
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 50

def self.get_description(record)
  return [] if record['MS'].blank?
  record['MS']
end

.get_print_synonyms(record) ⇒ Object



45
46
47
48
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 45

def self.get_print_synonyms(record)
  return [] if record['PRINT ENTRY'].blank?
  record['PRINT ENTRY'].map { |synonym| synonym.split('|').first }
end

.get_synonyms(record) ⇒ Object

XXX: delete everything below?



40
41
42
43
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 40

def self.get_synonyms(record)
  return [] if record['ENTRY'].blank?
  record['ENTRY'].map { |synonym| synonym.split('|').first }
end

.get_term(record) ⇒ Object



60
61
62
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 60

def self.get_term(record)
  record['MH'].first
end

.get_tree(record) ⇒ Object



55
56
57
58
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 55

def self.get_tree(record)
  return [] if record['MN'].blank?
  record['MN']
end

Instance Method Details

#all_recordsObject



32
33
34
35
36
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 32

def all_records
  result = []
  self.each_mesh_record {|rec| result << rec }
  return result
end

#each_mesh_record {|current_data| ... } ⇒ Object

Yields:

  • (current_data)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/qa/authorities/mesh_tools/mesh_data_parser.rb', line 11

def each_mesh_record(&block)
  current_data = {}
  in_record = false
  self.file.each_line do |line|
    case line
    when /\A\*NEWRECORD/
      yield(current_data) if in_record
      in_record = true
      current_data = {}
    when /\A(?<term>[^=]+) = (?<value>.*)/
      current_data[Regexp.last_match(:term)] ||= []
      current_data[Regexp.last_match(:term)] << Regexp.last_match(:value).strip
    when /\A\n/
      yield(current_data) if in_record
      in_record = false
    end
  end
  # final time in case file did not end with a blank line
  yield(current_data) if in_record
end