-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 new classes support: UniSymbol, UniSystem, Dimension, Quantity, Un…
…it refactoring
- Loading branch information
Showing
18 changed files
with
510 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
require "unitsml/version" | ||
require "unitsml/dimension" | ||
require "unitsml/quantity" | ||
require "unitsml/unit" | ||
require "unitsml/unit_system" | ||
|
||
module Unitsml | ||
def find_unit(ascii:) | ||
Unitsml::Unit.find_unit(ascii: ascii) | ||
end | ||
|
||
def find_dimension(ascii:) | ||
Unitsml::Dimension.find_unit(ascii: ascii) | ||
end | ||
|
||
def find_unit_system(ascii:) | ||
Unitsml::UnitSystem.find_unit_system(ascii: ascii) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module Unitsml | ||
class Dimension | ||
UNITS_DB_DIMENSIONS_PATH = File | ||
.expand_path("../../vendor/unitsdb/dimensions.yaml", | ||
File.dirname(__FILE__)) | ||
.freeze | ||
|
||
attr_reader :id, | ||
:length, | ||
:mass, | ||
:time, | ||
:electric_current, | ||
:thermodynamic_temperature, | ||
:amount_of_substance, | ||
:luminous_intensity, | ||
:plane_angle, | ||
:dimensionless | ||
|
||
class << self | ||
def find_dimension(ascii:) | ||
symbols.find { |unit| unit.id == ascii.to_s } | ||
end | ||
|
||
def symbols | ||
@symbols ||= YAML | ||
.load(File.read(UNITS_DB_DIMENSIONS_PATH)) | ||
.map { |(id, attrs)| Unitsml::Dimension.new(id, attrs) } | ||
end | ||
|
||
def from_yaml(yaml_path) | ||
@symbols = YAML | ||
.load(File.read(yaml_path)) | ||
.map { |(id, attrs)| Unitsml::Dimension.new(id, attrs) } | ||
end | ||
end | ||
|
||
def initialize(id, hash) | ||
begin | ||
@id = id | ||
@dimensionless = hash["dimensionless"] | ||
hash["length"] and @length = hash["length"]["powerNumerator"].to_i | ||
hash["mass"] and @mass = hash["mass"]["powerNumerator"].to_i | ||
hash["time"] and @time = hash["time"]["powerNumerator"].to_i | ||
hash["electric_current"] and @electric_current = hash["electric_current"]["powerNumerator"].to_i | ||
hash["thermodynamic_temperature"] and | ||
@thermodynamic_temperature = hash["thermodynamic_temperature"]["powerNumerator"].to_i | ||
hash["amount_of_substance"] and @amount_of_substance = hash["amount_of_substance"]["powerNumerator"].to_i | ||
hash["luminous_intensity"] and @luminous_intensity = hash["luminous_intensity"]["powerNumerator"].to_i | ||
hash["plane_angle"] and @plane_angle = hash["plane_angle"]["powerNumerator"].to_i | ||
rescue | ||
raise StandardError.new "Parse fail on Dimension #{id}: #{hash}" | ||
end | ||
end | ||
|
||
def keys | ||
ret = [] | ||
@length and ret << "Length" | ||
@mass and ret << "Mass" | ||
@time and ret << "Time" | ||
@electric_current and ret << "ElectricCurrent" | ||
@thermodynamic_temperature and ret << "ThermodynamicTemperature" | ||
@amount_of_substance and ret << "AmountOfSubstance" | ||
@luminous_intensity and ret << "LuminousIntensity" | ||
@plane_angle and ret << "PlaneAngle" | ||
ret | ||
end | ||
|
||
def exponent(key) | ||
case key | ||
when "Length" then @length | ||
when "Mass" then @mass | ||
when "Time" then @time | ||
when "ElectricCurrent" then @electric_current | ||
when "ThermodynamicTemperature" then @thermodynamic_temperature | ||
when "AmountOfSubstance" then @amount_of_substance | ||
when "LuminousIntensity" then @luminous_intensity | ||
when "PlaneAngle" then @plane_angle | ||
end | ||
end | ||
|
||
def vector | ||
"#{@length}:#{@mass}:#{@time}:#{@electric_current}:#{@thermodynamic_temperature}:#{@amount_of_substance}:"\ | ||
"#{@luminous_intensity}:#{@plane_angle}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require "unitsml/unit" | ||
|
||
module Unitsml | ||
class Quantity | ||
UNITS_DB_QUANTITIES_PATH = File | ||
.expand_path("../../vendor/unitsdb/quantities.yaml", | ||
File.dirname(__FILE__)) | ||
.freeze | ||
|
||
attr_reader :id, :dimension, :type, :names, :units | ||
|
||
class << self | ||
def find_quantity(ascii:) | ||
symbols.find { |unit| unit.id == ascii.to_s } | ||
end | ||
|
||
def symbols | ||
@symbols ||= YAML | ||
.load(File.read(UNITS_DB_QUANTITIES_PATH)) | ||
.map { |(id, attrs)| Unitsml::Quantity.new(id, attrs) } | ||
end | ||
|
||
def from_yaml(yaml_path) | ||
@symbols = YAML | ||
.load(File.read(yaml_path)) | ||
.map { |(id, attrs)| Unitsml::Quantity.new(id, attrs) } | ||
end | ||
end | ||
|
||
def initialize(id, hash) | ||
begin | ||
@id = id | ||
@dimension = hash["dimension_url"].sub(/^#/, "") | ||
@type = hash["quantity_type"] | ||
hash["quantity_name"] and @names = hash["quantity_name"] | ||
@units = serialize_units(hash["unit_reference"]) | ||
# rescue | ||
# raise StandardError.new "Parse fail on Quantity #{id}: #{hash}" | ||
end | ||
end | ||
|
||
def serialize_units(unit_references) | ||
return unless unit_references | ||
|
||
unit_references | ||
.map do |attrs| | ||
id = attrs["url"].sub(/^#/, "") | ||
Unitsml::Unit.symbols.find { |unit| unit.id == id } | ||
end | ||
end | ||
|
||
def name | ||
@names&.first | ||
end | ||
|
||
def unit | ||
@units&.first | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require "yaml" | ||
|
||
module Unitsml | ||
class UnitSymbol | ||
attr_reader :id, | ||
:ascii, | ||
:html, | ||
:mathml, | ||
:latex, | ||
:unicode | ||
|
||
def initialize(id, hash) | ||
@id = hash['id'] | ||
@ascii = hash['ascii'] | ||
@html = hash['html'] | ||
@mathml = hash['mathml'] | ||
@latex = hash['latex'] | ||
@unicode = hash['unicode'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Unitsml | ||
class UnitSystem | ||
UNITS_DB_UNIT_SYSTEMS_PATH = File | ||
.expand_path("../../vendor/unitsdb/unit_systems.yaml", | ||
File.dirname(__FILE__)) | ||
.freeze | ||
|
||
attr_reader :id, | ||
:name, | ||
:acceptable | ||
|
||
class << self | ||
def find_unit_system(ascii:) | ||
symbols.find { |unit| unit.id == ascii.to_s } | ||
end | ||
|
||
def symbols | ||
@symbols ||= YAML | ||
.load(File.read(UNITS_DB_UNIT_SYSTEMS_PATH)) | ||
.map { |attrs| Unitsml::UnitSystem.new(attrs["id"], attrs) } | ||
end | ||
|
||
def from_yaml(yaml_path) | ||
@symbols = YAML | ||
.load(File.read(yaml_path)) | ||
.map { |attrs| Unitsml::UnitSystem.new(attrs["id"], attrs) } | ||
end | ||
end | ||
|
||
def initialize(id, attrs) | ||
@id = attrs["id"] | ||
@name = attrs["name"] | ||
@acceptable = attrs["acceptable"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
NISTd1: | ||
length: | ||
powerNumerator: 1 | ||
symbol: L | ||
|
||
NISTd2: | ||
mass: | ||
powerNumerator: 1 | ||
symbol: M | ||
|
||
NISTd3: | ||
time: | ||
powerNumerator: 1 | ||
symbol: T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
- id: test1 | ||
name: SI | ||
acceptable: true | ||
- id: test2 | ||
name: SI | ||
acceptable: true |
Oops, something went wrong.