Skip to content

Commit ba623e1

Browse files
author
suleman-uzair
committed
fix: load UnitsDB with context fallback and clean up MathML handling
1 parent 5260d43 commit ba623e1

8 files changed

Lines changed: 57 additions & 26 deletions

File tree

‎Gemfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ gem "lutaml-model",
1010
"~> 0.8.0",
1111
github: "lutaml/lutaml-model",
1212
branch: "fix/global-context-register-lookup-fallback"
13-
gem "mml", github: "plurimath/mml", branch: "main"
13+
gem "mml", github: "plurimath/mml", branch: "fix/mixed-content-collection"
1414
gem "oga"
1515
gem "ox"
1616
gem "plurimath", github: "plurimath/plurimath",

‎docs/README.adoc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ out of commits unless the branch intentionally requires them.
5454

5555
The standard Ruby runtime path goes through `::Unitsdb.database`. When
5656
`unitsdb-ruby` cannot load its bundled `data/` directory, UnitsML falls back to
57-
the packaged YAML files under `vendor/unitsdb`.
57+
the packaged YAML files in the `unitsdb-ruby` gem's `vendor/unitsdb`
58+
directory, rather than a `vendor/unitsdb` directory in this repository.
5859

5960

6061
== Usage

‎lib/unitsml/formula.rb‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,6 @@ def plurimath_available?
199199
Plurimath.const_defined?(:Mathml)
200200
end
201201

202-
def nullify_mml_models
203-
return unless defined?(Plurimath::Mathml::Parser::CONFIGURATION)
204-
205-
Plurimath::Mathml::Parser::CONFIGURATION.each_key { |klass| klass.model(klass) }
206-
end
207-
208-
def reset_mml_models
209-
return unless defined?(Plurimath::Mathml::Parser::CONFIGURATION)
210-
211-
::Mml::V4::Configuration.custom_models = Plurimath::Mathml::Parser::CONFIGURATION
212-
end
213202
def process_value(math, mathml_instances)
214203
case mathml_instances
215204
when Array

‎lib/unitsml/unit.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def to_mathml(options)
4343
if prefix
4444
value = mml_v4_with_content(
4545
value,
46-
"#{prefix.to_mathml(**options, parent: value)}#{value.value}",
46+
"#{prefix.to_mathml(options.merge(parent: value))}#{value.value}",
4747
)
4848
end
4949
if power_numerator

‎lib/unitsml/unitsdb.rb‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,24 @@ def database
6868
private
6969

7070
def load_database
71+
context_id = Configuration.context.id
72+
7173
if RUBY_ENGINE == "opal"
72-
return Database.from_db(nil, context: Configuration.context.id)
74+
return Database.from_db(nil, context: context_id)
7375
end
7476

7577
if ::Unitsdb.respond_to?(:database)
76-
return ::Unitsdb.database(context: Configuration.context.id)
78+
return load_unitsdb_database(context_id)
7779
end
7880

79-
::Unitsdb::Database.from_db(database_path)
81+
Database.from_db(database_path, context: context_id)
82+
end
83+
84+
def load_unitsdb_database(context_id)
85+
::Unitsdb.database(context: context_id)
8086
rescue ::Unitsdb::Errors::DatabaseNotFoundError,
8187
::Unitsdb::Errors::DatabaseFileNotFoundError
82-
::Unitsdb::Database.from_db(database_path)
88+
Database.from_db(database_path, context: context_id)
8389
end
8490

8591
def database_path

‎lib/unitsml/unitsdb/prefix_reference.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def power
1616
end
1717

1818
def prefix
19-
@prefix ||= ::Unitsdb.database.prefixes.find do |p|
19+
@prefix ||= Unitsml::Unitsdb.database.prefixes.find do |p|
2020
p.identifiers.any? { |i| i.id == id }
2121
end
2222
end

‎lib/unitsml/utility.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ def prefix_like?(prefix)
184184
def prefix_symbolid(prefix)
185185
return prefix.symbolid if prefix.respond_to?(:symbolid)
186186

187-
resolved_prefix = resolved_prefix(prefix)
188-
return unless resolved_prefix
187+
prefix_record = resolved_prefix(prefix)
188+
return unless prefix_record
189189

190-
resolved_prefix.symbols&.first&.ascii
190+
prefix_record.symbols&.first&.ascii
191191
end
192192

193193
def prefix_base(prefix)

‎spec/unitsml/unitsdb/database_spec.rb‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@
3131
end
3232
end
3333
end
34-
end
3534

36-
RSpec.describe Unitsml::Unitsdb do
37-
describe ".database" do
38-
let(:context) { instance_double("Unitsdb context", id: :unitsml_ruby) }
35+
describe Unitsml::Unitsdb, ".database" do
36+
let(:context) { Struct.new(:id).new(:unitsml_ruby) }
3937

4038
before do
4139
described_class.instance_variable_set(:@database, nil)
@@ -61,5 +59,42 @@
6159
)
6260
end
6361
end
62+
63+
context "when unitsdb-ruby exposes a database loader" do
64+
before do
65+
stub_const("RUBY_ENGINE", "ruby")
66+
allow(Unitsdb).to receive(:database).and_return(:unitsdb_database)
67+
end
68+
69+
it "uses the unitsdb-ruby loader with the UnitsML context" do
70+
expect(described_class.database).to eq(:unitsdb_database)
71+
expect(Unitsdb).to have_received(:database).with(
72+
context: :unitsml_ruby,
73+
)
74+
end
75+
end
76+
77+
context "when unitsdb-ruby cannot load its packaged data directory" do
78+
before do
79+
stub_const("RUBY_ENGINE", "ruby")
80+
allow(Unitsdb).to receive(:database).and_raise(
81+
Unitsdb::Errors::DatabaseNotFoundError,
82+
)
83+
allow(described_class)
84+
.to receive(:database_path)
85+
.and_return("/tmp/fallback-unitsdb")
86+
allow(Unitsml::Unitsdb::Database)
87+
.to receive(:from_db)
88+
.and_return(:fallback_database)
89+
end
90+
91+
it "falls back to the UnitsML database wrapper with the same context" do
92+
expect(described_class.database).to eq(:fallback_database)
93+
expect(Unitsml::Unitsdb::Database).to have_received(:from_db).with(
94+
"/tmp/fallback-unitsdb",
95+
context: :unitsml_ruby,
96+
)
97+
end
98+
end
6499
end
65100
end

0 commit comments

Comments
 (0)