Skip to content

Commit

Permalink
#1 new classes support: UniSymbol, UniSystem, Dimension, Quantity, Un…
Browse files Browse the repository at this point in the history
…it refactoring
  • Loading branch information
w00lf authored and ronaldtse committed Dec 29, 2022
1 parent 161db9d commit 4939d85
Show file tree
Hide file tree
Showing 18 changed files with 510 additions and 27 deletions.
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ source "https://rubygems.org"

# Specify your gem's dependencies in unitsml.gemspec
gemspec

gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"
34 changes: 33 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ PATH
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
byebug (11.1.3)
coderay (1.1.3)
diff-lcs (1.4.4)
docile (1.3.5)
method_source (0.9.2)
parallel (1.20.1)
parser (3.0.0.0)
ast (~> 2.4.1)
powerpack (0.1.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rainbow (3.0.0)
rake (12.3.3)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand All @@ -21,13 +34,32 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (0.54.0)
parallel (~> 1.10)
parser (>= 2.5)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.11.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.2)
unicode-display_width (1.7.0)

PLATFORMS
ruby

DEPENDENCIES
byebug
pry (~> 0.12.2)
rake (~> 12.0)
rspec (~> 3.0)
rspec (~> 3.6)
rubocop (= 0.54.0)
simplecov (~> 0.15)
unitsml!

BUNDLED WITH
Expand Down
11 changes: 11 additions & 0 deletions lib/unitsml.rb
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
86 changes: 86 additions & 0 deletions lib/unitsml/dimension.rb
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
60 changes: 60 additions & 0 deletions lib/unitsml/quantity.rb
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
49 changes: 32 additions & 17 deletions lib/unitsml/unit.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "yaml"
require "unitsml/unit_symbol"

module Unitsml
class Unit
UNITS_DB_PATH = File
UNITS_DB_UNITS_PATH = File
.expand_path("../../vendor/unitsdb/units.yaml",
File.dirname(__FILE__))
.freeze
Expand All @@ -21,32 +22,42 @@ class Unit
:prefixed

class << self
def find_unit(ascii: ascii)
id, attrs = symbols.find { |(id, attribute)| id == ascii.to_s }
Unitsml::Unit.new(id, attrs) if id
def find_unit(ascii:)
symbols.find { |unit| unit.symbols_hash.keys.include?(ascii.to_s) }
end

def symbols
@symbols ||= YAML.load(File.read(UNITS_DB_PATH))
@symbols ||= YAML
.load(File.read(UNITS_DB_UNITS_PATH))
.map { |(id, attrs)| Unitsml::Unit.new(id, attrs) }
end

def from_yaml(yaml_path)
@symbols = YAML
.load(File.read(yaml_path))
.map { |(id, attrs)| Unitsml::Unit.new(id, attrs) }
end
end

def initialize(id, hash)
def initialize(id, attrs)
begin
@id = id
@short = short
@dimension = hash["dimension_url"].sub(/^#/, "")
hash["short"] && !hash["short"].empty? and @short = hash["short"]
@unit_system = hash["unit_system"]
@names = hash["unit_name"]
@symbols_hash = hash["unit_symbols"]&.each_with_object({}) { |h, m| m[h["id"]] = h } || {}
@symbols = hash["unit_symbols"]
hash["root_units"] and hash["root_units"]["enumerated_root_units"] and
@root = hash["root_units"]["enumerated_root_units"]
hash["quantity_reference"] and @quantities = hash["quantity_reference"].map { |x| x["url"].sub(/^#/, "") }
hash["si_derived_bases"] and @si_derived_bases = hash["si_derived_bases"]
@dimension = attrs["dimension_url"].sub(/^#/, "")
attrs["short"] && !attrs["short"].empty? and @short = attrs["short"]
@unit_system = attrs["unit_system"]
@names = attrs["unit_name"]
@symbols_hash = attrs["unit_symbols"]
&.each_with_object({}) do |sym_attrs, res|
res[sym_attrs["id"]] = Unitsml::UnitSymbol.new(id, sym_attrs)
end || {}
@symbols = attrs["unit_symbols"]&.map { |sym_attrs| Unitsml::UnitSymbol.new(sym_attrs["id"], sym_attrs) }
attrs["root_units"] and attrs["root_units"]["enumerated_root_units"] and
@root = attrs["root_units"]["enumerated_root_units"]
attrs["quantity_reference"] and @quantities = attrs["quantity_reference"].map { |x| x["url"].sub(/^#/, "") }
attrs["si_derived_bases"] and @si_derived_bases = attrs["si_derived_bases"]
rescue
raise StandardError.new "Parse fail on Unit #{id}: #{hash}"
raise StandardError.new "Parse fail on Unit #{id}: #{attrs}"
end
end

Expand All @@ -69,5 +80,9 @@ def symbolid
def symbolids
@symbols ? @symbols.map { |s| s["id"] } : [ @short ]
end

def to_latex
@symbols.map(&:latex).reduce(:+) if @symbols
end
end
end
21 changes: 21 additions & 0 deletions lib/unitsml/unit_symbol.rb
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
36 changes: 36 additions & 0 deletions lib/unitsml/unit_system.rb
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
15 changes: 15 additions & 0 deletions spec/fixtures/dimensions_test.yaml
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
7 changes: 7 additions & 0 deletions spec/fixtures/unit_systems_test.yaml
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
Loading

0 comments on commit 4939d85

Please sign in to comment.