Class: UnitsDB::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/unitsdb_ruby/unitsdb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, hash) ⇒ Unit

Returns a new instance of Unit.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/unitsdb_ruby/unitsdb.rb', line 124

def initialize(id, hash)
  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]
    @prefixed = (hash[:prefixed] == true)
  rescue
    raise StandardError.new "Parse fail on Unit #{id}: #{hash}"
  end
end

Instance Attribute Details

#dimensionObject (readonly)

Returns the value of attribute dimension.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def dimension
  @dimension
end

#idObject (readonly)

Returns the value of attribute id.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def id
  @id
end

#namesObject (readonly)

Returns the value of attribute names.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def names
  @names
end

#prefixedObject (readonly)

Returns the value of attribute prefixed.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def prefixed
  @prefixed
end

#quantitiesObject (readonly)

Returns the value of attribute quantities.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def quantities
  @quantities
end

#rootObject (readonly)

Returns the value of attribute root.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def root
  @root
end

#root_unitsObject (readonly)

Returns the value of attribute root_units.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def root_units
  @root_units
end

#shortObject (readonly)

Returns the value of attribute short.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def short
  @short
end

#si_derived_basesObject (readonly)

Returns the value of attribute si_derived_bases.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def si_derived_bases
  @si_derived_bases
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def symbols
  @symbols
end

#symbols_hashObject (readonly)

Returns the value of attribute symbols_hash.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def symbols_hash
  @symbols_hash
end

#unit_systemObject (readonly)

Returns the value of attribute unit_system.



121
122
123
# File 'lib/unitsdb_ruby/unitsdb.rb', line 121

def unit_system
  @unit_system
end

Instance Method Details

#nameObject



152
153
154
# File 'lib/unitsdb_ruby/unitsdb.rb', line 152

def name
  @names.first
end

#symbolidObject



156
157
158
# File 'lib/unitsdb_ruby/unitsdb.rb', line 156

def symbolid
  @symbols ? @symbols.first[:id] : @short
end

#symbolidsObject



160
161
162
# File 'lib/unitsdb_ruby/unitsdb.rb', line 160

def symbolids
  @symbols ? @symbols.map { |s| s[:id] } : [ @short ]
end

#system_nameObject



144
145
146
# File 'lib/unitsdb_ruby/unitsdb.rb', line 144

def system_name
  @unit_system[:name]
end

#system_typeObject



148
149
150
# File 'lib/unitsdb_ruby/unitsdb.rb', line 148

def system_type
  @unit_system[:type]
end