From e19e71497c69f4275a84a8ef5a452d9f3b8a3ed9 Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Fri, 24 Nov 2023 22:20:48 +0530 Subject: [PATCH 1/4] Aliased arithmetic methods --- lib/unit_measurements/arithmetic.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/unit_measurements/arithmetic.rb b/lib/unit_measurements/arithmetic.rb index 05184b8..f147226 100644 --- a/lib/unit_measurements/arithmetic.rb +++ b/lib/unit_measurements/arithmetic.rb @@ -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. @@ -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. @@ -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. @@ -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. From 8f21099828b33ef440b41f2b86dde5e15d64da8f Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Sat, 25 Nov 2023 18:53:09 +0530 Subject: [PATCH 2/4] Added `MissingPrimitiveUnitError` class --- lib/unit_measurements/base.rb | 8 ++++-- .../errors/missing_primitive_unit_error.rb | 24 ++++++++++++++++ lib/unit_measurements/measurement.rb | 10 +++++-- spec/unit_measurements/measurement_spec.rb | 28 +++++++++++++------ 4 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 lib/unit_measurements/errors/missing_primitive_unit_error.rb diff --git a/lib/unit_measurements/base.rb b/lib/unit_measurements/base.rb index 3cdd108..bf3cff6 100644 --- a/lib/unit_measurements/base.rb +++ b/lib/unit_measurements/base.rb @@ -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 @@ -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" diff --git a/lib/unit_measurements/errors/missing_primitive_unit_error.rb b/lib/unit_measurements/errors/missing_primitive_unit_error.rb new file mode 100644 index 0000000..63e460c --- /dev/null +++ b/lib/unit_measurements/errors/missing_primitive_unit_error.rb @@ -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 diff --git a/lib/unit_measurements/measurement.rb b/lib/unit_measurements/measurement.rb index 49b617c..856455d 100644 --- a/lib/unit_measurements/measurement.rb +++ b/lib/unit_measurements/measurement.rb @@ -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 @@ -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 diff --git a/spec/unit_measurements/measurement_spec.rb b/spec/unit_measurements/measurement_spec.rb index 444ac0d..7ecd8e8 100644 --- a/spec/unit_measurements/measurement_spec.rb +++ b/spec/unit_measurements/measurement_spec.rb @@ -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) @@ -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 @@ -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 From a069e1811d2164311e604d0e5c7ca803ccb2551a Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Sat, 25 Nov 2023 18:56:01 +0530 Subject: [PATCH 3/4] Updated changelog --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b29dc65..4ef0dca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 1cdda25d29159e39709fc7b1446dbcf57aaa5bc8 Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Sat, 25 Nov 2023 18:56:34 +0530 Subject: [PATCH 4/4] Bump version to `5.12.0` --- Gemfile.lock | 2 +- lib/unit_measurements/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3742bcf..7ce291e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - unit_measurements (5.11.1) + unit_measurements (5.12.0) activesupport (~> 7.0) GEM diff --git a/lib/unit_measurements/version.rb b/lib/unit_measurements/version.rb index 6d105fa..8636abe 100644 --- a/lib/unit_measurements/version.rb +++ b/lib/unit_measurements/version.rb @@ -4,5 +4,5 @@ module UnitMeasurements # Current stable version. - VERSION = "5.11.1" + VERSION = "5.12.0" end