Skip to content

Commit

Permalink
Merge pull request #65 from shivam091/5.12.0
Browse files Browse the repository at this point in the history
5.12.0
  • Loading branch information
shivam091 authored Nov 25, 2023
2 parents 594e1b0 + 1cdda25 commit 6b52fb1
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 14 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## [5.12.0](https://github.com/shivam091/unit_measurements/compare/v5.11.1...v5.12.0) - 2023-11-25

### What's new

- Added `MissingPrimitiveUnitError` error if user tries to convert measurement to
the primitive unit but it's not set for the unit group.
- Aliased `#+` method as `#add`.
- Aliased `#-` method as `#subtract`.
- Aliased `#*` method as `#times` and `#multiply`.
- Aliased `#/` method as `#divide`.

----------

## [5.11.1](https://github.com/shivam091/unit_measurements/compare/v5.11.0...v5.11.1) - 2023-11-16

### What's changed
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
unit_measurements (5.11.1)
unit_measurements (5.12.0)
activesupport (~> 7.0)

GEM
Expand Down
5 changes: 5 additions & 0 deletions lib/unit_measurements/arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Arithmetic
def +(other)
arithmetic_operation(other, :+)
end
alias_method :add, :+

# Subtracts the quantity of the other measurement or a numeric value from the
# quantity of the current measurement.
Expand All @@ -62,6 +63,7 @@ def +(other)
def -(other)
arithmetic_operation(other, :-)
end
alias_method :subtract, :-

# Multiplies the quantity of the current measurement by the quantity of the
# other measurement or a numeric value.
Expand All @@ -84,6 +86,8 @@ def *(other)
arithmetic_operation(other, :*)
end
alias_method :scale, :*
alias_method :times, :*
alias_method :multiply, :*

# Divides the quantity of the current measurement by the quantity of the other
# measurement or a numeric value.
Expand All @@ -105,6 +109,7 @@ def *(other)
def /(other)
arithmetic_operation(other, :/)
end
alias_method :divide, :/

# Raises the quantity of the current measurement to the power of the quantity of
# the other measurement or numeric value.
Expand Down
8 changes: 6 additions & 2 deletions lib/unit_measurements/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
module UnitMeasurements
# This is the base class for custom errors in the +UnitMeasurements+ module.
#
# @see UnitError
# @see ParseError
# @see PrimitiveUnitAlreadySetError
# @see BlankUnitError
# @see BlankQuantityError
# @see UnitAlreadyDefinedError
# @see UnitError
# @see MissingPrimitiveUnitError
# @see PrimitiveUnitAlreadySetError
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.1.0
class BaseError < StandardError; end
Expand Down Expand Up @@ -168,3 +171,4 @@ def configure
require "unit_measurements/errors/primitive_unit_already_set_error"
require "unit_measurements/errors/blank_quantity_error"
require "unit_measurements/errors/blank_unit_error"
require "unit_measurements/errors/missing_primitive_unit_error"
24 changes: 24 additions & 0 deletions lib/unit_measurements/errors/missing_primitive_unit_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module UnitMeasurements
# The +UnitMeasurements::MissingPrimitiveUnitError+ class represents an error
# that occurs when the primitive unit is not set for a unit group.
#
# This error is raised when a user attempts to convert a measurement to the
# primitive unit of a unit group that does not have a primitive unit defined.
#
# @see BaseError
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.12.0
class MissingPrimitiveUnitError < BaseError
# Initializes a new +MissingPrimitiveUnitError+ instance.
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.12.0
def initialize
super("The primitive unit is not set for the unit group.")
end
end
end
10 changes: 8 additions & 2 deletions lib/unit_measurements/measurement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,17 @@ def initialize(quantity, unit)
# A new +Measurement+ instance with the converted +quantity+ and
# +target unit+.
#
# @raise [MissingPrimitiveUnitError]
# if primitive unit is not set for the unit group.
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.0.0
def convert_to(target_unit, use_cache: false)
target_unit = if target_unit.to_s.eql?("primitive")
self.class.unit_group.primitive
primitive_unit = self.class.primitive

raise MissingPrimitiveUnitError if primitive_unit.nil?
primitive_unit
else
unit_from_unit_or_name!(target_unit)
end
Expand Down Expand Up @@ -474,7 +480,7 @@ def convert_quantity(quantity)
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.0.0
def unit_from_unit_or_name!(value)
value.is_a?(Unit) ? value : self.class.send(:unit_group).unit_for!(value)
value.is_a?(Unit) ? value : self.class.unit_for!(value)
end

# Calculates the conversion factor between the current unit and the target
Expand Down
2 changes: 1 addition & 1 deletion lib/unit_measurements/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

module UnitMeasurements
# Current stable version.
VERSION = "5.11.1"
VERSION = "5.12.0"
end
28 changes: 20 additions & 8 deletions spec/unit_measurements/measurement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@
expect(converted_length).to eq(base_length)
end

it "converts to a primitive unit" do
converted_length = other_length.convert_to("primitive")

expect(converted_length.quantity).to eq(1)
expect(converted_length.unit).to eq(m)
end

it "fetches conversion factor from cache when use_cache is true" do
cache.clear_cache
cache.set("m", "cm", 0.00001)
Expand All @@ -89,6 +82,25 @@

expect(converted_length.quantity).to eq(1e-5)
end

context "when the primitive unit is set to unit group" do
it "converts to a primitive unit" do
converted_length = other_length.convert_to("primitive")

expect(converted_length.quantity).to eq(1)
expect(converted_length.unit).to eq(m)
end
end

context "when the primitive unit is not set to unit group" do
it "raises UnitMeasurements::MissingPrimitiveUnitError" do
allow(UnitMeasurements::Length).to receive(:primitive).and_return(nil)

expect {
UnitMeasurements::Length.new(1, "m").convert_to("primitive")
}.to raise_error(UnitMeasurements::MissingPrimitiveUnitError, "The primitive unit is not set for the unit group.")
end
end
end

describe "#convert_to!" do
Expand Down Expand Up @@ -461,7 +473,7 @@
describe "#ratio" do
context "when the source unit and the target unit are strings" do
it "calculates the ratio" do
expect(UnitMeasurements::Length.ratio(:m, :cm)).to eq("0.01 m/cm")
expect(UnitMeasurements::Length.ratio("m", "cm")).to eq("0.01 m/cm")
end
end

Expand Down

0 comments on commit 6b52fb1

Please sign in to comment.