Skip to content

[WIP] Add programmatic composite unit builder#72

Draft
suleman-uzair wants to merge 2 commits into
mainfrom
feature/composite_units_builder
Draft

[WIP] Add programmatic composite unit builder#72
suleman-uzair wants to merge 2 commits into
mainfrom
feature/composite_units_builder

Conversation

@suleman-uzair

Copy link
Copy Markdown
Member

This PR adds programmatic creation of composite units, in addition to Unitsml.parse, using multiplication/division operators and the Unitsml.compose method. The generated Formula matches the equivalent parsed-string output in all supported conversions.

# operator DSL (on Unit, Dimension, and Formula) — returns a Unitsml::Formula
Unitsml::Unit.new("W") / Unitsml::Unit.new("m") / Unitsml::Unit.new("sr")
Unitsml::Unit.new("m") / Unitsml::Unit.new("s", 2) # power via the constructor

# keyword form
Unitsml.compose(units: ["W", { unit: "m", power: -1 }, :sr], quantity: "radiance")

# quantity/name/multiplier are passed to the conversion methods
formula.to_xml(quantity: "radiance")

Units are referenced by symbol id ("W") or short name ("watt"), and unknown units/prefixes or mixing units with dimensions raise Unitsml::Errors::*. This PR also updates quantity resolution to support names and emits a canonical Quantity element (NIST id, quantityType, dimensionURL) in XML output.

closes #2

suleman-uzair added 2 commits July 1, 2026 23:08
Compose units from parts instead of only parsing strings. Adds the multiplication and division operator DSL (on Unit, Dimension and Formula via a Composable mixin) and the Unitsml.compose keyword form, both producing a Formula whose output matches the equivalent parsed string across every format. Units are referenced by symbol id or short name, powers come from the constructor, and quantity/name/multiplier metadata are passed to the render methods.

Also resolves quantities by name and short slug (Quantities#find_by_name) and emits a canonical Quantity element with the NIST xml:id, quantityType and dimensionURL.

Implements #2.
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.

Implement composite units

1 participant