unitsml 0.6.11 → 0.6.12

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.
data/lib/unitsml/opal.rb CHANGED
@@ -94,6 +94,7 @@ require "unitsml/number"
94
94
  require "unitsml/parg"
95
95
  require "unitsml/parser"
96
96
  require "unitsml/quantity"
97
+ require "unitsml/conversion"
97
98
  require "unitsml/prefix"
98
99
  require "unitsml/prefix_adapter"
99
100
  require "unitsml/sqrt"
@@ -8,6 +8,16 @@ module Unitsml
8
8
  # (sorted), bottom-up, mirroring rule-for-rule the Transform rules in
9
9
  # unitsml/transform.rb.
10
10
  class TreeMapper
11
+ # Edition-2 artifacts (parsanol capture edition 2) name every
12
+ # uncaptured rule reference; these four are the names this grammar
13
+ # gains — the fence punctuation, the sign-suffix hyphen and the
14
+ # zero-width guard — none of which the v1 trees ever surfaced and
15
+ # none of which are authored capture names. Stripping them in the
16
+ # normalize pass keeps the dispatch table reading the same shapes
17
+ # for both editions, so this mapper works against artifacts baked
18
+ # before and after the asciiunits edition-2 flip.
19
+ STRUCTURAL_NAMES = %w[lparen rparen hyphen end_guard].freeze
20
+
11
21
  def apply(tree)
12
22
  map(normalize(tree))
13
23
  end
@@ -18,7 +28,9 @@ module Unitsml
18
28
  # Slice leaves; the mapper works on string keys and stringified leaves.
19
29
  def normalize(node)
20
30
  case node
21
- when Hash then node.to_h { |key, value| [key.to_s, normalize(value)] }
31
+ when Hash
32
+ node.to_h { |key, value| [key.to_s, normalize(value)] }
33
+ .except(*STRUCTURAL_NAMES)
22
34
  when Array then node.map { |value| normalize(value) }
23
35
  else node
24
36
  end
@@ -122,6 +134,24 @@ module Unitsml
122
134
  when %w[power_numerator prefix unit]
123
135
  Unit.new(text(node["unit"]), node["power_numerator"],
124
136
  prefix: Prefix.new(text(node["prefix"])))
137
+ when %w[prefix sequence unit]
138
+ Formula.new(
139
+ [Unit.new(text(node["unit"]),
140
+ prefix: Prefix.new(text(node["prefix"]))),
141
+ node["sequence"]],
142
+ )
143
+ when %w[sequence sqrt]
144
+ Formula.new(
145
+ [Sqrt.new(node["sqrt"]), node["sequence"]],
146
+ )
147
+ when %w[sequence unit]
148
+ Formula.new(
149
+ [Unit.new(text(node["unit"])), node["sequence"]],
150
+ )
151
+ when %w[implicit_extended sequence]
152
+ Formula.new(
153
+ [node["implicit_extended"], node["sequence"]],
154
+ )
125
155
  when %w[prefix] then Prefix.new(text(node["prefix"]))
126
156
  when %w[prefix unit]
127
157
  Unit.new(text(node["unit"]), prefix: Prefix.new(text(node["prefix"])))
@@ -25,10 +25,14 @@ module Unitsml
25
25
  end
26
26
  end
27
27
 
28
+ # rubocop:disable Metrics/AbcSize, Layout/LineLength
28
29
  # Base-dimension exponents summed across the composed expression —
29
30
  # V (L²MT⁻³I⁻¹) times A (I¹) merges to the watt vector L²MT⁻³.
30
31
  def dimension_vector
31
- vector = raw_dimension_list.each_with_object(Hash.new(0)) do |entry, acc|
32
+ list = raw_dimension_list
33
+ return nil if list.nil?
34
+
35
+ vector = list.each_with_object(Hash.new(0)) do |entry, acc|
32
36
  exponent = entry[:exponent]
33
37
  exponent = exponent.raw_value if exponent.respond_to?(:raw_value)
34
38
  acc[entry[:dimension]] += exponent.to_i
@@ -53,7 +57,7 @@ module Unitsml
53
57
  unit_expression: compose(self, other, "*"))
54
58
  end
55
59
 
56
- # rubocop:disable Metrics/AbcSize
60
+ # rubocop:disable-next Metrics/AbcSize
57
61
  def /(other)
58
62
  if other.is_a?(Numeric)
59
63
  gate_zero!(other)
@@ -70,7 +74,6 @@ module Unitsml
70
74
  Quantity.new(value: value / other.value,
71
75
  unit_expression: compose(self, other, "/"))
72
76
  end
73
- # rubocop:enable Metrics/AbcSize
74
77
 
75
78
  # R-A4: differences are legal on both ratio and interval scales;
76
79
  # only the dimension identity must match.
@@ -86,10 +89,32 @@ module Unitsml
86
89
  Quantity.new(value: value - other.value, unit_expression: norm_text)
87
90
  end
88
91
 
92
+ # R-A3: convert this quantity to another (dimension-compatible) unit
93
+ # using the unitsdb canonical_conversion data. Exact rational
94
+ # arithmetic throughout; the returned value is a Rational when a
95
+ # conversion was applied.
96
+ def convert_to(target_expression)
97
+ target_formula = if target_expression.is_a?(Formula)
98
+ target_expression
99
+ else
100
+ Unitsml.parse(target_expression)
101
+ end
102
+ gate_target = Quantity.new(value: 0,
103
+ unit_expression: target_formula.norm_text)
104
+ gate_dimension!(gate_target)
105
+
106
+ src = Conversion.to_canonical(formula)
107
+ dst = Conversion.to_canonical(target_formula)
108
+ converted = ((Rational(value) * src.factor) + src.offset - dst.offset) / dst.factor
109
+
110
+ Quantity.new(value: converted, unit_expression: target_formula.norm_text)
111
+ end
112
+
89
113
  def ==(other)
90
114
  other.is_a?(Quantity) && value == other.value &&
91
115
  dimension_vector == other.dimension_vector
92
116
  end
117
+ # rubocop:enable Metrics/AbcSize, Layout/LineLength
93
118
 
94
119
  private
95
120
 
@@ -108,12 +133,13 @@ module Unitsml
108
133
  def raw_dimension_list
109
134
  return [] unless formula
110
135
 
111
- Utility.units2dimensions(extract_units(formula.value))
136
+ Utility.units2dimensions(extract_units(formula.value)) ||
137
+ Conversion.dimension_entries(formula)
112
138
  end
113
139
 
114
140
  # Formula#units renders XML; the dimension identity needs the raw
115
141
  # decomposition, so extract it the same way Formula does internally.
116
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
142
+ # rubocop:disable-next Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
117
143
  def extract_units(formula)
118
144
  formula.each_with_object([]) do |term, units_arr|
119
145
  case term
@@ -132,7 +158,6 @@ module Unitsml
132
158
  end
133
159
  end
134
160
  end
135
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
136
161
 
137
162
  # R-A4: ratio-scale operations are specified errors on non-ratio
138
163
  # quantities. Differences and comparisons remain legal on both.
@@ -160,6 +185,9 @@ module Unitsml
160
185
  end
161
186
 
162
187
  def gate_dimension!(other)
188
+ # Unknown dimensions (units without a decomposition path) defer the
189
+ # legality decision to the conversion layer.
190
+ return if dimension_vector.nil? || other.dimension_vector.nil?
163
191
  return if dimension_vector == other.dimension_vector
164
192
 
165
193
  raise Errors::DimensionMismatchError,
@@ -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.12"
5
5
  end
data/lib/unitsml.rb CHANGED
@@ -27,6 +27,7 @@ module Unitsml
27
27
  autoload :Utility, "unitsml/utility"
28
28
  autoload :VERSION, "unitsml/version"
29
29
  autoload :Quantity, "unitsml/quantity"
30
+ autoload :Conversion, "unitsml/conversion"
30
31
  autoload :Xml, "unitsml/xml"
31
32
 
32
33
  def parse(string)
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.12
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-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciiunits
@@ -108,6 +108,7 @@ files:
108
108
  - docs/navigation.adoc
109
109
  - lib/unitsml.rb
110
110
  - lib/unitsml/configuration.rb
111
+ - lib/unitsml/conversion.rb
111
112
  - lib/unitsml/dimension.rb
112
113
  - lib/unitsml/errors.rb
113
114
  - lib/unitsml/errors/base_error.rb