Class: Onoma::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/onoma/database.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

#initialize(path) ⇒ Database



7
8
9
10
11
# File 'lib/onoma/database.rb', line 7

def initialize(path)
  @path = Pathname.new(path)
  @nomenclatures = ActiveSupport::HashWithIndifferentAccess.new
  @version = 0
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/onoma/database.rb', line 5

def path
  @path
end

#versionObject

Returns the value of attribute version.



4
5
6
# File 'lib/onoma/database.rb', line 4

def version
  @version
end

Class Method Details

.open(path) ⇒ Object



13
14
15
16
17
# File 'lib/onoma/database.rb', line 13

def self.open(path)
  db = new(path)
  db.send(:parse_file, path) if path.exist?
  db
end

Instance Method Details

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

Find nomenclature



36
37
38
# File 'lib/onoma/database.rb', line 36

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

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



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

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



133
134
135
136
137
# File 'lib/onoma/database.rb', line 133

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



162
163
164
165
# File 'lib/onoma/database.rb', line 162

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



181
182
183
184
185
# File 'lib/onoma/database.rb', line 181

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



148
149
150
151
152
153
154
155
# File 'lib/onoma/database.rb', line 148

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)


167
168
169
# File 'lib/onoma/database.rb', line 167

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

#copy(path) ⇒ Object



23
24
25
# File 'lib/onoma/database.rb', line 23

def copy(path)
  File.write(path, to_xml)
end

#each(&block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/onoma/database.rb', line 65

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

#exec_action(action) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/onoma/database.rb', line 108

def exec_action(action)
  case action.action_name.to_sym
  when :nomenclature_creation
    add_nomenclature(action.name, action.options)
  when :nomenclature_change
    change_nomenclature(action.nomenclature, action.changes)
  when :nomenclature_removal
    remove_nomenclature(action.nomenclature)
  when :property_creation
    add_property(action.nomenclature, action.name, action.type, action.options)
  when :property_change
    add_property(action.nomenclature, action.name, action.changes)
  when :item_creation
    add_item(action.nomenclature, action.name, action.options)
  when :item_change
    change_item(action.nomenclature, action.name, action.changes)
  when :item_merging
    merge_item(action.nomenclature, action.name, action.into)
  when :item_removal
    remove_item(action.nomenclature, action.name)
  else
    raise "Unknown action: #{action.action_name}"
  end
end

#exist?(name) ⇒ Boolean



61
62
63
# File 'lib/onoma/database.rb', line 61

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

#find!(name) ⇒ Object



54
55
56
57
58
59
# File 'lib/onoma/database.rb', line 54

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

#item(nomenclature_name, item_name) ⇒ Object

Find item



43
44
45
46
# File 'lib/onoma/database.rb', line 43

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

#merge_item(nomenclature_name, item_name, into) ⇒ Object



187
188
189
190
# File 'lib/onoma/database.rb', line 187

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



139
140
141
142
143
144
145
146
# File 'lib/onoma/database.rb', line 139

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



27
28
29
# File 'lib/onoma/database.rb', line 27

def nomenclature_names
  @nomenclatures.keys
end

#nomenclaturesObject



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

def nomenclatures
  @nomenclatures.values
end

#property(nomenclature_name, property_name) ⇒ Object

Find property



49
50
51
52
# File 'lib/onoma/database.rb', line 49

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

#referencesObject

Returns references between nomenclatures



74
75
76
77
78
79
80
# File 'lib/onoma/database.rb', line 74

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

#remove_item(nomenclature_name, item_name) ⇒ Object



192
193
194
195
# File 'lib/onoma/database.rb', line 192

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

#remove_nomenclature(nomenclature_name) ⇒ Object



157
158
159
160
# File 'lib/onoma/database.rb', line 157

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

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

Raises:

  • (NotImplementedError)


171
172
173
# File 'lib/onoma/database.rb', line 171

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

#to_xmlObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/onoma/database.rb', line 82

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

#writeObject



19
20
21
# File 'lib/onoma/database.rb', line 19

def write
  File.write(@path, to_xml)
end