Module: Conversions

Defined in:
lib/conversions.rb,
lib/conversions/unit.rb,
lib/conversions/active_record_accessors.rb

Overview

Conversions makes it easy to convert between units.

Defined Under Namespace

Modules: ActiveRecordAccessors, Ext Classes: Unit

Class Method Summary collapse

Class Method Details

.clearObject

Clear all previously registered conversions



6
7
8
# File 'lib/conversions.rb', line 6

def self.clear
  self.conversions = {}
end

.load_defaultsObject

Load all the default conversions shipped with the code



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

def self.load_defaults
  load File.expand_path('../conversions/defaults.rb', __FILE__)
end

.register(from, to, rate) ⇒ Object

Register a new conversion. This automatically also registers the inverse conversion.

  • from: The unit to convert from (ie. :miles, :stones, or :pints)

  • to: The unit to convert to

  • rate: The conversion rate from from to to. (from * rate = to)



21
22
23
24
25
26
# File 'lib/conversions.rb', line 21

def self.register(from, to, rate)
  conversions[from] ||= {}
  conversions[from][to] = rate
  conversions[to] ||= {}
  conversions[to][from] = 1.0 / rate
end