Class: Nomen::NomenclatureSet

Inherits:
Object
  • Object
show all
Defined in:
lib/nomen/nomenclature_set.rb

Overview

This class represents a set of nomenclature like the reference DB

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNomenclatureSet

Returns a new instance of NomenclatureSet.


6
7
8
9
# File 'lib/nomen/nomenclature_set.rb', line 6

def initialize
  @nomenclatures = {}.with_indifferent_access
  @version = 0
end

Instance Attribute Details

#versionObject

Returns the value of attribute version


4
5
6
# File 'lib/nomen/nomenclature_set.rb', line 4

def version
  @version
end

Class Method Details

.load_file(file) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nomen/nomenclature_set.rb', line 20

def self.load_file(file)
  set = new
  f = File.open(file, 'rb')
  document = Nokogiri::XML(f) do |config|
    config.strict.nonet.noblanks.noent
  end
  f.close
  document.root.children.each do |nomenclature|
    set.harvest_nomenclature(nomenclature)
  end
  set.version = document.root['version'].to_i
  set
end

Instance Method Details

#[](nomenclature_name) ⇒ Object Also known as: find, nomenclature

Find nomenclature


43
44
45
# File 'lib/nomen/nomenclature_set.rb', line 43

def [](nomenclature_name)
  @nomenclatures[nomenclature_name]
end

#add_item(nomenclature_name, item_name, options = {}) ⇒ Object


162
163
164
165
166
# File 'lib/nomen/nomenclature_set.rb', line 162

def add_item(nomenclature_name, item_name, options = {})
  nomenclature = find!(nomenclature_name)
  options = nomenclature.cast_options(options)
  nomenclature.add_item(item_name, options)
end

#add_nomenclature(name, options = {}) ⇒ Object


120
121
122
123
124
# File 'lib/nomen/nomenclature_set.rb', line 120

def add_nomenclature(name, options = {})
  raise "Nomenclature #{name} already exists" if @nomenclatures[name]
  options[:set] = self
  @nomenclatures[name] = Nomenclature.new(name, options)
end

#add_property(nomenclature_name, property_name, type, options = {}) ⇒ Object


149
150
151
152
# File 'lib/nomen/nomenclature_set.rb', line 149

def add_property(nomenclature_name, property_name, type, options = {})
  nomenclature = find!(nomenclature_name)
  nomenclature.add_property(property_name, type, options)
end

#change_item(nomenclature_name, item_name, updates = {}) ⇒ Object


168
169
170
171
172
# File 'lib/nomen/nomenclature_set.rb', line 168

def change_item(nomenclature_name, item_name, updates = {})
  nomenclature = find!(nomenclature_name)
  updates = nomenclature.cast_options(updates)
  nomenclature.change_item(item_name, updates)
end

#change_nomenclature(nomenclature_name, updates = {}) ⇒ Object


135
136
137
138
139
140
141
142
# File 'lib/nomen/nomenclature_set.rb', line 135

def change_nomenclature(nomenclature_name, updates = {})
  nomenclature = find!(nomenclature_name)
  nomenclature.update_attributes(updates)
  if updates[:name]
    nomenclature = move_nomenclature(nomenclature_name, updates[:name])
  end
  nomenclature
end

#change_property(_nomenclature_name, _property_name, _updates = {}) ⇒ Object

Raises:

  • (NotImplementedError)

154
155
156
# File 'lib/nomen/nomenclature_set.rb', line 154

def change_property(_nomenclature_name, _property_name, _updates = {})
  raise NotImplementedError
end

#each(&block) ⇒ Object


72
73
74
75
76
77
78
# File 'lib/nomen/nomenclature_set.rb', line 72

def each(&block)
  if block.arity == 2
    @nomenclatures.each(&block)
  else
    nomenclatures.each(&block)
  end
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/nomen/nomenclature_set.rb', line 68

def exist?(name)
  @nomenclatures[name].present?
end

#find!(name) ⇒ Object


61
62
63
64
65
66
# File 'lib/nomen/nomenclature_set.rb', line 61

def find!(name)
  unless nomenclature = @nomenclatures[name]
    raise "Nomenclature #{name} does not exist"
  end
  nomenclature
end

#harvest_nomenclature(element) ⇒ Object


115
116
117
118
# File 'lib/nomen/nomenclature_set.rb', line 115

def harvest_nomenclature(element)
  nomenclature = Nomenclature.harvest(element, set: self)
  @nomenclatures[nomenclature.name] = nomenclature
end

#item(nomenclature_name, item_name) ⇒ Object

Find item


50
51
52
53
# File 'lib/nomen/nomenclature_set.rb', line 50

def item(nomenclature_name, item_name)
  nomenclature = find!(nomenclature_name)
  nomenclature.item(item_name)
end

#load_data_from_xml(nomenclature_name) ⇒ Object


11
12
13
14
15
16
17
18
# File 'lib/nomen/nomenclature_set.rb', line 11

def load_data_from_xml(nomenclature_name)
  Rails.logger.info "Loading #{nomenclature_name}..."
  start = Time.now
  element = Nomen.reference_document.xpath("/xmlns:nomenclatures/xmlns:nomenclature[@name='#{nomenclature_name}']")
  nomenclature = harvest_nomenclature(element)
  Rails.logger.info "Loaded #{nomenclature_name} in #{(Time.now - start).round(2)} seconds..."
  nomenclature
end

#merge_item(nomenclature_name, item_name, into) ⇒ Object


174
175
176
177
# File 'lib/nomen/nomenclature_set.rb', line 174

def merge_item(nomenclature_name, item_name, into)
  nomenclature = find!(nomenclature_name)
  nomenclature.merge_item(item_name, into)
end

#move_nomenclature(old_name, new_name) ⇒ Object


126
127
128
129
130
131
132
133
# File 'lib/nomen/nomenclature_set.rb', line 126

def move_nomenclature(old_name, new_name)
  unless @nomenclatures[old_name]
    raise "Nomenclature #{old_name} does not exist"
  end
  raise "Nomenclature #{new_name} already exists" if @nomenclatures[new_name]
  @nomenclatures[new_name] = @nomenclatures.delete(old_name)
  @nomenclatures[new_name]
end

#nomenclature_namesObject


34
35
36
# File 'lib/nomen/nomenclature_set.rb', line 34

def nomenclature_names
  @nomenclatures.keys
end

#nomenclaturesObject


38
39
40
# File 'lib/nomen/nomenclature_set.rb', line 38

def nomenclatures
  @nomenclatures.values
end

#property(nomenclature_name, property_name) ⇒ Object

Find property


56
57
58
59
# File 'lib/nomen/nomenclature_set.rb', line 56

def property(nomenclature_name, property_name)
  nomenclature = find!(nomenclature_name)
  nomenclature.property(property_name)
end

#referencesObject

Returns references between nomenclatures


81
82
83
84
85
86
87
# File 'lib/nomen/nomenclature_set.rb', line 81

def references
  list = []
  each do |nomenclature|
    list += nomenclature.references
  end
  list
end

#remove_item(nomenclature_name, item_name) ⇒ Object


179
180
181
182
# File 'lib/nomen/nomenclature_set.rb', line 179

def remove_item(nomenclature_name, item_name)
  nomenclature = find!(nomenclature_name)
  nomenclature.remove_item(item_name)
end

#remove_nomenclature(nomenclature_name) ⇒ Object


144
145
146
147
# File 'lib/nomen/nomenclature_set.rb', line 144

def remove_nomenclature(nomenclature_name)
  nomenclature = find!(nomenclature_name)
  @nomenclatures.delete(nomenclature_name)
end

#remove_property(_nomenclature_name, _property_name, _options = {}) ⇒ Object

Raises:

  • (NotImplementedError)

158
159
160
# File 'lib/nomen/nomenclature_set.rb', line 158

def remove_property(_nomenclature_name, _property_name, _options = {})
  raise NotImplementedError
end

#to_xmlObject


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/nomen/nomenclature_set.rb', line 89

def to_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.nomenclatures(xmlns: Nomen::XMLNS, version: @version) do
      @nomenclatures.values.sort.each do |nomenclature|
        xml.nomenclature(nomenclature.to_xml_attrs) do
          if nomenclature.properties.any?
            xml.properties do
              nomenclature.properties.values.sort.each do |property|
                xml.property(property.to_xml_attrs)
              end
            end
          end
          if nomenclature.items.any?
            xml.items do
              nomenclature.items.values.sort_by(&:name).each do |item|
                xml.item(item.to_xml_attrs)
              end
            end
          end
        end
      end
    end
  end
  builder.to_xml
end