class Onoma::NomenclatureSet

This class represents a set of nomenclature like the reference DB

Attributes

version[RW]

Public Class Methods

load_file(file) click to toggle source
# 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
new() click to toggle source
# File lib/onoma/nomenclature_set.rb, line 6
def initialize
  @nomenclatures = {}.with_indifferent_access
  @version = 0
end

Public Instance Methods

[](nomenclature_name) click to toggle source

Find nomenclature

# File lib/onoma/nomenclature_set.rb, line 40
def [](nomenclature_name)
  @nomenclatures[nomenclature_name]
end
Also aliased as: find, nomenclature
add_item(nomenclature_name, item_name, options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/onoma/nomenclature_set.rb, line 66
def exist?(name)
  @nomenclatures[name].present?
end
find(nomenclature_name)
Alias for: []
find!(name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Find item

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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(nomenclature_name)
Alias for: []
nomenclature_names() click to toggle source
# File lib/onoma/nomenclature_set.rb, line 31
def nomenclature_names
  @nomenclatures.keys
end
nomenclatures() click to toggle source
# File lib/onoma/nomenclature_set.rb, line 35
def nomenclatures
  @nomenclatures.values
end
property(nomenclature_name, property_name) click to toggle source

Find property

# File lib/onoma/nomenclature_set.rb, line 53
def property(nomenclature_name, property_name)
  nomenclature = find!(nomenclature_name)
  nomenclature.property(property_name)
end
references() click to toggle source

Returns references between nomenclatures

# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# File lib/onoma/nomenclature_set.rb, line 159
def remove_property(_nomenclature_name, _property_name, _options = {})
  raise NotImplementedError
end
to_xml() click to toggle source
# 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