Skip to content

Commit

Permalink
Merge pull request #7 from shivam091/1.5.0
Browse files Browse the repository at this point in the history
1.5.0
  • Loading branch information
shivam091 authored Sep 17, 2023
2 parents 2c21af5 + f4c50d1 commit 0553895
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 56 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [1.5.0](https://github.com/shivam091/unit_measurements/compare/v1.4.0...v1.5.0) - 2023-08-18

## What's fixed

- Fixed precision in `Measurement#quantity` method

----------

## [1.4.0](https://github.com/shivam091/unit_measurements/compare/v1.3.0...v1.4.0) - 2023-08-17

### 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 (1.4.0)
unit_measurements (1.5.0)
activesupport (~> 7.0)

GEM
Expand Down
70 changes: 35 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ You can use `#convert_to` as:

```ruby
UnitMeasurements::Weight.new(1, :kg).convert_to(:g)
#=> 1000 g
#=> 1000.0 g
```

If you want to modify measurement object itself, you can use `#convert_to!` method as:

```ruby
UnitMeasurements::Weight.new(1, :kg).convert_to!(:g)
#=> 1000 g
#=> 1000.0 g
```

You can also chain call of `#convert_to` and `#convert_to!` methods as:
Expand All @@ -100,18 +100,18 @@ UnitMeasurements::Weight.new(1, :kg).convert_to(:g).convert_to(:t).convert_to!(:

```ruby
UnitMeasurements::Weight.parse("1 kg")
#=> 1 kg
#=> 1.0 kg
```

**Parse string that mentions quantity, source unit, and target unit:**

```ruby
UnitMeasurements::Weight.parse("1 kg to g")
#=> 1000 g
#=> 1000.0 g
UnitMeasurements::Weight.parse("1 kg as g")
#=> 1000 g
#=> 1000.0 g
UnitMeasurements::Weight.parse("1 kg in g")
#=> 1000 g
#=> 1000.0 g
```

**Parse rational numbers, source unit, and (or) target unit:**
Expand Down Expand Up @@ -144,58 +144,58 @@ UnitMeasurements::Weight.parse("2+3i kg to g")

```ruby
UnitMeasurements::Weight.new(BigDecimal(2), :kg).convert_to(:g)
#=> 2000 g
#=> 2000.0 g
UnitMeasurements::Weight.new(0.2e1, :kg).convert_to(:g)
#=> 2000 g
#=> 2000.0 g
UnitMeasurements::Weight.parse("0.2e1 kg").convert_to(:g)
#=> 2000 g
#=> 2000.0 g
UnitMeasurements::Weight.parse("0.2e1 kg to g")
#=> 2000 g
#=> 2000.0 g
```

**Parse ratios, source unit, and (or) target unit:**

```ruby
UnitMeasurements::Weight.new("1:2", :kg).convert_to(:g)
#=> 500 g
#=> 500.0 g
UnitMeasurements::Weight.parse("1:2 kg").convert_to(:g)
#=> 500 g
#=> 500.0 g
UnitMeasurements::Weight.parse("1:2 kg to g")
#=> 500 g
#=> 500.0 g
```

**Parse fractional notations, source unit, and (or) target unit:**

```ruby
UnitMeasurements::Weight.new("1/2", :kg).convert_to(:g)
#=> 500 g
#=> 500.0 g
UnitMeasurements::Weight.parse("1/2 kg").convert_to(:g)
#=> 500 g
#=> 500.0 g
UnitMeasurements::Weight.parse("1/2 kg to g")
#=> 500 g
#=> 500.0 g
UnitMeasurements::Weight.new("½", :kg).convert_to(:g)
#=> 500 g
#=> 500.0 g
UnitMeasurements::Weight.parse("½ kg").convert_to(:g)
#=> 500 g
#=> 500.0 g
UnitMeasurements::Weight.parse("½ kg to g")
#=> 500 g
#=> 500.0 g
```

**Parse mixed fractional notations, source unit, and (or) target unit:**

```ruby
UnitMeasurements::Weight.new("2 1/2", :kg).convert_to(:g)
#=> 2500 g
#=> 2500.0 g
UnitMeasurements::Weight.parse("2 1/2 kg").convert_to(:g)
#=> 2500 g
#=> 2500.0 g
UnitMeasurements::Weight.parse("2 1/2 kg to g")
#=> 2500 g
#=> 2500.0 g
UnitMeasurements::Weight.new("2 ½", :kg).convert_to(:g)
#=> 2500 g
#=> 2500.0 g
UnitMeasurements::Weight.parse("2 ½ kg").convert_to(:g)
#=> 2500 g
#=> 2500.0 g
UnitMeasurements::Weight.parse("2 ½ kg to g")
#=> 2500 g
#=> 2500.0 g
```

Supported special characters for fractional notations are `¼`, `½`, `¾`, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``, ``.
Expand All @@ -204,17 +204,17 @@ Supported special characters for fractional notations are `¼`, `½`, `¾`, `⅓

```ruby
UnitMeasurements::Weight.new("2e+2", :kg).convert_to(:g)
#=> 200000 g
#=> 200000.0 g
UnitMeasurements::Weight.parse("2e² kg").convert_to(:g)
#=> 200000 g
#=> 200000.0 g
UnitMeasurements::Weight.parse("2e+2 kg to g")
#=> 200000 g
#=> 200000.0 g
UnitMeasurements::Weight.new("2e⁺²", :kg).convert_to(:g)
#=> 200000 g
#=> 200000.0 g
UnitMeasurements::Weight.parse("2e⁺2 kg").convert_to(:g)
#=> 200000 g
#=> 200000.0 g
UnitMeasurements::Weight.parse("2e⁻² kg to g")
#=> 20 g
#=> 20.0 g
```

Supported special characters for exponents are ``, `¹`, `²`, `³`, ``, ``, ``, ``, ``, ``, ``, ``.
Expand All @@ -236,9 +236,9 @@ UnitMeasurements::Weight.parse("2 kg").to(:st).format("%.4<quantity>f")
**Extract the unit and the quantity from measurement:**

```ruby
weight = UnitMeasurements::Weight.new(1.0, :kg)
weight = UnitMeasurements::Weight.new(1, :kg)
weight.quantity
#=> 0.1e1
#=> 1
weight.unit
#=> #<UnitMeasurements::Unit: kg (kilogram, kilogramme, kilogrammes, kilograms)>
```
Expand Down Expand Up @@ -324,8 +324,8 @@ UnitMeasurements::Weight.parse("1 kg") >= UnitMeasurements::Weight.parse("0.5 kg

### Arithmetic

You have the ability to perform arithmetic operations on measurements with the same or
different units within the same unit group. You can perform arithmetic operations on
You have ability to perform arithmetic operations on measurements with the same or
different units within a same unit group. You can perform arithmetic operations on
measurement by either other compatible measurement or number.

**Methods:**
Expand Down
21 changes: 10 additions & 11 deletions lib/unit_measurements/measurement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ def inspect(dump: false)
end

def to_s
"#{humanized_quantity} #{unit.to_s}"
"#{quantity} #{unit}"
end

def quantity
case @quantity
when Rational
@quantity.denominator == 1 ? @quantity.numerator : @quantity
else
@quantity
end
end

class << self
Expand Down Expand Up @@ -92,15 +101,5 @@ def convert_quantity(quantity)
def unit_from_unit_or_name!(value)
value.is_a?(Unit) ? value : self.class.unit_group.unit_for!(value)
end

def humanized_quantity
case quantity
when Complex
quantity
when Numeric
num = quantity.to_r
num.denominator == 1 ? num.numerator.to_s : num.to_f.to_s
end
end
end
end
2 changes: 1 addition & 1 deletion lib/unit_measurements/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# -*- warn_indent: true -*-

module UnitMeasurements
VERSION = "1.4.0"
VERSION = "1.5.0"
end
14 changes: 7 additions & 7 deletions spec/unit_measurements/measurement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
expect(measurement.quantity).to eq(4.3e12)
expect(measurement.unit).to eq(m)

expect(measurement.inspect).to eq("4300000000000 m")
expect(measurement.inspect).to eq("4300000000000.0 m")
end

it "parses fractions" do
Expand Down Expand Up @@ -267,7 +267,7 @@
expect(measurement.quantity).to eq(1)
expect(measurement.unit).to eq(m)

expect(measurement.inspect).to eq("1 m")
expect(measurement.inspect).to eq("1.0 m")
end

it "parses ratios" do
Expand Down Expand Up @@ -299,7 +299,7 @@
expect(measurement.quantity).to eq(0.43e15)
expect(measurement.unit).to eq(cm)

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

it "parses fractions" do
Expand All @@ -315,7 +315,7 @@
expect(measurement.quantity).to eq(25)
expect(measurement.unit).to eq(cm)

expect(measurement.inspect).to eq("25 cm")
expect(measurement.inspect).to eq("25.0 cm")
end

it "parses fractions with special characters" do
Expand All @@ -337,7 +337,7 @@
expect(measurement.quantity).to eq(0.34e3)
expect(measurement.unit).to eq(cm)

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

it "parses mixed fractions with special characters" do
Expand Down Expand Up @@ -401,7 +401,7 @@
expect(measurement.quantity).to eq(0.21e3)
expect(measurement.unit).to eq(cm)

expect(measurement.inspect).to eq("210 cm")
expect(measurement.inspect).to eq("210.0 cm")
end

it "parses complexes" do
Expand All @@ -417,7 +417,7 @@
expect(measurement.quantity).to eq(0.1e3)
expect(measurement.unit).to eq(cm)

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

it "parses ratios" do
Expand Down
2 changes: 1 addition & 1 deletion spec/unit_measurements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

RSpec.describe UnitMeasurements do
it "has a valid version number" do
expect(UnitMeasurements::VERSION).to eq("1.4.0")
expect(UnitMeasurements::VERSION).to eq("1.5.0")
end
end

0 comments on commit 0553895

Please sign in to comment.