lib/conversions/unit.rb in seamusabshere-conversions-1.2.2 vs lib/conversions/unit.rb in seamusabshere-conversions-1.3.0
- old
+ new
@@ -12,45 +12,21 @@
# Convert to a certain other unit.
#
# * _to_: The unit to convert to (ie. :kilometers)
# * _scale_: The number of digits behind the decimal point to you want to keep (Optional)
- def to(to, scale=nil)
+ def to(to, options = {})
+ scale = options.delete(:scale)
value = @value * self.class.exchange_rate(@from, to)
scale.nil? ? value : (value * (10 ** scale)).round / (10 ** scale).to_f
end
def self.exchange_rate(from_unit, to_unit) #:nodoc:
return 1 if from_unit == to_unit
- from = conversion[from_unit]
+ from = Conversions.conversions[from_unit]
raise ArgumentError, "Can't convert from `#{from}', unknown unit" if from.nil?
to = from[to_unit]
raise ArgumentError, "Can't convert from `#{from_unit}' to `#{to_unit}', unknown unit" if to.nil?
to
- end
-
- def self.conversion #:nodoc:
- map_conversion if !defined? @@conversion
- @@conversion
- end
-
- def self.register(from, to, value)
- CONVERSION.merge!(from.to_sym => { to.to_sym => value.to_f })
- map_conversion
- Numeric.send :include, Conversions::Ext
- end
-
- private
-
- def self.map_conversion
- @@conversion = {}
- CONVERSION.each do |from, conversion|
- conversion.each do |to, value|
- @@conversion[from] ||= {}
- @@conversion[from][to] = value
- @@conversion[to] ||= {}
- @@conversion[to][from] = 1.0 / value
- end
- end
end
end
end