Class: Onoma::NomenclatureSet

Inherits:
Object
  • Object
show all
Defined in:
lib/onoma/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/onoma/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/onoma/nomenclature_set.rb', line 4

def version
  @version
end

Class Method Details

.load_file(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/onoma/nomenclature_set.rb', line 17

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



40
41
42
# File 'lib/onoma/nomenclature_set.rb', line 40

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

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



163
164
165
166
167
# File 'lib/onoma/nomenclature_set.rb', line 163

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



118
119
120
121
122
123
# File 'lib/onoma/nomenclature_set.rb', line 118

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/onoma/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



169
170
171
172
173
# File 'lib/onoma/nomenclature_set.rb', line 169

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/onoma/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



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

def change_property(nomenclature_name, property_name, updates = {})
  nomenclature = find!(nomenclature_name)
  nomenclature.change_property(property_name, updates)
end

#each(&block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/onoma/nomenclature_set.rb', line 70

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

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/onoma/nomenclature_set.rb', line 66

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

#find!(name) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/onoma/nomenclature_set.rb', line 58

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

  nomenclature
end

#harvest_nomenclature(element) ⇒ Object



113
114
115
116
# File 'lib/onoma/nomenclature_set.rb', line 113

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

#item(nomenclature_name, item_name) ⇒ Object

Find item



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

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
# File 'lib/onoma/nomenclature_set.rb', line 11

def load_data_from_xml(nomenclature_name)
  element = Onoma.reference_document.xpath("/xmlns:nomenclatures/xmlns:nomenclature[@name='#{nomenclature_name}']")

  harvest_nomenclature(element)
end

#merge_item(nomenclature_name, item_name, into) ⇒ Object



175
176
177
178
# File 'lib/onoma/nomenclature_set.rb', line 175

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



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

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



31
32
33
# File 'lib/onoma/nomenclature_set.rb', line 31

def nomenclature_names
  @nomenclatures.keys
end

#nomenclaturesObject



35
36
37
# File 'lib/onoma/nomenclature_set.rb', line 35

def nomenclatures
  @nomenclatures.values
end

#property(nomenclature_name, property_name) ⇒ Object

Find property



53
54
55
56
# File 'lib/onoma/nomenclature_set.rb', line 53

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

#referencesObject

Returns references between nomenclatures



79
80
81
82
83
84
85
# File 'lib/onoma/nomenclature_set.rb', line 79

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

#remove_item(nomenclature_name, item_name) ⇒ Object



180
181
182
183
# File 'lib/onoma/nomenclature_set.rb', line 180

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/onoma/nomenclature_set.rb', line 144

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

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

Raises:

  • (NotImplementedError)


159
160
161
# File 'lib/onoma/nomenclature_set.rb', line 159

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

#to_xmlObject



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

def to_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.nomenclatures(xmlns: Onoma::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