Class: MESH::Mesh

Inherits:
Object
  • Object
show all
Defined in:
lib/MESH/mesh.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def children
  @children
end

#entries(locale = nil) ⇒ Object

Returns the value of attribute entries.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def entries
  @entries
end

#natural_language_name(locale = nil) ⇒ Object

Returns the value of attribute natural_language_name.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def natural_language_name
  @natural_language_name
end

#original_heading(locale = nil) ⇒ Object

Returns the value of attribute original_heading.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def original_heading
  @original_heading
end

#parentsObject

Returns the value of attribute parents.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def parents
  @parents
end

#summary(locale = nil) ⇒ Object

Returns the value of attribute summary.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def summary
  @summary
end

#tree_numbersObject

Returns the value of attribute tree_numbers.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def tree_numbers
  @tree_numbers
end

#unique_idObject

Returns the value of attribute unique_id.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def unique_id
  @unique_id
end

#usefulObject

Returns the value of attribute useful.



6
7
8
# File 'lib/MESH/mesh.rb', line 6

def useful
  @useful
end

Class Method Details

.configure(args) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/MESH/mesh.rb', line 28

def self.configure(args)
  return if @@configured
  raise ArgumentError.new('MeshHeadingGraph requires a filename in order to configure itself') unless not args[:filename].nil?
  gzipped_file = File.open(args[:filename])
  file = Zlib::GzipReader.new(gzipped_file)
  current_heading = Mesh.new
  file.each_line do |line|
    if line.match(/^\*NEWRECORD$/) #Then store the previous record before continuing
      unless current_heading.unique_id.nil?
        current_heading.entries.sort!
        @@headings << current_heading
        @@by_unique_id[current_heading.unique_id] = current_heading
        @@by_original_heading[current_heading.original_heading] = current_heading
        current_heading.tree_numbers.each do |tree_number|
          @@by_tree_number[tree_number] = current_heading
        end
      end
      current_heading = Mesh.new
    end

    matches = line.match(/^UI = (.*)/)
    current_heading.unique_id = matches[1] unless matches.nil?

    matches = line.match(/^MN = (.*)/)
    current_heading.tree_numbers << matches[1] unless matches.nil?

    matches = line.match(/^MS = (.*)/)
    current_heading.summary = matches[1] unless matches.nil?

    matches = line.match(/^MH = (.*)/)
    unless matches.nil?
      mh = matches[1]
      current_heading.original_heading = mh
      current_heading.natural_language_name = mh
      current_heading.entries << mh
      librarian_parts = mh.match(/(.*), (.*)/)
      current_heading.natural_language_name = "#{librarian_parts[2]} #{librarian_parts[1]}" unless librarian_parts.nil?
    end

    matches = line.match(/^(?:PRINT )?ENTRY = ([^|]+)/)
    unless matches.nil?
      mh = matches[1].chomp
      current_heading.entries << mh
    end

  end

  @@by_unique_id.each do |id, heading|
    heading.tree_numbers.each do |tree_number|
      #D03.438.221.173
      parts = tree_number.split('.')
      if parts.size > 1
        parts.pop
        parent_tree_number = parts.join '.'
        parent = @@by_tree_number[parent_tree_number]
        heading.parents << parent unless parent.nil?
        parent.children << heading unless parent.nil?
      end
    end
  end
  @@configured = true
end

.eachObject



114
115
116
117
118
# File 'lib/MESH/mesh.rb', line 114

def self.each
  for i in 0 ... @@headings.size
    yield @@headings[i] if @@headings[i].useful
  end
end

.find(unique_id) ⇒ Object



91
92
93
94
# File 'lib/MESH/mesh.rb', line 91

def self.find(unique_id)
  raise 'MeshHeadingGraph.configure must be called before use' unless @@configured
  return @@by_unique_id[unique_id]
end

.find_by_original_heading(heading) ⇒ Object



101
102
103
104
# File 'lib/MESH/mesh.rb', line 101

def self.find_by_original_heading(heading)
  raise 'MeshHeadingGraph.configure must be called before use' unless @@configured
  return @@by_original_heading[heading]
end

.find_by_tree_number(tree_number) ⇒ Object



96
97
98
99
# File 'lib/MESH/mesh.rb', line 96

def self.find_by_tree_number(tree_number)
  raise 'MeshHeadingGraph.configure must be called before use' unless @@configured
  return @@by_tree_number[tree_number]
end

.match_in_text(text) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/MESH/mesh.rb', line 120

def self.match_in_text(text)
  text = text.downcase
  matches = []
  @@headings.each do |heading|
    next unless heading.useful
    heading.entries.each do |entry|
      if text.include? entry.downcase #This is a looser check than the regex but much, much faster
        regex = /(^|\W)#{Regexp.quote(entry)}(\W|$)/i
        if regex =~ text
          matches << {heading: heading, matched: entry}
        end
      end
    end
  end
  matches
end

.where(conditions) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/MESH/mesh.rb', line 106

def self.where(conditions)
  matches = []
  @@headings.each do |heading|
    matches << heading if heading.matches(conditions)
  end
  matches
end

Instance Method Details

#inspectObject



151
152
153
# File 'lib/MESH/mesh.rb', line 151

def inspect
  return "#{@unique_id}, #{@original_heading}"
end

#matches(conditions) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/MESH/mesh.rb', line 137

def matches(conditions)
  conditions.each do |field, pattern|
    field_content = self.send(field)
    if field_content.kind_of?(Array)
      return false unless field_content.find { |fc| pattern =~ fc }
    elsif field_content.is_a?(TrueClass) || field_content.is_a?(FalseClass)
      return false unless field_content == pattern
    else
      return false unless pattern =~ field_content
    end
  end
  return true
end