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).
-
.preserve_schema_header(original_file, yaml_content) ⇒ Object
Preserve existing schema header or add default one.
-
.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)
122 123 124 125 126 |
# File 'lib/unitsdb/commands/ucum/updater.rb', line 122 def get_entity_id(entity) return entity.identifiers.first.id unless entity.identifiers.empty? entity.short end |
.preserve_schema_header(original_file, yaml_content) ⇒ Object
Preserve existing schema header or add default one
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/unitsdb/commands/ucum/updater.rb', line 96 def preserve_schema_header(original_file, yaml_content) schema_header = nil # Extract existing schema header if file exists if File.exist?(original_file) original_content = File.read(original_file) if (match = original_content.match(/^# yaml-language-server: \$schema=.+$/)) schema_header = match[0] end end # Remove any existing schema header from new content to avoid duplication yaml_content = yaml_content.gsub( /^# yaml-language-server: \$schema=.+$\n/, "" ) # Add preserved or default schema header if schema_header "#{schema_header}\n#{yaml_content}" else entity_type = File.basename(original_file, ".yaml") "# yaml-language-server: $schema=schemas/#{entity_type}-schema.yaml\n#{yaml_content}" 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 76 77 78 |
# 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 do |ref| ref.uri == ext_ref.uri && ref. == ext_ref. end # 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.}" 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
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/unitsdb/commands/ucum/updater.rb', line 81 def write_yaml_file(output_file, output_data) # Ensure the output directory exists output_dir = File.dirname(output_file) FileUtils.mkdir_p(output_dir) # Write to YAML file with proper formatting yaml_content = output_data.to_yaml # Preserve existing schema header or add default one yaml_content = preserve_schema_header(output_file, yaml_content) File.write(output_file, yaml_content) end |