-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from shivam091/1.4.0
1.4.0
- Loading branch information
Showing
9 changed files
with
218 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
unit_measurements (1.3.0) | ||
unit_measurements (1.4.0) | ||
activesupport (~> 7.0) | ||
|
||
GEM | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module UnitMeasurements | ||
module Arithmetic | ||
# Adds the other measurement quantity or number to the measurement. | ||
# | ||
# @param [Numeric or Measurement] other | ||
# | ||
# @example | ||
# UnitMeasurements::Weight.new(1, :kg) + UnitMeasurements::Weight.new(1, :g) | ||
# => 1.001 kg | ||
# | ||
# @return [Measurement] | ||
def +(other) | ||
arithmetic_operation(other, :+) | ||
end | ||
|
||
# Subtracts the other measurement quantity or number from the measurement. | ||
# | ||
# @param [Numeric or Measurement] other | ||
# | ||
# @example | ||
# UnitMeasurements::Weight.new(2, :kg) - 1 | ||
# => 1 kg | ||
# | ||
# @return [Measurement] | ||
def -(other) | ||
arithmetic_operation(other, :-) | ||
end | ||
|
||
# Multiplies the measurement quantity by other measurement quantity or number. | ||
# | ||
# @param [Numeric or Measurement] other | ||
# | ||
# @example | ||
# UnitMeasurements::Weight.new(2, :kg) * 2 | ||
# => 4 kg | ||
# | ||
# @return [Measurement] | ||
def *(other) | ||
arithmetic_operation(other, :*) | ||
end | ||
|
||
# Divides the measurement quantity by other measurement quantity or number. | ||
# | ||
# @param [Numeric or Measurement] other | ||
# | ||
# @example | ||
# UnitMeasurements::Weight.new(4, :kg) / UnitMeasurements::Weight.new(2, :kg) | ||
# => 2 kg | ||
# | ||
# @return [Measurement] | ||
def /(other) | ||
arithmetic_operation(other, :/) | ||
end | ||
|
||
private | ||
|
||
def coerce(other) | ||
case other | ||
when Numeric then [self.class.new(other, self.unit), self] | ||
when self.class then [other, self] | ||
else raise TypeError, "Cannot coerce #{other.class} to #{self.class}" | ||
end | ||
end | ||
|
||
def arithmetic_operation(other, operator) | ||
other, _ = coerce(other) | ||
self.class.new(self.quantity.public_send(operator, other.convert_to(self.unit).quantity), self.unit) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
# -*- warn_indent: true -*- | ||
|
||
module UnitMeasurements | ||
VERSION = "1.3.0" | ||
VERSION = "1.4.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
# spec/unit_measurements/arithmetic_spec.rb | ||
|
||
RSpec.describe UnitMeasurements::Arithmetic do | ||
subject { UnitMeasurements::Length } | ||
let(:m) { subject.unit_for!(:m) } | ||
let(:cm) { subject.unit_for!(:cm) } | ||
|
||
describe "#+" do | ||
it "must add numbers" do | ||
measurement = subject.new(3.5, :m) + 2 | ||
expect(measurement.quantity).to eq(5.5) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must add similar units" do | ||
measurement = (subject.new(3.5, :m) + subject.new(2, :m)) | ||
expect(measurement.quantity).to eq(5.5) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must add unsimilar units" do | ||
measurement = (subject.new(3.5, :cm) + subject.new(2, :m)) | ||
expect(measurement.quantity).to eq(203.5) | ||
expect(measurement.unit).to eq(cm) | ||
end | ||
end | ||
|
||
describe "#-" do | ||
it "must subtract numbers" do | ||
measurement = (subject.new(3.5, :m) - 2) | ||
expect(measurement.quantity).to eq(1.5) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must subtract unsimilar units" do | ||
measurement = (subject.new(3.5, :m) - subject.new(2, :m)) | ||
expect(measurement.quantity).to eq(1.5) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must subtract similar units" do | ||
measurement = (subject.new(3.5, :m) - subject.new(2, :cm)) | ||
expect(measurement.quantity).to eq(3.48) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
end | ||
|
||
describe "#*" do | ||
it "must multiply by numbers" do | ||
measurement = (subject.new(3.5, :m) * 2) | ||
expect(measurement.quantity).to eq(7) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must multiply by similar units" do | ||
measurement = (subject.new(3.5, :m) * subject.new(2, :m)) | ||
expect(measurement.quantity).to eq(7) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must multiply by unsimilar units" do | ||
measurement = (subject.new(3.5, :m) * subject.new(2, :cm)) | ||
expect(measurement.quantity).to eq(0.07) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
end | ||
|
||
describe "#/" do | ||
it "must divide by numbers" do | ||
measurement = (subject.new(4, :m) / 2) | ||
expect(measurement.quantity).to eq(2) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must divide by similar units" do | ||
measurement = (subject.new(4, :m) / subject.new(2, :m)) | ||
expect(measurement.quantity).to eq(2) | ||
expect(measurement.unit).to eq(m) | ||
end | ||
|
||
it "must divide by unsimilar units" do | ||
measurement = (subject.new(36, :cm) / subject.new(2, :m)) | ||
expect(measurement.quantity).to eq(0.18) | ||
expect(measurement.unit).to eq(cm) | ||
end | ||
end | ||
|
||
describe "#coerce" do | ||
let(:meter) { subject.new(1, :m) } | ||
|
||
it "must coerce numbers" do | ||
expect((meter * 5).quantity).to eq(5) | ||
end | ||
|
||
it "must coerce measurements" do | ||
expect((meter * subject.new(1, :cm)).quantity).to eq(0.01) | ||
end | ||
|
||
it "should raise an error for other than numbers and measurement instance" do | ||
expect { meter * "foo" }.to raise_error(TypeError) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters