unitsml 0.6.2 → 0.6.4
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.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +23 -9
- data/Gemfile +5 -3
- data/docs/README.adoc +16 -2
- data/lib/unitsml/configuration.rb +47 -0
- data/lib/unitsml/dimension.rb +10 -4
- data/lib/unitsml/errors/opal_payload_not_bundled_error.rb +11 -0
- data/lib/unitsml/errors.rb +2 -0
- data/lib/unitsml/extender.rb +3 -1
- data/lib/unitsml/fenced.rb +6 -2
- data/lib/unitsml/formula.rb +14 -14
- data/lib/unitsml/mathml_helper.rb +60 -0
- data/lib/unitsml/number.rb +5 -3
- data/lib/unitsml/prefix.rb +3 -1
- data/lib/unitsml/unit.rb +10 -3
- data/lib/unitsml/unitsdb/database.rb +22 -0
- data/lib/unitsml/unitsdb/dimension.rb +23 -20
- data/lib/unitsml/unitsdb/{dimension_quantity.rb → dimension_details.rb} +3 -1
- data/lib/unitsml/unitsdb/dimensions.rb +2 -0
- data/lib/unitsml/unitsdb/prefix_reference.rb +3 -1
- data/lib/unitsml/unitsdb/prefixes.rb +2 -0
- data/lib/unitsml/unitsdb/quantities.rb +2 -0
- data/lib/unitsml/unitsdb/unit.rb +2 -8
- data/lib/unitsml/unitsdb/units.rb +2 -4
- data/lib/unitsml/unitsdb.rb +82 -14
- data/lib/unitsml/utility.rb +160 -35
- data/lib/unitsml/version.rb +1 -1
- data/lib/unitsml.rb +3 -44
- data/unitsml.gemspec +3 -3
- metadata +13 -11
- data/lib/unitsml/unitsdb/si_derived_base.rb +0 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 70a152434290d3e4f3274a3b5beed15db5d0e6184c93b629363311b8dcc39579
|
|
4
|
+
data.tar.gz: d70bd8514fafbfec08624c17f11440aa4277f57c0d6d045a39c8611f70952c56
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b18a3a711ca1f5fb60a59283ec4d62d976cd0e0ad30fd199ca0c17327fdad398b1b345d76f8e08d57bdee4f2ea2d9ab36accf9016e647214b640a564f938f0a8
|
|
7
|
+
data.tar.gz: 6e8c2bcb409905222668fdf87e2470df42d3fde887dcf065d085111b588995529648fd296ef0b2da9040fc040bc2ffb8045f1e940d1dba5d1e99ac8ff156ab8b
|
data/.rubocop_todo.yml
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
# This configuration was generated by
|
|
2
2
|
# `rubocop --auto-gen-config`
|
|
3
|
-
# on 2026-04-
|
|
3
|
+
# on 2026-04-15 09:29:09 UTC using RuboCop version 1.86.1.
|
|
4
4
|
# The point is for the user to remove these configuration records
|
|
5
5
|
# one by one as the offenses are removed from the code base.
|
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
|
8
8
|
|
|
9
|
-
# Offense count: 2
|
|
10
|
-
# This cop supports safe autocorrection (--autocorrect).
|
|
11
|
-
# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation.
|
|
12
|
-
Bundler/OrderedGems:
|
|
13
|
-
Exclude:
|
|
14
|
-
- 'Gemfile'
|
|
15
|
-
|
|
16
9
|
# Offense count: 1
|
|
17
10
|
Gemspec/RequiredRubyVersion:
|
|
18
11
|
Exclude:
|
|
19
12
|
- 'unitsml.gemspec'
|
|
20
13
|
|
|
21
|
-
# Offense count:
|
|
14
|
+
# Offense count: 1
|
|
15
|
+
# This cop supports safe autocorrection (--autocorrect).
|
|
16
|
+
# Configuration parameters: EnforcedStyle, IndentationWidth.
|
|
17
|
+
# SupportedStyles: with_first_argument, with_fixed_indentation
|
|
18
|
+
Layout/ArgumentAlignment:
|
|
19
|
+
Exclude:
|
|
20
|
+
- 'lib/unitsml/unit.rb'
|
|
21
|
+
|
|
22
|
+
# Offense count: 76
|
|
22
23
|
# This cop supports safe autocorrection (--autocorrect).
|
|
23
24
|
# Configuration parameters: Max, AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings.
|
|
24
25
|
# URISchemes: http, https
|
|
@@ -40,6 +41,13 @@ Layout/LineLength:
|
|
|
40
41
|
- 'spec/unitsml/conv/xml_spec.rb'
|
|
41
42
|
- 'unitsml.gemspec'
|
|
42
43
|
|
|
44
|
+
# Offense count: 1
|
|
45
|
+
# This cop supports safe autocorrection (--autocorrect).
|
|
46
|
+
# Configuration parameters: AllowInHeredoc.
|
|
47
|
+
Layout/TrailingWhitespace:
|
|
48
|
+
Exclude:
|
|
49
|
+
- 'lib/unitsml/unit.rb'
|
|
50
|
+
|
|
43
51
|
# Offense count: 3
|
|
44
52
|
Lint/ReturnInVoidContext:
|
|
45
53
|
Exclude:
|
|
@@ -160,6 +168,12 @@ RSpec/SpecFilePathFormat:
|
|
|
160
168
|
- 'spec/unitsml/conv/unicode_spec.rb'
|
|
161
169
|
- 'spec/unitsml/conv/xml_spec.rb'
|
|
162
170
|
|
|
171
|
+
# Offense count: 1
|
|
172
|
+
# This cop supports safe autocorrection (--autocorrect).
|
|
173
|
+
Style/MultilineIfModifier:
|
|
174
|
+
Exclude:
|
|
175
|
+
- 'lib/unitsml/unit.rb'
|
|
176
|
+
|
|
163
177
|
# Offense count: 2
|
|
164
178
|
# Configuration parameters: AllowedMethods.
|
|
165
179
|
# AllowedMethods: respond_to_missing?
|
data/Gemfile
CHANGED
|
@@ -6,11 +6,13 @@ source "https://rubygems.org"
|
|
|
6
6
|
gemspec
|
|
7
7
|
|
|
8
8
|
gem "canon"
|
|
9
|
-
gem "lutaml-model",
|
|
10
|
-
|
|
9
|
+
gem "lutaml-model",
|
|
10
|
+
github: "lutaml/lutaml-model",
|
|
11
|
+
branch: "fix/global-context-register-lookup-fallback"
|
|
11
12
|
gem "oga"
|
|
12
13
|
gem "ox"
|
|
13
|
-
gem "plurimath", github: "plurimath/plurimath",
|
|
14
|
+
gem "plurimath", github: "plurimath/plurimath",
|
|
15
|
+
branch: "rt-lutaml-080"
|
|
14
16
|
gem "pry"
|
|
15
17
|
gem "rake"
|
|
16
18
|
gem "rspec"
|
data/docs/README.adoc
CHANGED
|
@@ -40,8 +40,22 @@ The UnitsML Ruby library consists of several key components:
|
|
|
40
40
|
* Prefix: Represents unit prefixes (k, m, μ, etc.)
|
|
41
41
|
* Dimension: Represents physical dimensions
|
|
42
42
|
|
|
43
|
-
The library
|
|
44
|
-
|
|
43
|
+
The library uses the https://github.com/unitsml/unitsdb-ruby[unitsdb-ruby]
|
|
44
|
+
gem as the primary runtime source for standard units, prefixes, and dimensions.
|
|
45
|
+
|
|
46
|
+
== Development setup
|
|
47
|
+
|
|
48
|
+
The development `Gemfile` pins the current integration branches for
|
|
49
|
+
`lutaml-model`, `mml`, `plurimath`, and `unitsdb-ruby`. If you need to test
|
|
50
|
+
coordinated local changes across those repositories, temporarily switch those
|
|
51
|
+
dependencies to Bundler `path` entries that point at sibling checkouts, for
|
|
52
|
+
example `../lutaml-model` or `../unitsdb-ruby`. Keep those local path changes
|
|
53
|
+
out of commits unless the branch intentionally requires them.
|
|
54
|
+
|
|
55
|
+
The standard Ruby runtime path goes through `::Unitsdb.database`. When
|
|
56
|
+
`unitsdb-ruby` cannot load its bundled `data/` directory, UnitsML falls back to
|
|
57
|
+
the packaged YAML files in the `unitsdb-ruby` gem's `vendor/unitsdb`
|
|
58
|
+
directory, rather than a `vendor/unitsdb` directory in this repository.
|
|
45
59
|
|
|
46
60
|
|
|
47
61
|
== Usage
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "unitsdb"
|
|
4
|
+
|
|
5
|
+
module Unitsml
|
|
6
|
+
module Configuration
|
|
7
|
+
CONTEXT_ID = :unitsml_ruby
|
|
8
|
+
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def context_id
|
|
12
|
+
CONTEXT_ID
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def context(force_populate: false)
|
|
16
|
+
existing = ::Unitsdb::Config.find_context(context_id)
|
|
17
|
+
return existing if existing && !force_populate
|
|
18
|
+
|
|
19
|
+
build_context
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def register_model(klass, id:)
|
|
23
|
+
registered_models[id.to_sym] = klass
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def registered_models
|
|
27
|
+
@registered_models ||= {}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def build_context
|
|
31
|
+
::Unitsdb::Config.context # ensure unitsdb context exists
|
|
32
|
+
|
|
33
|
+
substitutions = registered_models.each_value.filter_map do |klass|
|
|
34
|
+
parent = klass.superclass
|
|
35
|
+
next if parent == Object
|
|
36
|
+
|
|
37
|
+
{ from_type: parent, to_type: klass }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
::Unitsdb::Config.populate_context(
|
|
41
|
+
id: context_id,
|
|
42
|
+
fallback_to: [::Unitsdb::Config.context_id],
|
|
43
|
+
substitutions: substitutions,
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/unitsml/dimension.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Dimension
|
|
5
|
+
include MathmlHelper
|
|
6
|
+
|
|
5
7
|
attr_accessor :dimension_name, :power_numerator
|
|
6
8
|
|
|
7
9
|
def initialize(dimension_name, power_numerator = nil)
|
|
@@ -26,7 +28,7 @@ module Unitsml
|
|
|
26
28
|
def to_mathml(options)
|
|
27
29
|
# MathML key's value in unitsdb/dimensions.yaml
|
|
28
30
|
# file includes mi tags only.
|
|
29
|
-
value =
|
|
31
|
+
value = mml_v4_from_xml(:mi, dim_symbols.mathml)
|
|
30
32
|
method_name = if power_numerator
|
|
31
33
|
value = msup_tag(value, options)
|
|
32
34
|
:msup
|
|
@@ -63,7 +65,10 @@ module Unitsml
|
|
|
63
65
|
symbol: dim_instance.processed_symbol,
|
|
64
66
|
power_numerator: power_numerator&.raw_value || 1,
|
|
65
67
|
}
|
|
66
|
-
Model::DimensionQuantities.const_get(modelize(element_name)).new(
|
|
68
|
+
Model::DimensionQuantities.const_get(modelize(element_name)).new(
|
|
69
|
+
**attributes,
|
|
70
|
+
lutaml_register: Configuration.context.id,
|
|
71
|
+
)
|
|
67
72
|
end
|
|
68
73
|
|
|
69
74
|
def xml_instances_hash(options)
|
|
@@ -94,8 +99,9 @@ module Unitsml
|
|
|
94
99
|
|
|
95
100
|
def msup_tag(value, options)
|
|
96
101
|
mathml = power_numerator.to_mathml(options)
|
|
97
|
-
msup =
|
|
98
|
-
|
|
102
|
+
msup = mml_v4_new(
|
|
103
|
+
:msup,
|
|
104
|
+
mrow_value: [mml_v4_new(:mrow, mi_value: [value])],
|
|
99
105
|
)
|
|
100
106
|
[mathml].flatten.each do |record|
|
|
101
107
|
record_values = msup.public_send("#{record[:method_name]}_value") || []
|
data/lib/unitsml/errors.rb
CHANGED
data/lib/unitsml/extender.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Extender
|
|
5
|
+
include MathmlHelper
|
|
6
|
+
|
|
5
7
|
attr_accessor :symbol
|
|
6
8
|
|
|
7
9
|
def initialize(symbol)
|
|
@@ -18,7 +20,7 @@ module Unitsml
|
|
|
18
20
|
extender = multiplier(options[:multiplier] || "⋅", unicode: true)
|
|
19
21
|
{
|
|
20
22
|
method_name: :mo,
|
|
21
|
-
value:
|
|
23
|
+
value: mml_v4_new(:mo, value: extender, rspace: rspace),
|
|
22
24
|
}
|
|
23
25
|
end
|
|
24
26
|
|
data/lib/unitsml/fenced.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Fenced
|
|
5
5
|
include FencedNumeric
|
|
6
|
+
include MathmlHelper
|
|
6
7
|
|
|
7
8
|
attr_reader :open_paren, :value, :close_paren
|
|
8
9
|
|
|
@@ -31,11 +32,14 @@ module Unitsml
|
|
|
31
32
|
mathml = value.to_mathml(options)
|
|
32
33
|
return mathml unless options[:explicit_parenthesis]
|
|
33
34
|
|
|
34
|
-
fenced =
|
|
35
|
+
fenced = mml_v4_new(
|
|
36
|
+
:mrow,
|
|
37
|
+
mo_value: [mml_v4_new(:mo, value: open_paren)],
|
|
38
|
+
)
|
|
35
39
|
fenced.ordered = true
|
|
36
40
|
fenced.element_order ||= [xml_order_element("mo")]
|
|
37
41
|
[mathml].flatten.each { |record| add_math_element(fenced, record) }
|
|
38
|
-
fenced.mo_value <<
|
|
42
|
+
fenced.mo_value << mml_v4_new(:mo, value: close_paren)
|
|
39
43
|
fenced.element_order << xml_order_element("mo")
|
|
40
44
|
{ method_name: :mrow, value: fenced }
|
|
41
45
|
end
|
data/lib/unitsml/formula.rb
CHANGED
|
@@ -5,6 +5,8 @@ require "htmlentities"
|
|
|
5
5
|
|
|
6
6
|
module Unitsml
|
|
7
7
|
class Formula
|
|
8
|
+
include MathmlHelper
|
|
9
|
+
|
|
8
10
|
attr_accessor :value, :explicit_value, :root
|
|
9
11
|
|
|
10
12
|
def initialize(value = [],
|
|
@@ -29,15 +31,14 @@ module Unitsml
|
|
|
29
31
|
def to_mathml(options = {})
|
|
30
32
|
if root
|
|
31
33
|
options = update_options(options)
|
|
32
|
-
|
|
33
|
-
math = ::Mml::V4::Math.new(display: "block")
|
|
34
|
+
math = mml_v4_new(:math, display: "block")
|
|
34
35
|
math.ordered = true
|
|
35
36
|
math.element_order ||= []
|
|
36
37
|
value.each do |instance|
|
|
37
38
|
process_value(math, instance.to_mathml(options))
|
|
38
39
|
end
|
|
39
|
-
generated_math = math.to_xml
|
|
40
|
-
|
|
40
|
+
generated_math = math.to_xml(register: mml_v4_context.id)
|
|
41
|
+
.gsub(%r{&(.*?)(?=</)}, '&\1')
|
|
41
42
|
|
|
42
43
|
generated_math.force_encoding("UTF-8")
|
|
43
44
|
else
|
|
@@ -80,7 +81,7 @@ module Unitsml
|
|
|
80
81
|
:asciimath)
|
|
81
82
|
end
|
|
82
83
|
|
|
83
|
-
Plurimath::Math.parse(to_mathml(options), :mathml)
|
|
84
|
+
Plurimath::Math.parse(compact_mathml_for_plurimath(to_mathml(options)), :mathml)
|
|
84
85
|
end
|
|
85
86
|
|
|
86
87
|
def dimensions_extraction
|
|
@@ -151,7 +152,10 @@ module Unitsml
|
|
|
151
152
|
dim_id = dims.map(&:generate_id).join
|
|
152
153
|
attributes = { id: "D_#{dim_id}" }
|
|
153
154
|
dims.each { |dim| attributes.merge!(dim.xml_instances_hash(options)) }
|
|
154
|
-
Model::Dimension.new(
|
|
155
|
+
Model::Dimension.new(
|
|
156
|
+
**attributes,
|
|
157
|
+
lutaml_register: Configuration.context.id,
|
|
158
|
+
).to_xml.force_encoding("UTF-8")
|
|
155
159
|
end
|
|
156
160
|
|
|
157
161
|
def sort_dims(values)
|
|
@@ -195,14 +199,6 @@ module Unitsml
|
|
|
195
199
|
Plurimath.const_defined?(:Mathml)
|
|
196
200
|
end
|
|
197
201
|
|
|
198
|
-
def nullify_mml_models
|
|
199
|
-
Plurimath::Mathml::Parser::CONFIGURATION.each_key { |klass| klass.model(klass) }
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
def reset_mml_models
|
|
203
|
-
::Mml::V4::Configuration.custom_models = Plurimath::Mathml::Parser::CONFIGURATION
|
|
204
|
-
end
|
|
205
|
-
|
|
206
202
|
def process_value(math, mathml_instances)
|
|
207
203
|
case mathml_instances
|
|
208
204
|
when Array
|
|
@@ -220,5 +216,9 @@ module Unitsml
|
|
|
220
216
|
options.merge(multiplier: multiplier,
|
|
221
217
|
explicit_parenthesis: explicit_parenthesis).compact
|
|
222
218
|
end
|
|
219
|
+
|
|
220
|
+
def compact_mathml_for_plurimath(mathml)
|
|
221
|
+
mathml.gsub(/>\s+</, "><").strip
|
|
222
|
+
end
|
|
223
223
|
end
|
|
224
224
|
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module MathmlHelper
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def mml_v4_context
|
|
8
|
+
::Mml::V4::Configuration.context
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def mml_v4_from_xml(klass_ref, xml)
|
|
12
|
+
mml_v4_class_for(klass_ref).from_xml(xml, register: mml_v4_context.id)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def mml_v4_new(klass_ref, **attributes)
|
|
16
|
+
klass = mml_v4_class_for(klass_ref)
|
|
17
|
+
coerce_mml_v4_collection_attributes!(klass, attributes)
|
|
18
|
+
klass.new(**attributes, lutaml_register: mml_v4_context.id)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def mml_v4_with_content(instance, content)
|
|
22
|
+
attributes = instance.to_hash.transform_keys(&:to_sym)
|
|
23
|
+
mml_v4_new(
|
|
24
|
+
instance.class,
|
|
25
|
+
**attributes,
|
|
26
|
+
mml_v4_content_attribute(instance) => content,
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def mml_v4_class_for(klass_ref)
|
|
31
|
+
return klass_ref if klass_ref.is_a?(Class)
|
|
32
|
+
|
|
33
|
+
mml_v4_context.lookup_local(klass_ref.to_sym)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def coerce_mml_v4_collection_attributes!(klass, attributes)
|
|
39
|
+
return unless klass.respond_to?(:attributes)
|
|
40
|
+
|
|
41
|
+
attributes.each do |name, value|
|
|
42
|
+
attribute = klass.attributes[name]
|
|
43
|
+
attributes[name] = coerce_mml_v4_collection_value(attribute, value)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def coerce_mml_v4_collection_value(attribute, value)
|
|
48
|
+
return value unless attribute&.collection?
|
|
49
|
+
return value if value.nil?
|
|
50
|
+
return value if attribute.collection_instance?(value)
|
|
51
|
+
|
|
52
|
+
attribute.build_collection(value)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def mml_v4_content_attribute(instance)
|
|
56
|
+
register = instance.lutaml_register || mml_v4_context.id
|
|
57
|
+
instance.class.mappings_for(:xml, register).content_mapping.to
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
data/lib/unitsml/number.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Number
|
|
5
5
|
include FencedNumeric
|
|
6
|
+
include MathmlHelper
|
|
6
7
|
|
|
7
8
|
attr_accessor :value
|
|
8
9
|
alias raw_value value
|
|
@@ -19,7 +20,7 @@ module Unitsml
|
|
|
19
20
|
def to_mathml(_options)
|
|
20
21
|
matched_value = value&.match(/-?(.+)/)
|
|
21
22
|
mn_value = matched_value ? matched_value[1] : value
|
|
22
|
-
mn_tag =
|
|
23
|
+
mn_tag = mml_v4_new(:mn, value: mn_value)
|
|
23
24
|
value.start_with?("-") ? mrow_hash(mn_tag) : mn_hash(mn_tag)
|
|
24
25
|
end
|
|
25
26
|
|
|
@@ -62,8 +63,9 @@ module Unitsml
|
|
|
62
63
|
def mrow_hash(mn_tag)
|
|
63
64
|
{
|
|
64
65
|
method_name: :mrow,
|
|
65
|
-
value:
|
|
66
|
-
|
|
66
|
+
value: mml_v4_new(
|
|
67
|
+
:mrow,
|
|
68
|
+
mo_value: [mml_v4_new(:mo, value: "−")],
|
|
67
69
|
mn_value: [mn_tag],
|
|
68
70
|
),
|
|
69
71
|
}
|
data/lib/unitsml/prefix.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Prefix
|
|
5
|
+
include MathmlHelper
|
|
6
|
+
|
|
5
7
|
attr_accessor :prefix_name, :only_instance
|
|
6
8
|
|
|
7
9
|
def initialize(prefix_name, only_instance = false)
|
|
@@ -41,7 +43,7 @@ module Unitsml
|
|
|
41
43
|
)
|
|
42
44
|
return symbol unless only_instance
|
|
43
45
|
|
|
44
|
-
{ method_name: :mi, value:
|
|
46
|
+
{ method_name: :mi, value: mml_v4_new(:mi, value: symbol) }
|
|
45
47
|
end
|
|
46
48
|
|
|
47
49
|
def to_latex(_)
|
data/lib/unitsml/unit.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Unit
|
|
5
|
+
include MathmlHelper
|
|
6
|
+
|
|
5
7
|
attr_accessor :unit_name, :power_numerator, :prefix
|
|
6
8
|
|
|
7
9
|
SI_UNIT_SYSTEM = %w[si_base si_derived_special
|
|
@@ -37,8 +39,13 @@ module Unitsml
|
|
|
37
39
|
def to_mathml(options)
|
|
38
40
|
value = unit_symbols&.mathml
|
|
39
41
|
tag_name = value.match(/^<(?<tag>\w+)/)[:tag]
|
|
40
|
-
value =
|
|
41
|
-
|
|
42
|
+
value = mml_v4_from_xml(tag_name, value)
|
|
43
|
+
if prefix
|
|
44
|
+
value = mml_v4_with_content(
|
|
45
|
+
value,
|
|
46
|
+
"#{prefix.to_mathml(options.merge(parent: value))}#{Array(value.value).join}",
|
|
47
|
+
)
|
|
48
|
+
end
|
|
42
49
|
if power_numerator
|
|
43
50
|
value = msup_tag(
|
|
44
51
|
{ method_name: tag_name, value: value },
|
|
@@ -127,7 +134,7 @@ module Unitsml
|
|
|
127
134
|
end
|
|
128
135
|
|
|
129
136
|
def msup_tag(value, options)
|
|
130
|
-
msup =
|
|
137
|
+
msup = mml_v4_new(:msup)
|
|
131
138
|
msup.ordered = true
|
|
132
139
|
msup.element_order = []
|
|
133
140
|
[value, power_numerator.to_mathml(options)].flatten.each do |record|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Unitsdb
|
|
5
|
+
class Database < ::Unitsdb::Database
|
|
6
|
+
DATABASE = nil
|
|
7
|
+
|
|
8
|
+
def self.from_db(dir_path, context: Unitsml::Configuration.context.id)
|
|
9
|
+
return super unless RUBY_ENGINE == "opal"
|
|
10
|
+
|
|
11
|
+
context_id = context.to_sym
|
|
12
|
+
raise Unitsml::Errors::OpalPayloadNotBundledError unless DATABASE
|
|
13
|
+
|
|
14
|
+
Unitsml::Configuration.context
|
|
15
|
+
|
|
16
|
+
from_hash(DATABASE, register: context_id)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
Configuration.register_model(self, id: :database)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -16,43 +16,41 @@ module Unitsml
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def length=(value)
|
|
19
|
-
quantities_common_code(:length,
|
|
19
|
+
quantities_common_code(:length, value)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def mass=(value)
|
|
23
|
-
quantities_common_code(:mass,
|
|
23
|
+
quantities_common_code(:mass, value)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def time=(value)
|
|
27
|
-
quantities_common_code(:time,
|
|
27
|
+
quantities_common_code(:time, value)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
def thermodynamic_temperature=(value)
|
|
31
|
-
quantities_common_code(:thermodynamic_temperature,
|
|
32
|
-
wrap_dimension_value(value))
|
|
31
|
+
quantities_common_code(:thermodynamic_temperature, value)
|
|
33
32
|
end
|
|
34
33
|
|
|
35
34
|
def amount_of_substance=(value)
|
|
36
|
-
quantities_common_code(:amount_of_substance,
|
|
37
|
-
wrap_dimension_value(value))
|
|
35
|
+
quantities_common_code(:amount_of_substance, value)
|
|
38
36
|
end
|
|
39
37
|
|
|
40
38
|
def luminous_intensity=(value)
|
|
41
|
-
quantities_common_code(:luminous_intensity,
|
|
39
|
+
quantities_common_code(:luminous_intensity, value)
|
|
42
40
|
end
|
|
43
41
|
|
|
44
42
|
def plane_angle=(value)
|
|
45
|
-
quantities_common_code(:plane_angle,
|
|
43
|
+
quantities_common_code(:plane_angle, value)
|
|
46
44
|
end
|
|
47
45
|
|
|
48
46
|
def electric_current=(value)
|
|
49
|
-
quantities_common_code(:electric_current,
|
|
47
|
+
quantities_common_code(:electric_current, value)
|
|
50
48
|
end
|
|
51
49
|
|
|
52
50
|
def dim_symbols
|
|
53
|
-
processed_keys.
|
|
54
|
-
public_send(vec)
|
|
55
|
-
end.
|
|
51
|
+
processed_keys.flat_map do |vec|
|
|
52
|
+
dimension_symbols_for(public_send(vec)).map(&:id)
|
|
53
|
+
end.compact
|
|
56
54
|
end
|
|
57
55
|
|
|
58
56
|
def processed_symbol
|
|
@@ -76,19 +74,24 @@ module Unitsml
|
|
|
76
74
|
|
|
77
75
|
instance_variable_set(:"@#{instance_var}", value)
|
|
78
76
|
@processed_keys << instance_var.to_s
|
|
79
|
-
|
|
77
|
+
dim_symbols = dimension_symbols_for(value)
|
|
78
|
+
return if Lutaml::Model::Utils.empty?(dim_symbols)
|
|
80
79
|
|
|
81
80
|
@parsable = true
|
|
82
|
-
|
|
81
|
+
dim_symbols.each { |dim_sym| @parsables[dim_sym.id] = id }
|
|
83
82
|
end
|
|
84
83
|
|
|
85
|
-
def
|
|
86
|
-
return
|
|
87
|
-
return DimensionQuantity.new(value.to_hash) if value.is_a?(::Unitsdb::DimensionDetails)
|
|
88
|
-
return DimensionQuantity.new(value) if value.is_a?(Hash)
|
|
84
|
+
def dimension_symbols_for(value)
|
|
85
|
+
return [] if value.nil?
|
|
89
86
|
|
|
90
|
-
value
|
|
87
|
+
if value.respond_to?(:dim_symbols)
|
|
88
|
+
Array(value.dim_symbols)
|
|
89
|
+
else
|
|
90
|
+
Array(value.symbols)
|
|
91
|
+
end
|
|
91
92
|
end
|
|
92
93
|
end
|
|
94
|
+
|
|
95
|
+
Configuration.register_model(Dimension, id: :dimension)
|
|
93
96
|
end
|
|
94
97
|
end
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
module Unitsdb
|
|
5
|
-
class
|
|
5
|
+
class DimensionDetails < ::Unitsdb::DimensionDetails
|
|
6
6
|
def dim_symbols_ids(hash, dim_id)
|
|
7
7
|
symbols&.each { |dim_sym| hash[dim_sym.id] = dim_id }
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
|
+
|
|
11
|
+
Configuration.register_model(DimensionDetails, id: :dimension_details)
|
|
10
12
|
end
|
|
11
13
|
end
|
|
@@ -16,10 +16,12 @@ module Unitsml
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def prefix
|
|
19
|
-
@prefix ||= ::Unitsdb.database.prefixes.find do |p|
|
|
19
|
+
@prefix ||= Unitsml::Unitsdb.database.prefixes.find do |p|
|
|
20
20
|
p.identifiers.any? { |i| i.id == id }
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
|
+
|
|
25
|
+
Configuration.register_model(PrefixReference, id: :prefix_reference)
|
|
24
26
|
end
|
|
25
27
|
end
|
data/lib/unitsml/unitsdb/unit.rb
CHANGED
|
@@ -3,14 +3,6 @@
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
module Unitsdb
|
|
5
5
|
class Unit < ::Unitsdb::Unit
|
|
6
|
-
def si_derived_bases=(value)
|
|
7
|
-
return super if value.nil?
|
|
8
|
-
|
|
9
|
-
super(value.map do |s|
|
|
10
|
-
SiDerivedBase.new(s.to_hash)
|
|
11
|
-
end)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
6
|
def dimension_url
|
|
15
7
|
quantity_id = quantity_references[0].id
|
|
16
8
|
quantity = Unitsml::Unitsdb.quantities.find_by_id(quantity_id)
|
|
@@ -24,6 +16,8 @@ module Unitsml
|
|
|
24
16
|
def nist_id
|
|
25
17
|
identifiers.find { |id| id.type == "nist" }&.id
|
|
26
18
|
end
|
|
19
|
+
|
|
20
|
+
Configuration.register_model(self, id: :unit)
|
|
27
21
|
end
|
|
28
22
|
end
|
|
29
23
|
end
|