Module: Unitsml::Unitsdb

Included in:
Parse, Utility
Defined in:
lib/unitsml/unitsdb.rb

Class Method Summary collapse

Class Method Details

.dimensions_hashObject



62
63
64
# File 'lib/unitsml/unitsdb.rb', line 62

def dimensions_hash
  @@dimensions_hashs ||= insert_vectors(load_dimensions)
end

.filtered_unitsObject



52
53
54
55
56
# File 'lib/unitsml/unitsdb.rb', line 52

def filtered_units
  @@filtered_units_array ||= units.keys.reject do |unit|
    ((/\*|\^|\/|^1$/).match?(unit) || units.dig(unit, :fields, "prefixed"))
  end
end

.find_id(value) ⇒ Object



74
75
76
77
78
79
# File 'lib/unitsml/unitsdb.rb', line 74

def find_id(value)
  return if value == true
  return unless value.is_a?(Hash)

  value&.dig("dim_symbols")&.map { |symbol| symbol&.dig("id") }&.first
end

.insert_vectors(dims) ⇒ Object



89
90
91
92
93
94
# File 'lib/unitsml/unitsdb.rb', line 89

def insert_vectors(dims)
  dims.each do |key, value|
    value[:vector] = vector(value)
    value[:id] = key
  end
end

.load_dimensionsObject



12
13
14
# File 'lib/unitsml/unitsdb.rb', line 12

def load_dimensions
  @@dim_file = load_yaml(:dimensions)
end

.load_unitsObject



16
17
18
# File 'lib/unitsml/unitsdb.rb', line 16

def load_units
  @@units_file = load_yaml(:units)
end

.load_yaml(file_name) ⇒ Object



7
8
9
10
# File 'lib/unitsml/unitsdb.rb', line 7

def load_yaml(file_name)
  @@hash ||= {}
  @@hash[file_name] ||= YAML.load_file(valid_path(file_name))
end

.parsable_dimensionsObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/unitsml/unitsdb.rb', line 36

def parsable_dimensions
  @@parsable_dimensions ||= {}
  return @@parsable_dimensions unless @@parsable_dimensions.empty?

  dimensions_hash.each do |key, value|
    value.each do |_, v|
      @@parsable_dimensions[find_id(v)] = { id: key, fields: value }
    end
  end
  @@parsable_dimensions
end

.prefixesObject



32
33
34
# File 'lib/unitsml/unitsdb.rb', line 32

def prefixes
  @@prefixes_array ||= prefixes_hash.keys.sort_by(&:length)
end

.prefixes_hashObject



58
59
60
# File 'lib/unitsml/unitsdb.rb', line 58

def prefixes_hash
  @@prefixes_hashes ||= prefixs_ids(load_yaml(:prefixes))
end

.prefixs_ids(prefixe_hash, hash = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/unitsml/unitsdb.rb', line 66

def prefixs_ids(prefixe_hash, hash = {})
  prefixe_hash&.each do |key, value|
    symbol = value&.dig("symbol", "ascii")
    hash[symbol] = { id: key, fields: value } unless symbol&.empty?
  end
  hash
end

.quantitiesObject



48
49
50
# File 'lib/unitsml/unitsdb.rb', line 48

def quantities
  @@quantities ||= load_yaml(:quantities)
end

.underscore(str) ⇒ Object



85
86
87
# File 'lib/unitsml/unitsdb.rb', line 85

def underscore(str)
  str.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

.unitsObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/unitsml/unitsdb.rb', line 20

def units
  @@units ||= {}
  return @@units unless @@units.empty?

  load_units.each do |key, value|
    value["unit_symbols"]&.each do |symbol|
      @@units[symbol["id"]] = { id: key, fields: value } unless symbol["id"]&.empty?
    end
  end
  @@units
end

.valid_path(file_name) ⇒ Object



96
97
98
99
100
# File 'lib/unitsml/unitsdb.rb', line 96

def valid_path(file_name)
  File.expand_path(
    File.join(__dir__, "..", "..","unitsdb", "#{file_name}.yaml")
  )
end

.vector(dim_hash) ⇒ Object



81
82
83
# File 'lib/unitsml/unitsdb.rb', line 81

def vector(dim_hash)
  Utility::DIMS_VECTOR.map { |h| dim_hash.dig(underscore(h), "powerNumerator") }.join(":")
end