Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversions for US customary volumes to metric (liters and milliliters) #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/ruby-measurement/definitions/us_customary/volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
unit.convert_to(:'fl oz') { |value| value * 128.0 }
unit.convert_to(:tbsp) { |value| value * 256.0 }
unit.convert_to(:tsp) { |value| value * 768.0 }
unit.convert_to(:ml) { |value| value * 3785.411784 }
unit.convert_to(:l) { |value| value * 3.785411784 }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work in conjunction with this require approach. Some users do not want to load all unit definitions, but simply the ones they need. If someone loads just us_customary or us_customary/volume definitions, then these definitions won't work.

Perhaps these belong in a separate require, but let me think on it. It might just make sense to expose an interface for adding custom conversions within your own project as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding of your approach was to allow people to selectively require source units (i.e. what they were converting from) but, once that was required, to allow conversion to any destination units of the same type (e.g. volume ➡️ volume).

I think it’s quite common to want to convert units across the US/metric boundary and I’d hope this library would allow that functionality, even if that means offering a less granular require options, the value of which is less obvious, at least to me.

end

Measurement.define(:'qt.') do |unit|
Expand All @@ -18,6 +20,8 @@
unit.convert_to(:'fl oz') { |value| value * 32.0 }
unit.convert_to(:tbsp) { |value| value * 64.0 }
unit.convert_to(:tsp) { |value| value * 192.0 }
unit.convert_to(:ml) { |value| value * 946.352946 }
unit.convert_to(:l) { |value| value * 0.946352946 }
end

Measurement.define(:'pt.') do |unit|
Expand All @@ -28,6 +32,8 @@
unit.convert_to(:'fl oz') { |value| value * 16.0 }
unit.convert_to(:tbsp) { |value| value * 32.0 }
unit.convert_to(:tsp) { |value| value * 96.0 }
unit.convert_to(:ml) { |value| value * 473.176473 }
unit.convert_to(:l) { |value| value * 0.473176473 }
end

Measurement.define(:'c.') do |unit|
Expand All @@ -38,6 +44,8 @@
unit.convert_to(:'fl oz') { |value| value * 8.0 }
unit.convert_to(:tbsp) { |value| value * 16.0 }
unit.convert_to(:tsp) { |value| value * 48.0 }
unit.convert_to(:ml) { |value| value * 236.5882365 }
unit.convert_to(:l) { |value| value * 0.2365882365 }
end

Measurement.define(:'fl. oz.') do |unit|
Expand All @@ -48,6 +56,8 @@
unit.convert_to(:c) { |value| value / 8.0 }
unit.convert_to(:tbsp) { |value| value * 2.0 }
unit.convert_to(:tsp) { |value| value * 6.0 }
unit.convert_to(:ml) { |value| value * 29.5735295625 }
unit.convert_to(:l) { |value| value * 0.0295735295625 }
end

Measurement.define(:'tbsp.') do |unit|
Expand All @@ -58,6 +68,8 @@
unit.convert_to(:c) { |value| value / 16.0 }
unit.convert_to(:'fl oz') { |value| value / 2.0 }
unit.convert_to(:tsp) { |value| value * 3.0 }
unit.convert_to(:ml) { |value| value * 14.786764781 }
unit.convert_to(:l) { |value| value * 0.014786764781 }
end

Measurement.define(:'tsp.') do |unit|
Expand All @@ -68,4 +80,6 @@
unit.convert_to(:c) { |value| value / 48.0 }
unit.convert_to(:'fl oz') { |value| value / 6.0 }
unit.convert_to(:tbsp) { |value| value / 3.0 }
unit.convert_to(:ml) { |value| value * 4.9289215938 }
unit.convert_to(:l) { |value| value * 0.0049289215938 }
end
6 changes: 3 additions & 3 deletions spec/ruby-measurement/core_ext/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

describe 'with valid quantity and invalid unit' do
subject { '3 people' }
specify { expect { subject.to_measurement }.to raise_error }
specify { expect { subject.to_measurement }.to raise_error(ArgumentError) }
end

describe 'with invalid input' do
subject { 'foobar' }
specify { expect { subject.to_measurement }.to raise_error }
specify { expect { subject.to_measurement }.to raise_error(ArgumentError) }
end
end

Expand All @@ -31,7 +31,7 @@

describe 'with invalid unit' do
subject { 'person' }
specify { expect { subject.to_unit }.to raise_error }
specify { expect { subject.to_unit }.to raise_error(ArgumentError) }
end
end
end
2 changes: 1 addition & 1 deletion spec/ruby-measurement/core_ext/symbol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

describe 'with invalid unit' do
subject { :person }
specify { expect { subject.to_unit }.to raise_error }
specify { expect { subject.to_unit }.to raise_error(ArgumentError) }
end
end
end
56 changes: 56 additions & 0 deletions spec/ruby-measurement/definitions/us_customary/volume_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
it 'converts to teaspoons' do
expect(subject.convert_to(:tsp).quantity).to eq 768
end

it 'converts to milliliters' do
expect(subject.convert_to(:ml).quantity).to eq 3785.411784
end

it 'converts to liters' do
expect(subject.convert_to(:l).quantity).to eq 3.785411784
end
end

describe 'quarts' do
Expand Down Expand Up @@ -57,6 +65,14 @@
it 'converts to teaspoons' do
expect(subject.convert_to(:tsp).quantity).to eq 768
end

it 'converts to milliliters' do
expect(subject.convert_to(:ml).quantity).to eq 3785.411784
end

it 'converts to liters' do
expect(subject.convert_to(:l).quantity).to eq 3.785411784
end
end

describe 'pints' do
Expand Down Expand Up @@ -85,6 +101,14 @@
it 'converts to teaspoons' do
expect(subject.convert_to(:tsp).quantity).to eq 768
end

it 'converts to milliliters' do
expect(subject.convert_to(:ml).quantity).to eq 3785.411784
end

it 'converts to liters' do
expect(subject.convert_to(:l).quantity).to eq 3.785411784
end
end

describe 'cups' do
Expand Down Expand Up @@ -113,6 +137,14 @@
it 'converts to teaspoons' do
expect(subject.convert_to(:tsp).quantity).to eq 768
end

it 'converts to milliliters' do
expect(subject.convert_to(:ml).quantity).to eq 3785.411784
end

it 'converts to liters' do
expect(subject.convert_to(:l).quantity).to eq 3.785411784
end
end

describe 'fluid ounces' do
Expand Down Expand Up @@ -141,6 +173,14 @@
it 'converts to teaspoons' do
expect(subject.convert_to(:tsp).quantity).to eq 768
end

it 'converts to milliliters' do
expect(subject.convert_to(:ml).quantity).to eq 3785.411784
end

it 'converts to liters' do
expect(subject.convert_to(:l).quantity).to eq 3.785411784
end
end

describe 'tablespoons' do
Expand Down Expand Up @@ -169,6 +209,14 @@
it 'converts to teaspoons' do
expect(subject.convert_to(:tsp).quantity).to eq 768
end

it 'converts to milliliters' do
expect(subject.convert_to(:ml).quantity).to eq 3785.411783936
end

it 'converts to liters' do
expect(subject.convert_to(:l).quantity).to eq 3.785411783936
end
end

describe 'teaspoons' do
Expand Down Expand Up @@ -197,5 +245,13 @@
it 'converts to tablespoons' do
expect(subject.convert_to(:tbsp).quantity).to eq 256
end

it 'converts to milliliters' do
expect(subject.convert_to(:ml).quantity).to eq 3785.4117840384
end

it 'converts to liters' do
expect(subject.convert_to(:l).quantity).to eq 3.7854117840384
end
end
end
20 changes: 10 additions & 10 deletions spec/ruby-measurement/measurement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@

describe 'with invalid quantity' do
it 'raises exception' do
expect { subject.new('hi') }.to raise_exception
expect { subject.new('hi') }.to raise_error(ArgumentError)
end
end

describe 'with invalid unit' do
it 'raises exception' do
expect { subject.new(3, :finklebaum) }.to raise_exception
expect { subject.new(3, :finklebaum) }.to raise_error(ArgumentError)
end
end
end
Expand Down Expand Up @@ -146,7 +146,7 @@
end

it 'raises exception when undefined' do
expect { subject.parse('3 finklebaums') }.to raise_error
expect { subject.parse('3 finklebaums') }.to raise_error(ArgumentError)
end
end
end
Expand Down Expand Up @@ -174,11 +174,11 @@
end

it 'raises exception if unit exists and is not convertable' do
expect { measurement.convert_to(:inches) }.to raise_error
expect { measurement.convert_to(:inches) }.to raise_error(ArgumentError)
end

it 'raises exception if unit does not exist' do
expect { measurement.convert_to(:finklebaum) }.to raise_error
expect { measurement.convert_to(:finklebaum) }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -222,7 +222,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement + other }.to raise_error
expect { measurement + other }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -256,7 +256,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement - other }.to raise_error
expect { measurement - other }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -290,7 +290,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement * other }.to raise_error
expect { measurement * other }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -324,7 +324,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement / other }.to raise_error
expect { measurement / other }.to raise_error(ArgumentError)
end
end

Expand All @@ -336,7 +336,7 @@
end

it 'raises exception for non-numeric values' do
expect { measurement ** subject.new(3) }.to raise_error
expect { measurement ** subject.new(3) }.to raise_error(ArgumentError)
end
end

Expand Down