Sha256: c812f77927cbdfa866ad23722bbbe67b5e5545986a3a5284535fa89d972a3777

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

# Conversions makes it easy to convert between units.
module Conversions
  mattr_accessor :conversions

  # Clear all previously registered conversions
  def self.clear
    self.conversions = {}
  end
  clear
  
  # Load all the default conversions shipped with the code
  def self.load_defaults
    load File.expand_path('../conversions/defaults.rb', __FILE__)
  end

  # 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_)
  def self.register(from, to, rate)
    conversions[from] ||= {}
    conversions[from][to] = rate
    conversions[to] ||= {}
    conversions[to][from] = 1.0 / rate
    Conversions.define_shortcut(from)
    Conversions.define_shortcut(to)
  end

  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

  module Ext
    # Convert from one unit to another.
    #
    # * _from_: The unit to convert from (ie. :miles, :stones, or :pints)
    # * _to_: The unit to convert to
    # * _options_:
    #   * :scale: The number of digits you want after the dot.
    def convert(from, to, options={})
      Conversions::Unit.new(self, from).to(to, options)
    end
  end
end

require 'conversions/unit'

Conversions.load_defaults
Numeric.send(:include, Conversions::Ext)

if defined?(ActiveRecord)
  require 'conversions/active_record_accessors'
  ActiveRecord::Base.send(:extend, Conversions::ActiveRecordAccessors)
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
seamusabshere-conversions-0.1.0 lib/conversions.rb
seamusabshere-conversions-1.4.0 lib/conversions.rb
seamusabshere-conversions-1.4.1 lib/conversions.rb
seamusabshere-conversions-1.4.2 lib/conversions.rb
conversions-1.4.3 lib/conversions.rb