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



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

def self.clear
  self.conversions = {}
end

.define_shortcut(unit) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/conversions.rb', line 36

def self.define_shortcut(unit)
  Numeric.class_eval do
    define_method unit do
      Conversions::Unit.new(self, unit)
    end unless respond_to? unit
  end
end

.load_defaultsObject

Load all the default conversions shipped with the code



18
19
20
# File 'lib/conversions.rb', line 18

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)



27
28
29
30
31
32
33
34
# File 'lib/conversions.rb', line 27

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