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

5.10.0 #60

Merged
merged 9 commits into from
Nov 7, 2023
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## [5.10.0](https://github.com/shivam091/unit_measurements/compare/v5.9.0...v5.10.0) - 2023-11-09

### What's new

- Added new method `#to_fs` to format the measurement.
- Aliased arithmetic method `#**` to `#pow` and `#^`.
- Aliased arithmetic method `#-@` to `#inverse` and `#negate`.
- Added `UnitMeasurements::BlankQuantityError` error if tried to initialize the `Measurement` with a blank quantity.
- Added `UnitMeasurements::BlankUnitError` error if tried to initialize the `Measurement` with a blank unit.

### What's deprecated

- `#format` method in favour of `#to_fs`.

----------

## [5.9.0](https://github.com/shivam091/unit_measurements/compare/v5.8.0...v5.9.0) - 2023-11-08

### What's new
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.9.0)
unit_measurements (5.10.0)
activesupport (~> 7.0)

GEM
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ UnitMeasurements::Length.new(1, "km")

This gem allows you to convert among units of same unit group. You can convert measurement to other unit using `#convert_to`
(aliased as `#to`, `#in`, and `#as`) or `#convert_to!` (aliased as `#to!`, `#in!`, and `#as!`) methods.
You can also chain call of `#convert_to` and `#convert_to!` methods.

These methods provide `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.

Expand All @@ -119,13 +120,6 @@ UnitMeasurements::Length.new(1, "cm").convert_to("primitive")
#=> 0.01 m
```

You can also chain call of `#convert_to` and `#convert_to!` methods as:

```ruby
UnitMeasurements::Length.new(100, "m").convert_to("ft").convert_to!("in", use_cache: true)
#=> 3937.00787401574071916010498688 in
```

**Parse string without having to split out the quantity and source unit:**

This method provides `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.
Expand Down Expand Up @@ -198,7 +192,7 @@ UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s")
You can check more about formatting along with their examples
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Formatter.html).

**Extract the unit and the quantity from measurement:**
**Extract the quantity and the unit from measurement:**

```ruby
length = UnitMeasurements::Length.new(1, "km")
Expand All @@ -208,6 +202,15 @@ length.unit
#=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
```

Unit object can be interrogated for a range of attributes:

```ruby
length.unit.aliases # Alternative names for the unit.
#=> #<Set: {"kilometer", "kilometers", "kilometre", "kilometres"}>
length.unit.conversion_factor # Conversion factor relative to primitive unit.
#=> 1000.0
```

**See primitive unit of the unit group:**

```ruby
Expand Down
4 changes: 4 additions & 0 deletions lib/unit_measurements/arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def /(other)
def **(other)
arithmetic_operation(other, :**)
end
alias_method :pow, :**
alias_method :^, :**

# Negates the quantity of the measurement.
#
Expand All @@ -151,6 +153,8 @@ def **(other)
def -@
self.class.new(-self.quantity, self.unit)
end
alias_method :inverse, :-@
alias_method :negate, :-@

# Checks whether the quantity of the measurement is nonzero.
#
Expand Down
2 changes: 2 additions & 0 deletions lib/unit_measurements/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ def configure
require "unit_measurements/errors/parse_error"
require "unit_measurements/errors/unit_already_defined_error"
require "unit_measurements/errors/primitive_unit_already_set_error"
require "unit_measurements/errors/blank_quantity_error"
require "unit_measurements/errors/blank_unit_error"
2 changes: 1 addition & 1 deletion lib/unit_measurements/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize
# @since 5.3.0
def use_cache=(use_cache)
unless [true, false].include?(use_cache)
raise BaseError, "Configuration#use_cache= only accepts true or false, but received #{use_cache}"
raise ArgumentError, "Configuration#use_cache= only accepts true or false, but received #{use_cache}"
end

@use_cache = use_cache
Expand Down
21 changes: 21 additions & 0 deletions lib/unit_measurements/errors/blank_quantity_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module UnitMeasurements
# The +UnitMeasurements::BlankQuantityError+ class represents an error that
# occurs when trying to initialize the +Measurement+ with a blank quantity.
#
# @see BaseError
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.10.0
class BlankQuantityError < BaseError
# Initializes a new +BlankQuantityError+ instance.
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.10.0
def initialize
super("Quantity cannot be blank.")
end
end
end
21 changes: 21 additions & 0 deletions lib/unit_measurements/errors/blank_unit_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module UnitMeasurements
# The +UnitMeasurements::BlankUnitError+ class represents an error that
# occurs when trying to initialize the +Measurement+ with a blank unit.
#
# @see BaseError
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.10.0
class BlankUnitError < BaseError
# Initializes a new +BlankUnitError+ instance.
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.10.0
def initialize
super("Unit cannot be blank.")
end
end
end
25 changes: 16 additions & 9 deletions lib/unit_measurements/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@ module Formatter
# containing placeholders for +quantity+ and +unit+.
DEFAULT_FORMAT = "%.2<quantity>f %<unit>s".freeze

# @deprecated This method has been deprecated in favour of {#to_fs}.
#
# This method is no longer recommended for use. Please use {#to_fs}
# instead.
def format(format = nil)
warn "DEPRECATION WARNING: The `format` method is deprecated and will be removed in upcoming release. Please use `to_fs` instead."
to_fs(format)
end

# Formats measurement to certain formatted string specified by +format+.
# If +format+ is not specified, it uses +DEFAULT_FORMAT+ for formatting the
# If +format+ is not specified, it uses {DEFAULT_FORMAT} for formatting the
# measurement.
#
# The format method allows for customization of the output format of a
# measurement. It uses format placeholders for +quantity+ and +unit+. If no
# custom format is provided, it will use the +DEFAULT_FORMAT+.
# The {#to_fs} method allows for customization of the output format of a
# measurement. It uses format placeholders for +quantity+ and +unit+.
#
# @example
# UnitMeasurements::Length.new(1, "m").to("in").format
# UnitMeasurements::Length.new(1, "m").to("in").to_fs
# => "39.37 in"
#
# UnitMeasurements::Length.new(1, "m").to("in").format("%.4<quantity>f %<unit>s")
# UnitMeasurements::Length.new(1, "m").to("in").to_fs("%.4<quantity>f %<unit>s")
# => "39.3701 in"
#
# @param [String, optional] format
Expand All @@ -41,10 +49,9 @@ module Formatter
#
# @see DEFAULT_FORMAT
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.1.0
def format(format = nil)
# @since 5.10.0
def to_fs(format = nil)
kwargs = {quantity: quantity, unit: unit.to_s}

(format || DEFAULT_FORMAT) % kwargs
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/unit_measurements/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def abs
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.6.0
def floor(ndigits =0)
def floor(ndigits = 0)
self.class.new(quantity.floor(ndigits), unit)
end

Expand All @@ -84,7 +84,7 @@ def floor(ndigits =0)
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.6.0
def ceil(ndigits =0)
def ceil(ndigits = 0)
self.class.new(quantity.ceil(ndigits), unit)
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/unit_measurements/measurement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ class Measurement
# @param [Numeric|String] quantity The quantity of the measurement.
# @param [String|Unit] unit The unit of the measurement.
#
# @raise [BaseError] If +quantity+ or +unit+ is blank.
# @raise [BlankQuantityError] If +quantity+ is blank.
# @raise [BlankUnitError] If +unit+ is blank.
#
# @see BaseError
# @see BlankQuantityError
# @see BlankUnitError
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.0.0
def initialize(quantity, unit)
raise BaseError, "Quantity cannot be blank." if quantity.blank?
raise BaseError, "Unit cannot be blank." if unit.blank?
raise BlankQuantityError if quantity.blank?
raise BlankUnitError if unit.blank?

@quantity = convert_quantity(quantity)
@unit = unit_from_unit_or_name!(unit)
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.9.0"
VERSION = "5.10.0"
end
2 changes: 1 addition & 1 deletion spec/unit_measurements/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
it "raises BaseError for invalid input" do
config = described_class.new

expect { config.use_cache = "invalid" }.to raise_error(UnitMeasurements::BaseError, /only accepts true or false/)
expect { config.use_cache = "invalid" }.to raise_error(ArgumentError, /only accepts true or false/)
end
end
end
16 changes: 13 additions & 3 deletions spec/unit_measurements/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@
describe "#format" do
subject { UnitMeasurements::Length.parse("1 m to cm") }

it "produces a deprecation warning" do
expect {
subject.format
}.to output(a_string_including("DEPRECATION WARNING")).to_stderr
end
end

describe "#to_fs" do
subject { UnitMeasurements::Length.parse("1 m to cm") }

context "when format is specified" do
it "formats measurement using specified format" do
expect(subject.format("%.1<quantity>f %<unit>s")).to eq("100.0 cm")
expect(subject.format("%.1<quantity>f")).to eq("100.0")
expect(subject.to_fs("%.1<quantity>f %<unit>s")).to eq("100.0 cm")
expect(subject.to_fs("%.1<quantity>f")).to eq("100.0")
end
end

context "when format is not specified" do
it "formats measurement using default format" do
expect(subject.format).to eq("100.00 cm")
expect(subject.to_fs).to eq("100.00 cm")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit_measurements/measurement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

describe "#initialize" do
it "raises an error for blank quantity" do
expect { UnitMeasurements::Length.new(nil, "m") }.to raise_error(UnitMeasurements::BaseError, "Quantity cannot be blank.")
expect { UnitMeasurements::Length.new("", "m") }.to raise_error(UnitMeasurements::BlankQuantityError, "Quantity cannot be blank.")
end

it "raises an error for blank unit" do
expect { UnitMeasurements::Length.new(1, nil) }.to raise_error(UnitMeasurements::BaseError, "Unit cannot be blank.")
expect { UnitMeasurements::Length.new(1, "") }.to raise_error(UnitMeasurements::BlankUnitError, "Unit cannot be blank.")
end

it "sets attributes correctly" do
Expand Down