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


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


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nomen/nomenclature_set.rb', line 11

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


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

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

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


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

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


107
108
109
110
111
# File 'lib/nomen/nomenclature_set.rb', line 107

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


136
137
138
139
# File 'lib/nomen/nomenclature_set.rb', line 136

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


155
156
157
158
159
# File 'lib/nomen/nomenclature_set.rb', line 155

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


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

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)

141
142
143
# File 'lib/nomen/nomenclature_set.rb', line 141

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

#each(&block) ⇒ Object


63
64
65
66
67
68
69
# File 'lib/nomen/nomenclature_set.rb', line 63

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

#exist?(name) ⇒ Boolean


59
60
61
# File 'lib/nomen/nomenclature_set.rb', line 59

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

#find!(name) ⇒ Object


52
53
54
55
56
57
# File 'lib/nomen/nomenclature_set.rb', line 52

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

#harvest_nomenclature(element) ⇒ Object


102
103
104
105
# File 'lib/nomen/nomenclature_set.rb', line 102

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

#item(nomenclature_name, item_name) ⇒ Object

Find item


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

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

#merge_item(nomenclature_name, item_name, into) ⇒ Object


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

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


113
114
115
116
117
118
119
120
# File 'lib/nomen/nomenclature_set.rb', line 113

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


25
26
27
# File 'lib/nomen/nomenclature_set.rb', line 25

def nomenclature_names
  @nomenclatures.keys
end

#nomenclaturesObject


29
30
31
# File 'lib/nomen/nomenclature_set.rb', line 29

def nomenclatures
  @nomenclatures.values
end

#property(nomenclature_name, property_name) ⇒ Object

Find property


47
48
49
50
# File 'lib/nomen/nomenclature_set.rb', line 47

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

#referencesObject

Returns references between nomenclatures


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

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

#remove_item(nomenclature_name, item_name) ⇒ Object


166
167
168
169
# File 'lib/nomen/nomenclature_set.rb', line 166

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

#remove_nomenclature(nomenclature_name) ⇒ Object


131
132
133
134
# File 'lib/nomen/nomenclature_set.rb', line 131

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

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

Raises:

  • (NotImplementedError)

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

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

#to_xmlObject


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nomen/nomenclature_set.rb', line 80

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
          xml.properties do
            nomenclature.properties.values.sort.each do |property|
              xml.property(property.to_xml_attrs)
            end
          end if nomenclature.properties.any?
          xml.items do
            nomenclature.items.values.sort { |a, b| a.name <=> b.name }.each do |item|
              xml.item(item.to_xml_attrs)
            end
          end if nomenclature.items.any?
        end
      end
    end
  end
  builder.to_xml
end