unitsml 0.6.11 → 0.6.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +5 -0
  3. data/Gemfile +0 -1
  4. data/docs/README.adoc +234 -1
  5. data/lib/unitsml/compose/builder.rb +347 -0
  6. data/lib/unitsml/compose/composable.rb +118 -0
  7. data/lib/unitsml/compose/composite.rb +145 -0
  8. data/lib/unitsml/compose/term_tree.rb +48 -0
  9. data/lib/unitsml/compose.rb +151 -0
  10. data/lib/unitsml/conversion/definition.rb +117 -0
  11. data/lib/unitsml/conversion.rb +332 -0
  12. data/lib/unitsml/dimension.rb +30 -3
  13. data/lib/unitsml/errors/base_error.rb +11 -0
  14. data/lib/unitsml/errors/empty_composition_error.rb +14 -0
  15. data/lib/unitsml/errors/invalid_power_error.rb +37 -0
  16. data/lib/unitsml/errors/invalid_unit_entry_error.rb +44 -0
  17. data/lib/unitsml/errors/misplaced_extender_error.rb +18 -0
  18. data/lib/unitsml/errors/mixed_terms_error.rb +15 -0
  19. data/lib/unitsml/errors/parse_error.rb +18 -0
  20. data/lib/unitsml/errors/quantity_errors.rb +1 -0
  21. data/lib/unitsml/errors/unknown_dimension_error.rb +18 -0
  22. data/lib/unitsml/errors/unknown_prefix_error.rb +15 -0
  23. data/lib/unitsml/errors/unknown_unit_error.rb +16 -0
  24. data/lib/unitsml/errors.rb +11 -0
  25. data/lib/unitsml/formula.rb +65 -4
  26. data/lib/unitsml/model/unit.rb +2 -0
  27. data/lib/unitsml/model/units/conversion_note.rb +19 -0
  28. data/lib/unitsml/model/units/conversions.rb +25 -0
  29. data/lib/unitsml/model/units/float64_conversion_from.rb +62 -0
  30. data/lib/unitsml/model/units/special_conversion_from.rb +27 -0
  31. data/lib/unitsml/model/units/wsdl_conversion_from.rb +26 -0
  32. data/lib/unitsml/model/units.rb +5 -0
  33. data/lib/unitsml/number.rb +5 -1
  34. data/lib/unitsml/opal/database_payload.rb +1 -1
  35. data/lib/unitsml/opal.rb +23 -0
  36. data/lib/unitsml/parg/tree_mapper.rb +31 -1
  37. data/lib/unitsml/parser.rb +12 -1
  38. data/lib/unitsml/power_numerator.rb +61 -0
  39. data/lib/unitsml/quantity.rb +122 -22
  40. data/lib/unitsml/unit.rb +60 -4
  41. data/lib/unitsml/unitsdb/quantities.rb +21 -0
  42. data/lib/unitsml/unitsdb/units.rb +6 -0
  43. data/lib/unitsml/utility.rb +61 -10
  44. data/lib/unitsml/version.rb +1 -1
  45. data/lib/unitsml.rb +11 -0
  46. metadata +24 -2
@@ -55,7 +55,15 @@ module Unitsml
55
55
  end
56
56
 
57
57
  def quantity_instance(id)
58
- Unitsdb.quantities.find_by_id(id)
58
+ # A quantity is resolved and silently dropped when unresolvable, so a
59
+ # pathological id (BasicObject, or a #to_s that raises/returns nil or a
60
+ # non-String) normalizes to nil here rather than leaking a raw
61
+ # exception through the render path.
62
+ string = Compose.safe_string(id)
63
+ return if string.nil? || string.strip.empty?
64
+
65
+ Unitsdb.quantities.find_by_id(string) ||
66
+ Unitsdb.quantities.find_by_name(string)
59
67
  end
60
68
 
61
69
  def units2dimensions(units)
@@ -116,11 +124,20 @@ module Unitsml
116
124
  unit_name = Unitsdb.units.find_by_id(k.unit_reference.id).symbols.first.id
117
125
  exponent = (k.power&.to_i || 1) * (u.power_numerator&.to_f || 1)
118
126
  object << { prefix: prefix,
119
- unit: Unit.new(unit_name, exponent, prefix: prefix) }
127
+ unit: dimension_base_unit(unit_name, exponent, prefix) }
120
128
  end
121
129
  end
122
130
  end
123
131
 
132
+ # A throwaway unit used only to compute the dimension vector. Its exponent
133
+ # is kept as the raw Numeric (bypassing Number coercion) so the vector
134
+ # still stringifies to a Unitsdb-matchable value; it is never rendered.
135
+ def dimension_base_unit(unit_name, exponent, prefix)
136
+ unit = Unit.new(unit_name, prefix: prefix)
137
+ unit.instance_variable_set(:@power_numerator, exponent)
138
+ unit
139
+ end
140
+
124
141
  def gather_units(units)
125
142
  units.sort_by { |a| a[:unit]&.unit_name }.each_with_object([]) do |k, m|
126
143
  if m.empty? || m[-1][:unit]&.unit_name != k[:unit]&.unit_name
@@ -368,14 +385,46 @@ module Unitsml
368
385
  xml.force_encoding("UTF-8")
369
386
  end
370
387
 
371
- def quantity(normtext, instance)
388
+ def quantity(normtext, instance, dims = nil)
372
389
  unit = unit_instance(normtext)
373
390
  return unless unit_or_quantity(unit, instance)
374
391
 
375
- model_quantity_xml(
376
- instance || unit.quantity_references&.first&.id,
377
- "##{unit_dimension_id(unit)}",
378
- )
392
+ record = instance && quantity_instance(instance)
393
+ # An explicit but unresolvable quantity emits nothing (silent), rather
394
+ # than leaking the raw reference as an xml:id.
395
+ return if instance && record.nil?
396
+
397
+ # unit is nil for a composite; fall back to the record's own identifier
398
+ # so a quantity without a NIST id still emits a usable xml:id.
399
+ id = canonical_nist_id(record) || quantity_reference_id(unit) ||
400
+ record_identifier_id(record)
401
+ model_quantity_xml(id, quantity_dimension_url(unit, dims),
402
+ record&.quantity_type)
403
+ end
404
+
405
+ # The Quantity's dimensionURL, or nil when the dimension id cannot be
406
+ # determined (e.g. a composite whose decomposition hits the UNKNOWN
407
+ # sentinel) — the attribute is then omitted rather than emitting a
408
+ # broken "#" pointer, keeping the rest of the Quantity data intact.
409
+ def quantity_dimension_url(unit, dims)
410
+ ref = unit ? unit_dimension_id(unit) : dim_id(dims)
411
+ "##{ref}" if ref
412
+ end
413
+
414
+ def canonical_nist_id(record)
415
+ record&.identifiers&.find { |identifier| identifier.type == "nist" }&.id
416
+ end
417
+
418
+ def quantity_reference_id(unit)
419
+ return unless unit
420
+
421
+ unit.quantity_references&.first&.id
422
+ end
423
+
424
+ def record_identifier_id(record)
425
+ return unless record
426
+
427
+ record.identifiers&.first&.id
379
428
  end
380
429
 
381
430
  def unit_nist_id(unit)
@@ -409,13 +458,15 @@ module Unitsml
409
458
  quantity_instance(quantity)
410
459
  end
411
460
 
412
- def model_quantity_xml(id, url)
413
- xml = Model::Quantity.new(
461
+ def model_quantity_xml(id, url, quantity_type = nil)
462
+ attrs = {
414
463
  id: id,
415
464
  name: quantity_name(id),
416
465
  dimension_url: url,
417
466
  lutaml_register: Configuration.context.id,
418
- ).to_xml
467
+ }
468
+ attrs[:quantity_type] = quantity_type if quantity_type
469
+ xml = Model::Quantity.new(**attrs).to_xml
419
470
  xml.force_encoding("UTF-8")
420
471
  end
421
472
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Unitsml
4
- VERSION = "0.6.11"
4
+ VERSION = "0.6.13"
5
5
  end
data/lib/unitsml.rb CHANGED
@@ -6,6 +6,7 @@ require "mml"
6
6
  module Unitsml
7
7
  module_function
8
8
 
9
+ autoload :Compose, "unitsml/compose"
9
10
  autoload :Dimension, "unitsml/dimension"
10
11
  autoload :Configuration, "unitsml/configuration"
11
12
  autoload :Errors, "unitsml/errors"
@@ -19,6 +20,7 @@ module Unitsml
19
20
  autoload :Number, "unitsml/number"
20
21
  autoload :Parg, "unitsml/parg"
21
22
  autoload :Parser, "unitsml/parser"
23
+ autoload :PowerNumerator, "unitsml/power_numerator"
22
24
  autoload :Prefix, "unitsml/prefix"
23
25
  autoload :PrefixAdapter, "unitsml/prefix_adapter"
24
26
  autoload :Sqrt, "unitsml/sqrt"
@@ -27,9 +29,18 @@ module Unitsml
27
29
  autoload :Utility, "unitsml/utility"
28
30
  autoload :VERSION, "unitsml/version"
29
31
  autoload :Quantity, "unitsml/quantity"
32
+ autoload :Conversion, "unitsml/conversion"
30
33
  autoload :Xml, "unitsml/xml"
31
34
 
32
35
  def parse(string)
33
36
  Parser.new(string).parse
34
37
  end
38
+
39
+ def compose(units: nil, dimensions: nil,
40
+ quantity: nil, name: nil, multiplier: nil)
41
+ Unitsml::Compose::Composite.new(
42
+ units: units, dimensions: dimensions,
43
+ quantity: quantity, name: name, multiplier: multiplier
44
+ ).to_formula
45
+ end
35
46
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unitsml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.11
4
+ version: 0.6.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-24 00:00:00.000000000 Z
11
+ date: 2026-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciiunits
@@ -107,15 +107,31 @@ files:
107
107
  - docs/README.adoc
108
108
  - docs/navigation.adoc
109
109
  - lib/unitsml.rb
110
+ - lib/unitsml/compose.rb
111
+ - lib/unitsml/compose/builder.rb
112
+ - lib/unitsml/compose/composable.rb
113
+ - lib/unitsml/compose/composite.rb
114
+ - lib/unitsml/compose/term_tree.rb
110
115
  - lib/unitsml/configuration.rb
116
+ - lib/unitsml/conversion.rb
117
+ - lib/unitsml/conversion/definition.rb
111
118
  - lib/unitsml/dimension.rb
112
119
  - lib/unitsml/errors.rb
113
120
  - lib/unitsml/errors/base_error.rb
121
+ - lib/unitsml/errors/empty_composition_error.rb
114
122
  - lib/unitsml/errors/invalid_model_error.rb
123
+ - lib/unitsml/errors/invalid_power_error.rb
124
+ - lib/unitsml/errors/invalid_unit_entry_error.rb
125
+ - lib/unitsml/errors/misplaced_extender_error.rb
126
+ - lib/unitsml/errors/mixed_terms_error.rb
115
127
  - lib/unitsml/errors/opal_payload_not_bundled_error.rb
116
128
  - lib/unitsml/errors/parg_load_error.rb
129
+ - lib/unitsml/errors/parse_error.rb
117
130
  - lib/unitsml/errors/plurimath_load_error.rb
118
131
  - lib/unitsml/errors/quantity_errors.rb
132
+ - lib/unitsml/errors/unknown_dimension_error.rb
133
+ - lib/unitsml/errors/unknown_prefix_error.rb
134
+ - lib/unitsml/errors/unknown_unit_error.rb
119
135
  - lib/unitsml/errors/unsupported_payload_type_error.rb
120
136
  - lib/unitsml/extender.rb
121
137
  - lib/unitsml/fenced.rb
@@ -143,11 +159,16 @@ files:
143
159
  - lib/unitsml/model/quantity.rb
144
160
  - lib/unitsml/model/unit.rb
145
161
  - lib/unitsml/model/units.rb
162
+ - lib/unitsml/model/units/conversion_note.rb
163
+ - lib/unitsml/model/units/conversions.rb
146
164
  - lib/unitsml/model/units/enumerated_root_unit.rb
165
+ - lib/unitsml/model/units/float64_conversion_from.rb
147
166
  - lib/unitsml/model/units/name.rb
148
167
  - lib/unitsml/model/units/root_units.rb
168
+ - lib/unitsml/model/units/special_conversion_from.rb
149
169
  - lib/unitsml/model/units/symbol.rb
150
170
  - lib/unitsml/model/units/system.rb
171
+ - lib/unitsml/model/units/wsdl_conversion_from.rb
151
172
  - lib/unitsml/namespace.rb
152
173
  - lib/unitsml/number.rb
153
174
  - lib/unitsml/opal.rb
@@ -156,6 +177,7 @@ files:
156
177
  - lib/unitsml/parg.rb
157
178
  - lib/unitsml/parg/tree_mapper.rb
158
179
  - lib/unitsml/parser.rb
180
+ - lib/unitsml/power_numerator.rb
159
181
  - lib/unitsml/prefix.rb
160
182
  - lib/unitsml/prefix_adapter.rb
161
183
  - lib/unitsml/quantity.rb