Class: MESH::Heading

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/MESH/heading.rb

Constant Summary collapse

@@descriptor_classes =
[:make_array_start_at_1, :topical_descriptor, :publication_type, :check_tag, :geographic_descriptor]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def children
  @children
end

#default_localeObject

Returns the value of attribute default_locale.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def default_locale
  @default_locale
end

#descriptor_classObject

Returns the value of attribute descriptor_class.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def descriptor_class
  @descriptor_class
end

#forward_referencesObject

Returns the value of attribute forward_references.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def forward_references
  @forward_references
end

#linkified_summaryObject (readonly)

Returns the value of attribute linkified_summary.



8
9
10
# File 'lib/MESH/heading.rb', line 8

def linkified_summary
  @linkified_summary
end

#parentsObject

Returns the value of attribute parents.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def parents
  @parents
end

#rootsObject

Returns the value of attribute roots.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def roots
  @roots
end

#semantic_typesObject

Returns the value of attribute semantic_types.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def semantic_types
  @semantic_types
end

#structured_entriesObject

Returns the value of attribute structured_entries.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def structured_entries
  @structured_entries
end

#tree_numbersObject

Returns the value of attribute tree_numbers.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def tree_numbers
  @tree_numbers
end

#unique_idObject

Returns the value of attribute unique_id.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def unique_id
  @unique_id
end

#usefulObject

Returns the value of attribute useful.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def useful
  @useful
end

Returns the value of attribute wikipedia_links.



7
8
9
# File 'lib/MESH/heading.rb', line 7

def wikipedia_links
  @wikipedia_links
end

Instance Method Details

#<=>(other) ⇒ Object



10
11
12
# File 'lib/MESH/heading.rb', line 10

def <=> other
  self.unique_id <=> other.unique_id
end

#connect_to_forward_referencesObject



122
123
124
125
126
127
128
129
# File 'lib/MESH/heading.rb', line 122

def connect_to_forward_references
  if !@connected_to_forward_references
    @forward_references = @forward_reference_terms.map do |term|
      @tree.find_by_original_heading(term)
    end
    @connected_to_forward_references = true
  end
end

#connect_to_parentsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/MESH/heading.rb', line 105

def connect_to_parents
  if !@connected_to_parents
    @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 = @tree.find_by_tree_number(parent_tree_number)
        @parents << parent unless parent.nil? || @parents.include?(parent)
        parent.children << self unless parent.nil? || parent.children.include?(self)
      end
    end
    @connected_to_parents = true
  end
end

#deepest_position(root = '') ⇒ Object



57
58
59
60
61
# File 'lib/MESH/heading.rb', line 57

def deepest_position(root = '')
  return nil if tree_numbers.empty?
  deepest_tree_number = tree_numbers.max_by { |tn| tn.start_with?(root) ? tn.length : 0 }
  deepest_tree_number.split('.').length
end

#entries(locale = default_locale) ⇒ Object



34
35
36
# File 'lib/MESH/heading.rb', line 34

def entries(locale = default_locale)
  @entries[locale] ||= []
end

#has_ancestor(heading) ⇒ Object



38
39
40
41
42
43
# File 'lib/MESH/heading.rb', line 38

def has_ancestor(heading)
  return false if parents.empty?
  return true if parents.include? heading
  in_grandparents = parents.map { |p| p.has_ancestor(heading) }
  return in_grandparents.include? true
end

#has_descendant(heading) ⇒ Object



45
46
47
48
49
50
# File 'lib/MESH/heading.rb', line 45

def has_descendant(heading)
  return false if children.empty?
  return true if children.include? heading
  in_grandchildren = children.map { |p| p.has_descendant(heading) }
  return in_grandchildren.include? true
end

#inspectObject



89
90
91
# File 'lib/MESH/heading.rb', line 89

def inspect
  to_s
end

#linkify_summaryObject



26
27
28
29
30
31
32
# File 'lib/MESH/heading.rb', line 26

def linkify_summary
  return if summary.nil?
  @linkified_summary = summary.gsub(/[A-Z]+[A-Z,\s-]+[A-Z]+/).each do |text|
    heading = @tree.find_by_entry(text)
    heading ? yield(text, heading) : text
  end
end

#matches(conditions) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/MESH/heading.rb', line 69

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
    elsif field_content.is_a? Symbol
      return field_content == pattern
    else
      return false unless pattern =~ field_content
    end
  end
  return true
end

#natural_language_name(locale = default_locale) ⇒ Object



18
19
20
# File 'lib/MESH/heading.rb', line 18

def natural_language_name(locale = default_locale)
  @natural_language_name[locale]
end

#original_heading(locale = default_locale) ⇒ Object



14
15
16
# File 'lib/MESH/heading.rb', line 14

def original_heading(locale = default_locale)
  @original_heading[locale]
end

#set_natural_language_name(name, locale = default_locale) ⇒ Object



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

def set_natural_language_name(name, locale = default_locale)
  @natural_language_name[locale] = name
end

#set_original_heading(heading, locale = default_locale) ⇒ Object



93
94
95
# File 'lib/MESH/heading.rb', line 93

def set_original_heading(heading, locale = default_locale)
  @original_heading[locale] = heading
end

#set_summary(summary, locale = default_locale) ⇒ Object



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

def set_summary(summary, locale = default_locale)
  @summary[locale] = summary
end

#shallowest_positionObject



63
64
65
66
67
# File 'lib/MESH/heading.rb', line 63

def shallowest_position
  return nil if tree_numbers.empty?
  shallowest_tree_number = tree_numbers.min_by { |tn| tn.length }
  shallowest_tree_number.split('.').length
end

#sibling?(heading) ⇒ Boolean



52
53
54
55
# File 'lib/MESH/heading.rb', line 52

def sibling?(heading)
  common_parents = parents & heading.parents
  !common_parents.empty?
end

#summary(locale = default_locale) ⇒ Object



22
23
24
# File 'lib/MESH/heading.rb', line 22

def summary(locale = default_locale)
  @summary[locale]
end

#to_sObject



85
86
87
# File 'lib/MESH/heading.rb', line 85

def to_s
  "#{unique_id}, #{original_heading}, [#{tree_numbers.join(',')}]"
end