Module: Unitsdb::Commands::Ucum::Updater
- Defined in:
- lib/unitsdb/commands/ucum/updater.rb
Overview
Updater for adding UCUM references to UnitsDB entities
Constant Summary collapse
- UCUM_AUTHORITY =
"ucum"
Class Method Summary collapse
-
.get_entity_id(entity) ⇒ Object
Get entity ID (either from identifiers array or directly).
-
.update_references(entity_type, matches, db_entities, output_file, include_potential = false) ⇒ Object
Update references in UnitsDB entities with UCUM references.
-
.write_yaml_file(output_file, output_data) ⇒ Object
Helper to write YAML file.
Class Method Details
.get_entity_id(entity) ⇒ Object
Get entity ID (either from identifiers array or directly)
88 89 90 91 92 93 94 |
# File 'lib/unitsdb/commands/ucum/updater.rb', line 88 def get_entity_id(entity) if entity.respond_to?(:identifiers) && entity.identifiers && !entity.identifiers.empty? entity.identifiers.first.id elsif entity.respond_to?(:id) entity.id end end |
.update_references(entity_type, matches, db_entities, output_file, include_potential = false) ⇒ Object
Update references in UnitsDB entities with UCUM references
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/unitsdb/commands/ucum/updater.rb', line 16 def update_references(entity_type, matches, db_entities, output_file, include_potential = false) puts "Updating UCUM references for #{entity_type}..." # Create a map of entity IDs to their UCUM references entity_references = {} # Process each match matches.each do |match| db_entity = match[:db_entity] ucum_entity = match[:ucum_entity] # Skip potential matches unless specified next if match[:potential] && !include_potential # Get entity ID entity_id = get_entity_id(db_entity) next unless entity_id # Initialize references for this entity entity_references[entity_id] = ExternalReference.new( uri: ucum_entity.identifier, type: "informative", authority: UCUM_AUTHORITY ) end # Update the YAML content db_entities.send(entity_type).each do |entity| # Find entity by ID entity_id = if entity.identifiers begin entity.identifiers.first.id rescue StandardError nil end end next unless entity_id && entity_references.key?(entity_id) # Initialize references array if it doesn't exist entity.references ||= [] # Add new references if (ext_ref = entity_references[entity_id]) if entity.references.detect { |ref| ref.uri == ext_ref.uri && ref. == ext_ref. } # Skip if reference already exists puts "Reference already exists for entity ID: #{entity_id}" else # Add the reference puts "Adding reference for entity ID: #{entity_id}, URI: #{ext_ref.uri}, Authority: #{ext_ref.authority}" entity.references << ext_ref end end end # Write to YAML file write_yaml_file(output_file, db_entities) puts "Added #{entity_references.values.flatten.size} UCUM references to #{entity_type}" end |
.write_yaml_file(output_file, output_data) ⇒ Object
Helper to write YAML file
78 79 80 81 82 83 84 85 |
# File 'lib/unitsdb/commands/ucum/updater.rb', line 78 def write_yaml_file(output_file, output_data) # Ensure the output directory exists output_dir = File.dirname(output_file) FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir) # Write to YAML file File.write(output_file, output_data.to_yaml) end |