Skip to content

Add Opal database payload loader#65

Closed
suleman-uzair wants to merge 3 commits into
mainfrom
fix/opal-database-payload-loader
Closed

Add Opal database payload loader#65
suleman-uzair wants to merge 3 commits into
mainfrom
fix/opal-database-payload-loader

Conversation

@suleman-uzair

Copy link
Copy Markdown
Member

Adds an explicit Unitsml::Unitsdb::Database.load_opal_payload hook for Opal builds.

This lets downstream packages provide the bundled UnitsDB payload without generating subclasses or reimplementing from_db. The Opal loader remains owned by unitsml-ruby, including the missing-payload error path.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a dedicated Opal-only hook to inject a bundled UnitsDB payload at runtime, allowing Opal consumers to use Unitsml::Unitsdb::Database.from_db without subclassing or re-implementing loading behavior.

Changes:

  • Add Unitsml::Unitsdb::Database.load_opal_payload and opal_payload helpers to support injected payloads (with fallback to the DATABASE constant).
  • Update Opal from_db path to use the new opal_payload accessor and raise when missing.
  • Extend specs to cover the new Opal payload injection behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/unitsml/unitsdb/database.rb Introduces load_opal_payload/opal_payload and switches Opal loading to use the payload accessor.
spec/unitsml/unitsdb/database_spec.rb Resets Opal payload between examples and adds coverage for the new payload injection hook.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/unitsml/unitsdb/database.rb Outdated
Comment thread spec/unitsml/unitsdb/database_spec.rb Outdated
@suleman-uzair suleman-uzair marked this pull request as ready for review April 27, 2026 11:21
@suleman-uzair suleman-uzair force-pushed the fix/opal-database-payload-loader branch from 64ec644 to 85e08a1 Compare April 29, 2026 11:26
ronaldtse added a commit that referenced this pull request Jun 27, 2026
Under Opal, autoload declarations do not lazy-execute, so consumers
(plurimath-js) need an explicit boot file that eager-requires every
autoloaded entry point in dependency order. unitsml is unique among
the moxml-dependent gems: it ships a large unitsdb YAML database that
must be pre-serialized as a Ruby hash under Opal (no filesystem).

This change adds the full Opal toolchain for unitsml:

- lib/unitsml/opal.rb: boot file eager-requiring every autoloaded
  entry point. Requires upstream boot files (unitsdb/opal, mml/opal,
  omml/opal) and the payload file so Database.from_db can satisfy
  without a filesystem.

- lib/unitsml/opal/payload_generator.rb: Unitsml::Opal::PayloadGenerator
  class that reads unitsdb-ruby YAML data on MRI and emits a Ruby
  source file defining Unitsml::Unitsdb::Database::DATABASE as a
  frozen hash. Invoked via rake unitsml:generate_opal_payload.

- lib/unitsml/opal/database_payload.rb: AUTO-GENERATED output committed
  to the gem. Loaded by the boot file under Opal.

- lib/unitsml/unitsdb/database.rb: adopts the loader mechanism from
  fix/opal-database-payload-loader (load_opal_payload, opal_payload,
  reset_opal_payload) so tests can inject small payloads and the
  committed DATABASE constant is found via const_defined?(:DATABASE,
  false). Supersedes PR #65.

- lib/unitsml/unitsdb.rb, lib/unitsml/model.rb and the four nested
  model parent files: convert require_relative and __dir__-interpolated
  autoloads to plain load-path-relative autoload declarations, per
  the global rule. Removes a dangling autoload (Unitsml::Model::
  Namespace) whose target file never existed; MRI never tripped on
  it because nothing referenced the constant, but Opal eager-loads
  every autoload and would have crashed.

- Rakefile: adds the unitsml:generate_opal_payload task.

- Gemfile: adds nokogiri and opal so the boot-file spec can compile
  the boot file end-to-end via Opal::Builder with native deps
  stubbed.

- .rubocop.yml: excludes the generated payload file from lint.

- spec/unitsml/opal_boot_spec.rb: verifies the boot file structure
  and that Opal::Builder compiles it end-to-end with stubs.

- spec/unitsml/payload_sync_spec.rb: verifies the committed payload
  matches what the generator produces from the current unitsdb gem
  and that it round-trips through from_hash.

- spec/unitsml/opal/payload_generator_spec.rb: verifies the
  generator produces evaluable Ruby that reproduces the source
  database.

- spec/unitsml/unitsdb/database_spec.rb: extends coverage to the
  new loader mechanism (load_opal_payload, DATABASE fallback).

- .github/workflows/opal.yml: GHA workflow running the opal specs
  on Ubuntu/Ruby 3.3/Node 18.
ronaldtse added a commit that referenced this pull request Jun 27, 2026
Under Opal, autoload declarations do not lazy-execute, so consumers
(plurimath-js) need an explicit boot file that eager-requires every
autoloaded entry point in dependency order. unitsml is unique among
the moxml-dependent gems: it ships a large unitsdb YAML database that
must be pre-serialized as a Ruby hash under Opal (no filesystem).

This change adds the full Opal toolchain for unitsml:

- lib/unitsml/opal.rb: boot file eager-requiring every autoloaded
  entry point. Requires upstream boot files (unitsdb/opal, mml/opal,
  omml/opal) and the payload file so Database.from_db can satisfy
  without a filesystem.

- lib/unitsml/opal/payload_generator.rb: Unitsml::Opal::PayloadGenerator
  class that reads unitsdb-ruby YAML data on MRI and emits a Ruby
  source file defining Unitsml::Unitsdb::Database::DATABASE as a
  frozen hash. Invoked via rake unitsml:generate_opal_payload.

- lib/unitsml/opal/database_payload.rb: AUTO-GENERATED output committed
  to the gem. Loaded by the boot file under Opal.

- lib/unitsml/unitsdb/database.rb: adopts the loader mechanism from
  fix/opal-database-payload-loader (load_opal_payload, opal_payload,
  reset_opal_payload) so tests can inject small payloads and the
  committed DATABASE constant is found via const_defined?(:DATABASE,
  false). Supersedes PR #65.

- lib/unitsml/unitsdb.rb, lib/unitsml/model.rb and the four nested
  model parent files: convert require_relative and __dir__-interpolated
  autoloads to plain load-path-relative autoload declarations, per
  the global rule. Removes a dangling autoload (Unitsml::Model::
  Namespace) whose target file never existed; MRI never tripped on
  it because nothing referenced the constant, but Opal eager-loads
  every autoload and would have crashed.

- Rakefile: adds the unitsml:generate_opal_payload task.

- Gemfile: adds nokogiri and opal so the boot-file spec can compile
  the boot file end-to-end via Opal::Builder with native deps
  stubbed.

- .rubocop.yml: excludes the generated payload file from lint.

- spec/unitsml/opal_boot_spec.rb: verifies the boot file structure
  and that Opal::Builder compiles it end-to-end with stubs.

- spec/unitsml/payload_sync_spec.rb: verifies the committed payload
  matches what the generator produces from the current unitsdb gem
  and that it round-trips through from_hash.

- spec/unitsml/opal/payload_generator_spec.rb: verifies the
  generator produces evaluable Ruby that reproduces the source
  database.

- spec/unitsml/unitsdb/database_spec.rb: extends coverage to the
  new loader mechanism (load_opal_payload, DATABASE fallback).

- .github/workflows/opal.yml: GHA workflow running the opal specs
  on Ubuntu/Ruby 3.3/Node 18.
@suleman-uzair

Copy link
Copy Markdown
Member Author

Closing as superseded by #69 (refined in #71).

@suleman-uzair suleman-uzair deleted the fix/opal-database-payload-loader branch June 29, 2026 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants