Method: Quantify::Dimensions.for

Defined in:
lib/quantify/dimensions.rb

.for(name) ⇒ Object

Retrieve a known quantity - returns a Dimensions instance, which is a clone of the initialized instance of the specified quantity. This enables the object to be modified/manipulated without corrupting the representation of the quantity in the @@dimensions class array.

The required quantity name/descriptor can be specified as a symbol or a string, e.g.:

Dimensions.for :acceleration
Dimensions.for 'luminous_flux'

These can be shortened to, e.g. Dimensions.acceleration by virtue of the #method_missing class method (below)



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/quantify/dimensions.rb', line 116

def self.for(name)
  return name if name.is_a? Dimensions
  if (name.is_a?(String) || name.is_a?(Symbol))
    name = name.remove_underscores.downcase
    if quantity = @@dimensions.find { |quantity| quantity.physical_quantity == name }
      return quantity.clone
    else
      raise Exceptions::InvalidArgumentError, "Physical quantity not known: #{name}"
    end
  else
    raise Exceptions::InvalidArgumentError, "Argument must be a Symbol or String"
  end
end