Skip to content

Commit

Permalink
Handle exponents correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
halogenandtoast committed Sep 14, 2013
1 parent be14339 commit c933aee
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/alchemist/measurement.rb
Expand Up @@ -18,7 +18,7 @@ def per

def to type = nil
if type
convertor.send(type)
convertor.send(type, exponent)
else
convertor
end
Expand Down Expand Up @@ -52,7 +52,7 @@ def * multiplicand

def base unit_type
conversion_base = conversion_base_for(unit_type)
convert_to_base conversion_base
convert_to_base(conversion_base) * exponent
end

def to_s
Expand All @@ -68,7 +68,7 @@ def to_f
end

def <=>(other)
(self.to_f * exponent).to_f <=> other.to(unit_name).to_f
self.to_f <=> other.to(unit_name).to_f
end

def types
Expand Down
4 changes: 2 additions & 2 deletions lib/alchemist/measurement_convertor.rb
Expand Up @@ -9,7 +9,7 @@ def initialize from

def method_missing method, *args, &block
exponent, unit_name = PrefixParser.new.parse(method)
convert(from.shared_types(unit_name), unit_name, exponent)
convert(from.shared_types(unit_name), unit_name, args.first || exponent)
end

private
Expand All @@ -32,7 +32,7 @@ def convert_from_type(type, unit_name, exponent)
conversion_factor = library.conversion_base_for(type, unit_name)

value = value_from(conversion_base, conversion_factor)
Measurement.new(value, unit_name, exponent)
Measurement.new(value / from.exponent, unit_name, exponent)
end

def value_from(base, factor)
Expand Down
5 changes: 5 additions & 0 deletions spec/measurement_spec.rb
Expand Up @@ -67,6 +67,11 @@ module Alchemist
expect(1.6.meters.floor).to eq(1.meter)
end

it "handles exponents correctly" do
expect(1.kg.to.kg).to eq(1.kg)
expect(1.kg.to(:kg)).to eq(1.kg)
end

describe '#geospatial' do

it 'should convert angles to meters' do
Expand Down

0 comments on commit c933aee

Please sign in to comment.