Class | Units::Converter
In: | lib/facets/more/units.rb
Parent: | Object

This class handles all conversions between Units.

There are two kinds of Units; those that are not expressed as a function of other Units —called base units— and those that are expressed as a function of other Units —called derived units. The former kind is registered without specifying how it depends on other Units, while the latter kind is registered with doing so.

This class also registers a list of Converters that are generally useable. The default Converter which is used when none is specified, can be retrieved with Converter.current. Converters can be registered with Converter.register.

Converters can be loaded from YAML. This allows Converters to be specified in configuration files.

### Methods

converter current from_yaml load_yaml method_missing new register register_binary_unit register_currency register_length_unit register_si_unit register_unit registered? registered_converters registered_units

### Classes and Modules

Class Units::Converter::ExchangeRate

### Constants

SI_PREFIXES

=

{ 'yotta' => {:abbrev => 'Y', :multiplier => 1e24}, 'zetta' => {:abbrev => 'Z', :multiplier => 1e21}, 'exa' => {:abbrev => 'E', :multiplier => 1e18}, 'peta' => {:abbrev => 'P', :multiplier => 1e14}, 'tera' => {:abbrev => 'T', :multiplier => 1e12}, 'giga' => {:abbrev => 'G', :multiplier => 1e9}, 'mega' => {:abbrev => 'M', :multiplier => 1e6}, 'kilo' => {:abbrev => 'k', :multiplier => 1e3}, 'hecto' => {:abbrev => 'h', :multiplier => 1e2}, 'deca' => {:abbrev => 'da', :multiplier => 1e1}, 'deci' => {:abbrev => 'd', :multiplier => 1e-1}, 'centi' => {:abbrev => 'c', :multiplier => 1e-2}, 'milli' => {:abbrev => 'm', :multiplier => 1e-3}, 'micro' => {:abbrev => 'u', :multiplier => 1e-6}, 'nano' => {:abbrev => 'n', :multiplier => 1e-9}, 'pico' => {:abbrev => 'p', :multiplier => 1e-12}, 'femto' => {:abbrev => 'f', :multiplier => 1e-15}, 'atto' => {:abbrev => 'a', :multiplier => 1e-18}, 'zepto' => {:abbrev => 'z', :multiplier => 1e-21}, 'yocto' => {:abbrev => 'y', :multiplier => 1e-24}

The prefixes used for SI units. See also Converter#register_si_unit.

BINARY_PREFIXES

=

{ 'yotta' => {:abbrev => 'Y', :multiplier => 1024.0 ** 8}, 'zetta' => {:abbrev => 'Z', :multiplier => 1024.0 ** 7}, 'exa' => {:abbrev => 'E', :multiplier => 1024.0 ** 6}, 'peta' => {:abbrev => 'P', :multiplier => 1024.0 ** 5}, 'tera' => {:abbrev => 'T', :multiplier => 1024.0 ** 4}, 'giga' => {:abbrev => 'G', :multiplier => 1024.0 ** 3}, 'mega' => {:abbrev => 'M', :multiplier => 1024.0 ** 2}, 'kilo' => {:abbrev => 'k', :multiplier => 1024.0}, }

The prefixes used for binary units. See also Converter#register_binary_unit.

LENGTH_PREFIXES | = | { 'square_' => {:abbrev => 'sq_', :power => 2}, 'cubic_' => {:abbrev => 'cu_', :power => 3} |  | The prefixes used for length units. See also Converter#register_length_unit.

### Public Class methods

converter(name)

Returns the converter with the given name. This name can be a Symbol or a String.

current()

Returns the current Converter in the current Thread. The default converter is the one returned by `converter(:default)`. See also Units#with_converter and Converter.converter.

from_yaml(file)

Creates a new Converter, where the data is loaded from the file with the given file name.

new(&blk)

Creates a new Converter. If a block is given, it is executed in the newly created Converter‘s context.

register(name, converter)

Registers the given Converter under the given name. This name can be a Symbol or a String.

registered_converters()

Returns the list of names of registered converters.

### Public Instance methods

load_yaml(file)

Loads data from the YAML file with the given name. Returns `self`.

method_missing(m, *args, &blk)
register_binary_unit(unit, data = {})

Registers a new binary unit. The Unit and its aliases are registered with all available binary prefixes (see BINARY_PREFIXES) as well, while the abbreviations are registered with the abbreviated version of the prefixes.

For the syntax of the options, see `register_unit`.

register_currency(curr, service = nil)

Registers a given currency, with a given servive to find the exchange rate between the given currency and a given base currency. The service should be a subclass of ExchangeRate. This is not strictly necessary, but ExchangeRate handles all of the magic.

register_length_unit(unit, data = {})

Registers a new length unit. The Unit and its aliases are registered with all available length prefixes (see LENGTH_PREFIXES) as well, while the abbreviations are registered with the abbreviated version of the prefixes.

For the syntax of the options, see `register_unit`.

register_si_unit(unit, data = {})

Registers a new SI unit. The Unit and its aliases are registered with all available SI prefixes (see SI_PREFIXES) as well, while the abbreviations are registered with the abbreviated version of the prefixes.

For the syntax of the options, see `register_unit`.

register_unit(name, data = {})

Registers a new Unit with the given name. The `data` parameter is a Hash with some extra parameters (can be Strings or Symbols):

`alias`: | Specifies possible aliases for the Unit.
`abbrev`: | Specifies possible abbreviations or symbols for the Unit. The differences with aliases is that prefixes work differently; see `register_si_unit` and `register_binary_unit`.
`equals`: | If present, specifies how the Unit depends on other Units. The value for this key can either be a Hash with `unit` mapping to a Unit and `multiplier` mapping to a numeric multiplier, or a String containing the multiplier, followed by a space, followed by a representation of the Unit as returned by Unit#to_s.

Examples:

      converter.register_unit(:pint, :alias => :pints, :abbrev => [:pt, :pts]))
      converter.register_unit(:quart, 'alias' => :quarts, :abbrev => ['qt', :qts], :equals => '2.0 pt'))
      converter.register_unit(:gallon, :alias => :gallons, :abbrev => :gal, 'equals' => {:unit => Unit.new('qt' => 1, converter), 'multiplier' => 4.0))

Note that Symbols and Strings are generally exchangeable within this library (internally they are converted to Symbols). The number one reason for this is that String look better in YAML.

See also `register_si_unit`, `register_binary_unit`, `register_length_unit` and `register_currency`.

registered?(unit)

Checks whether the unit with the given name is registered. The name can be a symbol or a string.

registered_units()

Returns the list of registered unit names as symbols.

[Validate]