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
-
.clear ⇒ Object
Clear all previously registered conversions.
- .define_shortcut(unit) ⇒ Object
-
.load_defaults ⇒ Object
Load all the default conversions shipped with the code.
-
.register(from, to, rate) ⇒ Object
Register a new conversion.
Class Method Details
.clear ⇒ Object
Clear all previously registered conversions
14 15 16 |
# File 'lib/conversions.rb', line 14 def self.clear self.conversions = {} end |
.define_shortcut(unit) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/conversions.rb', line 38 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_defaults ⇒ Object
Load all the default conversions shipped with the code
20 21 22 |
# File 'lib/conversions.rb', line 20 def self.load_defaults load File.('../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)
29 30 31 32 33 34 35 36 |
# File 'lib/conversions.rb', line 29 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 |