Class: Onoma::Nomenclature
- Inherits:
-
Object
- Object
- Onoma::Nomenclature
- Defined in:
- lib/onoma/nomenclature.rb
Overview
This class represents a nomenclature
Instance Attribute Summary collapse
-
#forest_right ⇒ Object
Returns the value of attribute forest_right.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#name ⇒ Object
Returns the value of attribute name.
-
#notions ⇒ Object
Returns the value of attribute notions.
-
#properties ⇒ Object
(also: #property_natures)
readonly
Returns the value of attribute properties.
-
#translateable ⇒ Object
Returns the value of attribute translateable.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#[](item_name) ⇒ Object
Returns the given item.
-
#add_item(name, attributes = {}, options = {}) ⇒ Object
Add an item to the nomenclature.
-
#add_property(name, type, options = {}) ⇒ Object
Add an property to the nomenclature.
-
#best_match(property_name, searched_item) ⇒ Object
Returns the best match on nomenclature properties.
-
#cascade_item_renaming(name, new_name) ⇒ Object
name and new_name are Symbol.
- #cast_options(options) ⇒ Object
- #cast_property(name, value) ⇒ Object
-
#change_item(name, changes = {}) ⇒ Object
Add an item to the nomenclature.
- #check! ⇒ Object
-
#default(item_name = nil) ⇒ Object
Return the default item name.
- #degree_of_kinship(a, b) ⇒ Object
- #dependency_index ⇒ Object
-
#exists?(item) ⇒ Boolean
(also: #include?)
Returns
trueif an item exists in the nomenclature that matches the name, orfalseotherwise. -
#find(item_name) ⇒ Object
(also: #item)
Return the Item for the given name.
-
#find!(item_name) ⇒ Object
Return the Item for the given name.
- #find_by(properties) ⇒ Object
-
#find_each(&block) ⇒ Object
Iterates on items.
-
#first(item_name = nil) ⇒ Object
Return first item name.
-
#harvest_item(element, attributes = {}) ⇒ Object
Add an item to the nomenclature from an XML element.
-
#harvest_property(element) ⇒ Object
Add an property to the nomenclature from an XML element.
-
#human_name(options = {}) ⇒ Object
(also: #humanize)
Return human name.
-
#initialize(name, options = {}) ⇒ Nomenclature
constructor
Instanciate a new nomenclature.
- #inspect ⇒ Object
-
#list ⇒ Object
Returns list of items as an Array.
- #merge_item(name, into) ⇒ Object
-
#method_missing(method_name, *args) ⇒ Object
Returns property nature.
- #new_boundaries(count = 2) ⇒ Object
- #property(property_name) ⇒ Object
-
#rebuild_tree! ⇒ Object
Build a nested set index on items Returns last right value.
- #references ⇒ Object
- #remove_item(name) ⇒ Object
- #rename_item(name, new_name) ⇒ Object
- #roots ⇒ Object
- #select(&block) ⇒ Object
-
#select_without(already_imported) ⇒ Object
Returns a list for select, without specified items.
-
#selection(item_name = nil) ⇒ Object
Returns a list for select as an array of pair (array).
-
#selection_hash(item_name = nil) ⇒ Object
Returns a list for select as an array of pair (hash).
- #sibling(name) ⇒ Object
- #table_name ⇒ Object
-
#to_a(item_name = nil) ⇒ Object
(also: #all)
List all item names.
-
#to_csv(file, _options = {}) ⇒ Object
Write CSV file with all items an there properties.
- #to_xml_attrs ⇒ Object
- #translateable? ⇒ Boolean
-
#tree ⇒ Object
Returns hash with items in tree: => nil, b => {c => nil}.
- #update_attributes(attributes = {}) ⇒ Object
-
#where(properties) ⇒ Object
List items with properties filtering.
- #without(*names) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Nomenclature
Instanciate a new nomenclature
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/onoma/nomenclature.rb', line 12 def initialize(name, = {}) @name = name.to_sym @set = .delete(:set) @items = ActiveSupport::HashWithIndifferentAccess.new @forest_right = 0 @roots = [] @properties = ActiveSupport::HashWithIndifferentAccess.new @translateable = ![:translateable].is_a?(FalseClass) @notions = [:notions] || [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Returns property nature
488 489 490 |
# File 'lib/onoma/nomenclature.rb', line 488 def method_missing(method_name, *args) @properties[method_name] || super end |
Instance Attribute Details
#forest_right ⇒ Object
Returns the value of attribute forest_right.
8 9 10 |
# File 'lib/onoma/nomenclature.rb', line 8 def forest_right @forest_right end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
7 8 9 |
# File 'lib/onoma/nomenclature.rb', line 7 def items @items end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/onoma/nomenclature.rb', line 7 def name @name end |
#notions ⇒ Object
Returns the value of attribute notions.
8 9 10 |
# File 'lib/onoma/nomenclature.rb', line 8 def notions @notions end |
#properties ⇒ Object (readonly) Also known as: property_natures
Returns the value of attribute properties.
7 8 9 |
# File 'lib/onoma/nomenclature.rb', line 7 def properties @properties end |
#translateable ⇒ Object
Returns the value of attribute translateable.
8 9 10 |
# File 'lib/onoma/nomenclature.rb', line 8 def translateable @translateable end |
Class Method Details
.harvest(element, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/onoma/nomenclature.rb', line 24 def harvest(element, = {}) notions = element.attr('notions').to_s.split(/\s*\,\s*/).map(&:to_sym) [:notions] = notions if notions.any? [:translateable] = element.attr('translateable').to_s != 'false' name = element.attr('name').to_s nomenclature = new(name, ) element.xpath('xmlns:properties/xmlns:property').each do |property| nomenclature.harvest_property(property) end element.xpath('xmlns:items/xmlns:item').each do |item| nomenclature.harvest_item(item) end nomenclature.list.each(&:fetch_parent) nomenclature.rebuild_tree! nomenclature end |
Instance Method Details
#<=>(other) ⇒ Object
337 338 339 |
# File 'lib/onoma/nomenclature.rb', line 337 def <=>(other) name <=> other.name end |
#[](item_name) ⇒ Object
Returns the given item
319 320 321 |
# File 'lib/onoma/nomenclature.rb', line 319 def [](item_name) @items[item_name] end |
#add_item(name, attributes = {}, options = {}) ⇒ Object
Add an item to the nomenclature
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/onoma/nomenclature.rb', line 148 def add_item(name, attributes = {}, = {}) i = Item.new(self, name, attributes) if @items[i.name] raise "Item #{i.name} is already defined in nomenclature #{@name}" end @items[i.name] = i @roots << i unless i.parent? i.rebuild_tree! unless [:rebuild].is_a?(FalseClass) i end |
#add_property(name, type, options = {}) ⇒ Object
Add an property to the nomenclature
233 234 235 236 237 238 239 240 241 |
# File 'lib/onoma/nomenclature.rb', line 233 def add_property(name, type, = {}) p = Property.new(self, name, type, ) if @properties[p.name] raise "Property #{p.name} is already defined in nomenclature #{@name}" end @properties[p.name] = p @references = nil p end |
#best_match(property_name, searched_item) ⇒ Object
Returns the best match on nomenclature properties
475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/onoma/nomenclature.rb', line 475 def best_match(property_name, searched_item) items = [] begin list.select do |item| items << item if item.property(property_name) == searched_item.name end break if items.any? searched_item = searched_item.parent end while searched_item items end |
#cascade_item_renaming(name, new_name) ⇒ Object
name and new_name are Symbol
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/onoma/nomenclature.rb', line 194 def cascade_item_renaming(name, new_name) @set.references.each do |reference| next unless reference.foreign_nomenclature == self p = reference.property if p.list? reference.nomenclature.find_each do |item| v = item.property(p.name) if v && v.include?(name) l = v.map do |n| n == name ? new_name : n end item.set(p.name, l) end end else reference.nomenclature.find_each do |item| v = item.property(p.name) item.set(p.name, new_name) if v == name end end end end |
#cast_options(options) ⇒ Object
492 493 494 495 496 497 498 499 500 501 |
# File 'lib/onoma/nomenclature.rb', line 492 def () return {} if .nil? .each_with_object({}) do |(k, v), h| h[k.to_sym] = if properties[k] cast_property(k, v.to_s) else v end end end |
#cast_property(name, value) ⇒ Object
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/onoma/nomenclature.rb', line 503 def cast_property(name, value) value = value.to_s if property = properties[name] if property.type == :choice || property.type == :item if value =~ /\,/ raise InvalidProperty, 'A property nature of choice type cannot contain commas' end value = value.strip.to_sym elsif property.list? value = value.strip.split(/[[:space:]]*\,[[:space:]]*/).map(&:to_sym) elsif property.type == :boolean value = (value == 'true' ? true : value == 'false' ? false : nil) elsif property.type == :decimal value = value.to_d elsif property.type == :integer value = value.to_i elsif property.type == :date value = (value.blank? ? nil : value.to_date) elsif property.type == :symbol unless value =~ /\A\w+\z/ raise InvalidProperty, "A property '#{name}' must contains a symbol. /[a-z0-9_]/ accepted. No spaces. Got #{value.inspect}" end value = value.to_sym end elsif !%w[name parent aliases].include?(name.to_s) raise ArgumentError, "Undefined property '#{name}' in #{@name}" end value end |
#change_item(name, changes = {}) ⇒ Object
Add an item to the nomenclature
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/onoma/nomenclature.rb', line 160 def change_item(name, changes = {}) i = find!(name) has_parent = changes.key?(:parent) new_parent = changes[:parent] new_name = changes[:name] changes.each do |k, v| next if %i[parent name].include? k i.set(k, v) end if has_parent @roots << i if i.parent? && new_parent.nil? @roots.delete(i) if i.root? && new_parent i.parent = new_parent end i = rename_item(name, new_name) if new_name i end |
#check! ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/onoma/nomenclature.rb', line 247 def check! # Check properties @properties.values.each do |property| if property.choices_nomenclature && !property.inline_choices? && !Onoma[property.choices_nomenclature.to_s] raise InvalidProperty, "[#{name}] #{property.name} nomenclature property must refer to an existing nomenclature. Got #{property.choices_nomenclature.inspect}. Expecting: #{Onoma.names.inspect}" end next unless property.type == :choice && property.default unless property.choices.include?(property.default) raise InvalidProperty, "The default choice #{property.default.inspect} is invalid (in #{name}##{property.name}). Pick one from #{property.choices.sort.inspect}." end end # Check items list.each do |item| @properties.values.each do |property| choices = property.choices if item.property(property.name) && property.type == :choice # Cleans for parametric reference name = item.property(property.name).to_s.split(/\(/).first.to_sym unless choices.include?(name) raise InvalidProperty, "The given choice #{name.inspect} is invalid (in #{self.name}##{item.name}). Pick one from #{choices.sort.inspect}." end elsif item.property(property.name) && property.type == :list && property.choices_nomenclature for name in item.property(property.name) || [] # Cleans for parametric reference name = name.to_s.split(/\(/).first.to_sym unless choices.include?(name) raise InvalidProperty, "The given choice #{name.inspect} is invalid (in #{self.name}##{item.name}). Pick one from #{choices.sort.inspect}." end end end end end # Default return true end |
#default(item_name = nil) ⇒ Object
Return the default item name
394 395 396 |
# File 'lib/onoma/nomenclature.rb', line 394 def default(item_name = nil) first(item_name) end |
#degree_of_kinship(a, b) ⇒ Object
384 385 386 |
# File 'lib/onoma/nomenclature.rb', line 384 def degree_of_kinship(a, b) a.degree_of_kinship_with(b) end |
#dependency_index ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/onoma/nomenclature.rb', line 341 def dependency_index unless @dependency_index @dependency_index = 0 properties.each do |_n, p| if p.choices_nomenclature && !p.inline_choices? @dependency_index += 1 + Onoma[p.choices_nomenclature].dependency_index end end end @dependency_index end |
#exists?(item) ⇒ Boolean Also known as: include?
Returns true if an item exists in the nomenclature that matches the name, or false otherwise. The argument can take two forms:
* String/Symbol - Find an item with this primary name
* Nomen::Item - Find an item with the same name of the item
416 417 418 |
# File 'lib/onoma/nomenclature.rb', line 416 def exists?(item) @items[item.respond_to?(:name) ? item.name : item].present? end |
#find(item_name) ⇒ Object Also known as: item
Return the Item for the given name. Returns nil if no item found
399 400 401 |
# File 'lib/onoma/nomenclature.rb', line 399 def find(item_name) @items[item_name] end |
#find!(item_name) ⇒ Object
Return the Item for the given name. Raises Nomen::ItemNotFound if no item found in nomenclature
406 407 408 409 410 |
# File 'lib/onoma/nomenclature.rb', line 406 def find!(item_name) i = find(item_name) raise ItemNotFound, "Cannot find item #{item_name.inspect} in #{name}" unless i i end |
#find_by(properties) ⇒ Object
468 469 470 471 472 |
# File 'lib/onoma/nomenclature.rb', line 468 def find_by(properties) items = where(properties) return nil unless items.any? items.first end |
#find_each(&block) ⇒ Object
Iterates on items
431 432 433 |
# File 'lib/onoma/nomenclature.rb', line 431 def find_each(&block) list.each(&block) end |
#first(item_name = nil) ⇒ Object
Return first item name
389 390 391 |
# File 'lib/onoma/nomenclature.rb', line 389 def first(item_name = nil) all(item_name).first end |
#harvest_item(element, attributes = {}) ⇒ Object
Add an item to the nomenclature from an XML element
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/onoma/nomenclature.rb', line 96 def harvest_item(element, attributes = {}) name = element.attr('name').to_s parent = attributes[:parent] || (element.key?('parent') ? element['parent'] : nil) attributes = element.attributes.each_with_object(HashWithIndifferentAccess.new) do |(k, v), h| next if %w[name parent].include?(k) h[k] = cast_property(k, v.to_s) end attributes[:parent] = parent if parent item = add_item(name, attributes, rebuild: false) item end |
#harvest_property(element) ⇒ Object
Add an property to the nomenclature from an XML element
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/onoma/nomenclature.rb', line 109 def harvest_property(element) name = element.attr('name').to_sym type = element.attr('type').to_sym = {} if element.has_attribute?('fallbacks') [:fallbacks] = element.attr('fallbacks').to_s.strip.split(/[[:space:]]*\,[[:space:]]*/).map(&:to_sym) end if element.has_attribute?('default') [:default] = element.attr('default').to_sym end [:required] = !!(element.attr('required').to_s != 'true') # options[:inherit] = !!(element.attr('inherit').to_s == 'true') if type == :list type = element.has_attribute?('nomenclature') ? :item_list : :choice_list elsif type == :choice type = :item if element.has_attribute?('nomenclature') end if type == :choice || type == :choice_list if element.has_attribute?('choices') [:choices] = element.attr('choices').to_s.strip.split(/[[:space:]]*\,[[:space:]]*/).map(&:to_sym) else type = :string_list end elsif type == :item || type == :item_list if element.has_attribute?('choices') [:choices] = element.attr('choices').to_s.strip.to_sym elsif element.has_attribute?('nomenclature') [:choices] = element.attr('nomenclature').to_s.strip.to_sym else raise MissingChoices, "[#{@name}] Property #{name} must have nomenclature as choices" end end unless Property::TYPES.include?(type) raise ArgumentError, "Property #{name} type is unknown: #{type.inspect}" end add_property(name, type, ) end |
#human_name(options = {}) ⇒ Object Also known as: humanize
Return human name
303 304 305 306 |
# File 'lib/onoma/nomenclature.rb', line 303 def human_name( = {}) I18n.translate("nomenclatures.#{name}.name", .merge(default: ["labels.#{name}".to_sym, name.to_s.humanize])) end |
#inspect ⇒ Object
285 286 287 |
# File 'lib/onoma/nomenclature.rb', line 285 def inspect "Nomen::#{name.to_s.classify}" end |
#list ⇒ Object
Returns list of items as an Array
426 427 428 |
# File 'lib/onoma/nomenclature.rb', line 426 def list Onoma::Relation.new(self, @items.values) end |
#merge_item(name, into) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/onoma/nomenclature.rb', line 217 def merge_item(name, into) i = find!(name) dest = find!(into) i.children.each do |child| child.parent = dest end cascade_item_renaming(name.to_sym, into.to_sym) @items.delete(name) end |
#new_boundaries(count = 2) ⇒ Object
309 310 311 312 313 314 315 316 |
# File 'lib/onoma/nomenclature.rb', line 309 def new_boundaries(count = 2) boundaries = [] count.times do @forest_right += 1 boundaries << @forest_right end boundaries end |
#property(property_name) ⇒ Object
421 422 423 |
# File 'lib/onoma/nomenclature.rb', line 421 def property(property_name) @properties[property_name] end |
#rebuild_tree! ⇒ Object
Build a nested set index on items Returns last right value
90 91 92 93 |
# File 'lib/onoma/nomenclature.rb', line 90 def rebuild_tree! @forest_right = 0 roots.each(&:rebuild_tree!) end |
#references ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/onoma/nomenclature.rb', line 56 def references unless @references @references = [] properties.each do |_p, property| if property.item_reference? @references << Onoma::Reference.new(@set, property, @set.find(property.source), property.item_list? ? :array : :key) end end end @references end |
#remove_item(name) ⇒ Object
227 228 229 230 |
# File 'lib/onoma/nomenclature.rb', line 227 def remove_item(name) find!(name) @items.delete(name) end |
#rename_item(name, new_name) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/onoma/nomenclature.rb', line 178 def rename_item(name, new_name) if @items[new_name] raise "Item #{new_name} is already defined in nomenclature #{@name}. Use merging instead." end i = find!(name) i.children.each do |child| child.parent_name = new_name end cascade_item_renaming(name.to_sym, new_name.to_sym) i = @items.delete(i.name) i.name = new_name @items[new_name] = i i end |
#roots ⇒ Object
42 43 44 |
# File 'lib/onoma/nomenclature.rb', line 42 def roots @items.values.select(&:root?) end |
#select(&block) ⇒ Object
323 324 325 |
# File 'lib/onoma/nomenclature.rb', line 323 def select(&block) list.select(&block) end |
#select_without(already_imported) ⇒ Object
Returns a list for select, without specified items
372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/onoma/nomenclature.rb', line 372 def select_without(already_imported) ActiveSupport::Deprecation.warn 'Nomen::Nomenclature#select_without method is deprecated. Please use Nomen::Nomenclature#without method instead.' = @items.values.collect do |item| [item.human_name, item.name.to_s] unless already_imported[item.name.to_s] end .compact! .sort! do |a, b| a.first <=> b.first end end |
#selection(item_name = nil) ⇒ Object
Returns a list for select as an array of pair (array)
354 355 356 357 358 359 360 361 |
# File 'lib/onoma/nomenclature.rb', line 354 def selection(item_name = nil) items = (item_name ? find!(item_name).self_and_children : @items.values) items.collect do |item| [item.human_name, item.name.to_s] end.sort do |a, b| a.first.lower_ascii <=> b.first.lower_ascii end end |
#selection_hash(item_name = nil) ⇒ Object
Returns a list for select as an array of pair (hash)
364 365 366 367 368 369 |
# File 'lib/onoma/nomenclature.rb', line 364 def selection_hash(item_name = nil) items = (item_name ? find!(item_name).self_and_children : @items.values) items.collect do |item| { label: item.human_name, value: item.name } end.sort { |a, b| a[:label].lower_ascii <=> b[:label].lower_ascii } end |
#sibling(name) ⇒ Object
243 244 245 |
# File 'lib/onoma/nomenclature.rb', line 243 def sibling(name) @set.find(name) end |
#table_name ⇒ Object
289 290 291 |
# File 'lib/onoma/nomenclature.rb', line 289 def table_name @name.to_s.pluralize end |
#to_a(item_name = nil) ⇒ Object Also known as: all
List all item names. Can filter on a given item name and its children
328 329 330 331 332 333 334 |
# File 'lib/onoma/nomenclature.rb', line 328 def to_a(item_name = nil) if item_name.present? && @items[item_name] @items[item_name].self_and_children.map(&:name) else @items.keys.sort end end |
#to_csv(file, _options = {}) ⇒ Object
Write CSV file with all items an there properties
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/onoma/nomenclature.rb', line 69 def to_csv(file, = {}) properties = @properties.values CSV.open(file, 'wb') do |csv| csv << [:name] + properties.map do |p| suffix = (p.decimal? ? ' D' : p.integer? ? ' I' : p.boolean? ? ' B' : p.item? ? " R(#{p.choices_nomenclature})" : '') "#{p.name}#{suffix}" end @items.values.each do |i| csv << [i.name] + properties.map { |p| i.attributes[p.name] } end end end |
#to_xml_attrs ⇒ Object
82 83 84 85 86 |
# File 'lib/onoma/nomenclature.rb', line 82 def to_xml_attrs attrs = { name: name, translateable: translateable.to_s } attrs[:notions] = @notions.join(', ') if @notions.any? attrs end |
#translateable? ⇒ Boolean
298 299 300 |
# File 'lib/onoma/nomenclature.rb', line 298 def translateable? @translateable end |
#tree ⇒ Object
Returns hash with items in tree: => nil, b => {c => nil}
294 295 296 |
# File 'lib/onoma/nomenclature.rb', line 294 def tree @roots.collect(&:tree).join end |
#update_attributes(attributes = {}) ⇒ Object
50 51 52 53 54 |
# File 'lib/onoma/nomenclature.rb', line 50 def update_attributes(attributes = {}) attributes.each do |attribute, value| send("#{attribute}=", value) end end |
#where(properties) ⇒ Object
List items with properties filtering
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/onoma/nomenclature.rb', line 436 def where(properties) list.select do |item| valid = true properties.each do |name, value| item_value = item.property(name) if value.is_a?(Array) one_found = false value.each do |val| if val.is_a?(Onoma::Item) one_found = true if item_value == val.name.to_sym else one_found = true if item_value == val end end valid = false unless one_found elsif value.is_a?(Onoma::Item) valid = false unless item_value == value.name.to_sym else valid = false unless item_value == value end end valid end end |
#without(*names) ⇒ Object
461 462 463 464 465 466 |
# File 'lib/onoma/nomenclature.rb', line 461 def without(*names) excluded = names.flatten.compact.map(&:to_sym) list.reject do |item| excluded.include?(item.name) end end |