Skip to content

Commit

Permalink
Merge pull request #61 from shivam091/5.11.0
Browse files Browse the repository at this point in the history
5.11.0
  • Loading branch information
shivam091 authored Nov 9, 2023
2 parents f726a09 + 25d58f3 commit 6aeb4c1
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 32 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [5.11.0](https://github.com/shivam091/unit_measurements/compare/v5.10.0...v5.11.0) - 2023-11-11

### What's new

- Added `#systems` method to return unit systems within the unit group.
- Added `#ratio` method to calculate the ratio between two units.

----------

## [5.10.0](https://github.com/shivam091/unit_measurements/compare/v5.9.0...v5.10.0) - 2023-11-09

### 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.10.0)
unit_measurements (5.11.0)
activesupport (~> 7.0)

GEM
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ UnitMeasurements::Length.parse("1:2 km to m")
#=> 500.0 m
```

**Calculate the ratio between two units:**

```ruby
UnitMeasurements::Length.ratio("in", "ft")
#=> "12.0 in/ft"
```

**Formatting measurement:**

If you want to format the measurement to certain format, you can use `#to_fs` method.
Expand Down Expand Up @@ -239,6 +246,13 @@ UnitMeasurements::Length.unit_names_with_aliases
#=> ["\"", "'", "feet", "foot", "ft", "in", "inch", "inches", "m", "meter", "meters", "metre", "metres", "mi", "mile", "miles", "yard", "yards", "yd"]
```

**See list of unit systems defined within the unit group:**

```ruby
UnitMeasurements::Length.systems
#=> ["metric", "imperial", "us_customary", "astronomical"]
```

**See list of units within the unit system:**

You can use `#units_for` or `#units_for!` methods to find units within the unit system.
Expand Down
36 changes: 35 additions & 1 deletion lib/unit_measurements/measurement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class << self
def_delegators :unit_group, :primitive, :units, :cache_file, :unit_names,
:unit_with_name_and_aliases, :unit_names_with_aliases,
:unit_for, :unit_for!, :defined?, :unit_or_alias?, :[],
:units_for, :units_for!
:units_for, :units_for!, :systems

# Parses an input string and returns a +Measurement+ instance depending on
# the input string. This method first normalizes the +input+ internally,
Expand Down Expand Up @@ -368,6 +368,40 @@ def clear_cache
cached.clear_cache
end

# Calculates the ratio between two units.
#
# This method takes a source unit and a target unit, and returns the ratio
# between them as a string representation.
#
# @example Calculating the ratio between 'in' and 'ft':
# UnitMeasurements::Length.ratio("in", "ft")
# => "12.0 in/ft"
#
# UnitMeasurements::Length.ratio(UnitMeasurements::Length.unit_for("in"), "ft")
# => "12.0 in/ft"
#
# @param [Unit|String|Symbol] source_unit
# The source unit for the ratio calculation.
# @param [Unit|String|Symbol] target_unit
# The target unit for the ratio calculation.
#
# @return [String] The ratio between the source and target units.
#
# @raise [UnitError]
# If either the source unit or the target unit is not found in the unit group.
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.11.0
def ratio(source_unit, target_unit)
source_unit = source_unit.is_a?(Unit) ? source_unit : unit_for!(source_unit)
target_unit = target_unit.is_a?(Unit) ? target_unit : unit_for!(target_unit)

source_quantity = 1
target_quantity = new(source_quantity, target_unit).convert_to(source_unit).quantity

"#{target_quantity} #{source_unit}/#{target_unit}"
end

private

# @private
Expand Down
16 changes: 16 additions & 0 deletions lib/unit_measurements/unit_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ def units_for!(system_name)
system_units
end

# Returns an array of unit system names defined within the unit group.
#
# It scans through the units and compiles a list of unique system names.
#
# @example
# UnitMeasurements::Length.systems
# => ["metric", "imperial", "us_customary", "astronomical"]
#
# @return [Array<String>] An array of unit system names.
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 5.11.0
def systems
units.map { _1.system }.uniq
end

private

# @private
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.10.0"
VERSION = "5.11.0"
end
72 changes: 43 additions & 29 deletions spec/unit_measurements/measurement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@
context "when only quantity and source unit is provided" do
it "parses scientific notations" do
measurement = UnitMeasurements::Length.parse("4.3e12 m")
expect(measurement.quantity).to eq(4.3e12)
expect(measurement.quantity).to eq(4.3e+12)
expect(measurement.unit).to eq(m)

measurement = UnitMeasurements::Length.parse("4.3e12 m")
expect(measurement.quantity).to eq(4.3e12)
expect(measurement.quantity).to eq(4.3e+12)
expect(measurement.unit).to eq(m)

measurement = UnitMeasurements::Length.parse("4.3e12m")
expect(measurement.quantity).to eq(4.3e12)
expect(measurement.quantity).to eq(4.3e+12)
expect(measurement.unit).to eq(m)

measurement = UnitMeasurements::Length.new("4.3e12", "m")
expect(measurement.quantity).to eq(4.3e12)
expect(measurement.quantity).to eq(4.3e+12)
expect(measurement.unit).to eq(m)

expect(measurement.inspect).to eq("4300000000000.0 m")
Expand Down Expand Up @@ -307,27 +307,27 @@
context "when quantity, source unit, and target unit is provided" do
it "parses scientific notations" do
measurement = UnitMeasurements::Length.parse("4.3e12 m to cm")
expect(measurement.quantity).to eq(0.43e15)
expect(measurement.quantity).to eq(4.3e+14)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("4.3e12 m to cm")
expect(measurement.quantity).to eq(0.43e15)
expect(measurement.quantity).to eq(4.3e+14)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("4.3e12m to cm")
expect(measurement.quantity).to eq(0.43e15)
expect(measurement.quantity).to eq(4.3e+14)
expect(measurement.unit).to eq(cm)

expect(measurement.inspect).to eq("430000000000000.0 cm")
end

it "parses fractions" do
measurement = UnitMeasurements::Length.parse("1/4 m to cm")
expect(measurement.quantity).to eq(0.25e2)
expect(measurement.quantity).to eq(25)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("1/4 m to cm")
expect(measurement.quantity).to eq(0.25e2)
expect(measurement.quantity).to eq(25)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("1/4m to cm")
Expand All @@ -339,85 +339,85 @@

it "parses fractions with special characters" do
measurement = UnitMeasurements::Length.parse("⅜ m to cm")
expect(measurement.quantity).to eq(0.375e2)
expect(measurement.quantity).to eq(37.5)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("⅜ m to cm")
expect(measurement.quantity).to eq(0.375e2)
expect(measurement.quantity).to eq(37.5)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("⅜m to cm")
expect(measurement.quantity).to eq(0.375e2)
expect(measurement.quantity).to eq(37.5)
expect(measurement.unit).to eq(cm)
end

it "parses mixed fractions" do
measurement = UnitMeasurements::Length.parse("3 2/5 m to cm")
expect(measurement.quantity).to eq(0.34e3)
expect(measurement.quantity).to eq(340)
expect(measurement.unit).to eq(cm)

expect(measurement.inspect).to eq("340.0 cm")
end

it "parses mixed fractions with special characters" do
measurement = UnitMeasurements::Length.parse("3⅜ m to cm")
expect(measurement.quantity).to eq(0.3375e3)
expect(measurement.quantity).to eq(337.5)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("3 ⅜ m to cm")
expect(measurement.quantity).to eq(0.3375e3)
expect(measurement.quantity).to eq(337.5)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("3⅜ m to cm")
expect(measurement.quantity).to eq(0.3375e3)
expect(measurement.quantity).to eq(337.5)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("3 ⅜ m to cm")
expect(measurement.quantity).to eq(0.3375e3)
expect(measurement.quantity).to eq(337.5)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("3⅜m to cm")
expect(measurement.quantity).to eq(0.3375e3)
expect(measurement.quantity).to eq(337.5)
expect(measurement.unit).to eq(cm)
end

it "parses exponent symbols" do
measurement = UnitMeasurements::Length.parse("10⁸ m to cm")
expect(measurement.quantity).to eq(0.108e5)
expect(measurement.quantity).to eq(10800)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("10e³ m to cm")
expect(measurement.quantity).to eq(0.1e7)
expect(measurement.quantity).to eq(1000000)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("10e⁺³ m to cm")
expect(measurement.quantity).to eq(0.1e7)
expect(measurement.quantity).to eq(1000000)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("10e+³ m to cm")
expect(measurement.quantity).to eq(0.1e7)
expect(measurement.quantity).to eq(1000000)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("10e⁻³ m to cm")
expect(measurement.quantity).to eq(0.1e1)
expect(measurement.quantity).to eq(1)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("10e-³ m to cm")
expect(measurement.quantity).to eq(0.1e1)
expect(measurement.quantity).to eq(1)
expect(measurement.unit).to eq(cm)
end

it "parses decimals" do
measurement = UnitMeasurements::Length.parse("2.1 m to cm")
expect(measurement.quantity).to eq(0.21e3)
expect(measurement.quantity).to eq(210)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("2.1 m to cm")
expect(measurement.quantity).to eq(0.21e3)
expect(measurement.quantity).to eq(210)
expect(measurement.unit).to eq(cm)

measurement = UnitMeasurements::Length.parse("2.1m to cm")
expect(measurement.quantity).to eq(0.21e3)
expect(measurement.quantity).to eq(210)
expect(measurement.unit).to eq(cm)

expect(measurement.inspect).to eq("210.0 cm")
Expand All @@ -433,15 +433,15 @@

it "parses integers" do
measurement = UnitMeasurements::Length.parse("1 m to cm")
expect(measurement.quantity).to eq(0.1e3)
expect(measurement.quantity).to eq(100)
expect(measurement.unit).to eq(cm)

expect(measurement.inspect).to eq("100.0 cm")
end

it "parses ratios" do
measurement = UnitMeasurements::Length.parse("1:4 m to cm")
expect(measurement.quantity).to eq(0.25e2)
expect(measurement.quantity).to eq(25)
expect(measurement.unit).to eq(cm)
end
end
Expand All @@ -457,4 +457,18 @@
expect(cache.get("km", "m")).to be_nil
end
end

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")
end
end

context "when the source unit and the target unit are instance of Unit" do
it "calculates the ratio" do
expect(UnitMeasurements::Length.ratio(m, cm)).to eq("0.01 m/cm")
end
end
end
end
6 changes: 6 additions & 0 deletions spec/unit_measurements/unit_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,10 @@
end
end
end

describe "#systems" do
it "returns arrays of unit system names" do
expect(unit_group.systems).to eq(["metric"])
end
end
end

0 comments on commit 6aeb4c1

Please sign in to comment.