Module: Unitsdb::Commands::CheckSi::SiUpdater

Defined in:
lib/unitsdb/commands/check_si/si_updater.rb

Overview

Updater for SI references in YAML

Constant Summary collapse

SI_AUTHORITY =
"si-digital-framework"

Class Method Summary collapse

Class Method Details

.preserve_schema_header(original_file, yaml_content) ⇒ Object

Preserve existing schema header or add default one



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/unitsdb/commands/check_si/si_updater.rb', line 207

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

.resolve_yaml_file(output_file, entity_type) ⇒ Object

Resolve which YAML file to read from. Caller supplies output_file as the canonical path; if it doesn't exist yet we seed it with an empty {entity_type => []} template.



183
184
185
186
187
188
189
# File 'lib/unitsdb/commands/check_si/si_updater.rb', line 183

def resolve_yaml_file(output_file, entity_type)
  unless File.exist?(output_file)
    FileUtils.mkdir_p(File.dirname(output_file))
    File.write(output_file, { entity_type => [] }.to_yaml)
  end
  output_file
end

.update_db_references(entity_type, missing_refs, output_file, include_potential = false) ⇒ Object

Update references in YAML file (DB → TTL direction)



97
98
99
100
101
102
103
104
105
106
107
108
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/unitsdb/commands/check_si/si_updater.rb', line 97

def update_db_references(entity_type, missing_refs, output_file,
  include_potential = false)
  original_yaml_file = resolve_yaml_file(output_file, entity_type)

  # Load the original YAML file
  yaml_content = File.read(original_yaml_file)
  output_data = YAML.safe_load(yaml_content)

  # Group by entity ID to avoid duplicates
  missing_refs_by_id = {}

  missing_refs.each do |match|
    entity_id = match[:entity_id] || match[:db_entity].short
    ttl_entities = match[:ttl_entities]
    match_types = match[:match_types] || {}

    # Filter TTL entities based on include_potential parameter
    filtered_ttl_entities = ttl_entities.select do |ttl_entity|
      # Check if it's an exact match or if we're including potential matches
      match_type = match_types[ttl_entity[:uri]] || "Exact match" # Default to exact match
      match_pair_key = "#{entity_id}:#{ttl_entity[:uri]}"
      match_details = Unitsdb::Commands::CheckSi::SiMatcher.match_details&.dig(match_pair_key)

      if match_details && %w[symbol_match
                             partial_match].include?(match_details[:match_desc])
        include_potential
      else
        match_type == "Exact match" || include_potential
      end
    end

    # Skip if no entities after filtering
    next if filtered_ttl_entities.empty?

    missing_refs_by_id[entity_id] ||= []

    # Add filtered matching TTL entities for this DB entity
    filtered_ttl_entities.each do |ttl_entity|
      missing_refs_by_id[entity_id] << {
        uri: ttl_entity[:uri],
        type: "normative",
        authority: SI_AUTHORITY,
      }
    end
  end

  # Update the YAML content
  output_data[entity_type].each do |entity_yaml|
    # Find entity by ID or short
    entity_id = if entity_yaml["identifiers"]
                  begin
                    entity_yaml["identifiers"].first["id"]
                  rescue StandardError
                    nil
                  end
                elsif entity_yaml["id"]
                  entity_yaml["id"]
                end

    next unless entity_id && missing_refs_by_id.key?(entity_id)

    # Add references
    entity_yaml["references"] ||= []

    missing_refs_by_id[entity_id].each do |ref|
      # Check if this reference already exists
      next if entity_yaml["references"].any? do |existing_ref|
        existing_ref["uri"] == ref[:uri] &&
          existing_ref["authority"] == ref[:authority]
      end

      # Add the reference
      entity_yaml["references"] << {
        "uri" => ref[:uri],
        "type" => ref[:type],
        "authority" => ref[:authority],
      }
    end
  end

  write_yaml_file(output_file, output_data)
end

.update_references(entity_type, missing_matches, _db_entities, output_file, include_potential = false) ⇒ Object

Update references in YAML file (TTL → DB direction)



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/unitsdb/commands/check_si/si_updater.rb', line 16

def update_references(entity_type, missing_matches, _db_entities,
  output_file, include_potential = false)
  original_yaml_file = resolve_yaml_file(output_file, entity_type)

  # Load the original YAML file
  yaml_content = File.read(original_yaml_file)
  output_data = YAML.safe_load(yaml_content)

  # Group by entity ID to avoid duplicates
  grouped_matches = missing_matches.group_by do |match|
    match[:entity_id]
  end

  # Process each entity that needs updating
  grouped_matches.each do |entity_id, matches|
    # Filter matches based on include_potential parameter
    filtered_matches = matches.select do |match|
      # Check if it's an exact match or if we're including potential matches
      match_details = match[:match_details]
      if match_details&.dig(:exact) == false || %w[symbol_match
                                                   partial_match].include?(match_details&.dig(:match_desc) || "")
        include_potential
      else
        true
      end
    end

    # Skip if no matches after filtering
    next if filtered_matches.empty?

    # Find the entity in the array under the entity_type key
    entity_index = output_data[entity_type].find_index do |e|
      # Find entity with matching identifier
      e["identifiers"]&.any? { |id| id["id"] == entity_id }
    end

    next unless entity_index

    # Get the entity
    entity = output_data[entity_type][entity_index]

    # Initialize references array if it doesn't exist
    entity["references"] ||= []

    # Add new references
    filtered_matches.each do |match|
      # If this match has multiple SI references, add them all
      if match[:multiple_si]
        match[:multiple_si].each do |si_data|
          # Check if reference already exists
          next if entity["references"].any? do |ref|
            ref["uri"] == si_data[:uri] && ref["authority"] == SI_AUTHORITY
          end

          # Add new reference
          entity["references"] << {
            "uri" => si_data[:uri],
            "type" => "normative",
            "authority" => SI_AUTHORITY,
          }
        end
      else
        # Check if reference already exists
        next if entity["references"].any? do |ref|
          ref["uri"] == match[:si_uri] && ref["authority"] == SI_AUTHORITY
        end

        # Add new reference
        entity["references"] << {
          "uri" => match[:si_uri],
          "type" => "normative",
          "authority" => SI_AUTHORITY,
        }
      end
    end
  end

  write_yaml_file(output_file, output_data)
end

.write_yaml_file(output_file, output_data) ⇒ Object

Helper to write YAML file Ensure the output directory exists



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/unitsdb/commands/check_si/si_updater.rb', line 193

def write_yaml_file(output_file, output_data)
  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