unit_measurements 1.5.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aac3030ed1c46bb4bdfe831bc9bedb29ad2a8b0e28595ba4726b2a3ff5e24791
4
- data.tar.gz: 9179c7b41ef7e84b6864ef98ccb45210ab34b2488830aa3de5552659b7f39039
3
+ metadata.gz: bbb7978f79ac8f4fac738ef87936cf1bf20397c74772c35120205aed22d1ac1c
4
+ data.tar.gz: 1b99cb2665a071d0e721f78cabad86fc50a39e2eda8349bd5813ba80c48afe89
5
5
  SHA512:
6
- metadata.gz: 5435fba092be97c1c8407b917857c1990db07d1aa1729653ecf14519d98dcec2a4c5e5ad0c4997586eb2f72b17d1099506068aa99eb592ee35ddd8da4a2f8ba7
7
- data.tar.gz: 5938944868b729168d49d1f5e7e888029931bb17237fd72c66980bf6b47d7fe58f8eefdd94f1927652d9b5d1e3e480e8df746f5c7607995390ea7caeec186634
6
+ metadata.gz: 27da14501c804050def9568138cfcab08b7b81eeab9d47ba3c23117534d2f32f33ed42260c9199d95f1c5cc71e715d1844cc493be34e347989c9fb71932fb058
7
+ data.tar.gz: e6c5e47ab28b199599cc7b86b748205d3124c52fbbdfb83a38849876177764e280ce6d324336908ebdff0eff6a3acdb98b8cf8a8bc94ef8f325deca2d378f560
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## [1.7.0](https://github.com/shivam091/unit_measurements/compare/v1.6.0...v1.7.0) - 2023-08-20
2
+
3
+ ### What's new
4
+
5
+ - Added support to convert the measurements to other numeric types viz.,
6
+ `Integer`, `BigDecimal`, `Rational`, `Complex`, and `Float`.
7
+
8
+ ----------
9
+
10
+ ## [1.6.0](https://github.com/shivam091/unit_measurements/compare/v1.5.1...v1.6.0) - 2023-08-19
11
+
12
+ ### What's new
13
+
14
+ - Added support to perform mathematical operations on the measurements.
15
+
16
+ ----------
17
+
1
18
  ## [1.5.1](https://github.com/shivam091/unit_measurements/compare/v1.5.0...v1.5.1) - 2023-08-18
2
19
 
3
20
  ### What's updated
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.5.1)
4
+ unit_measurements (1.7.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -348,6 +348,45 @@ UnitMeasurements::Weight.new(4, :kg) / UnitMeasurements::Weight.new(2, :kg)
348
348
  #=> 2 kg
349
349
  ```
350
350
 
351
+ ### Math
352
+
353
+ You can perform mathematical operations on the measurements.
354
+
355
+ **Methods:**
356
+ 1. `#round` - Rounds quantity of the measurement. If `ndigits` is not specified, quantity is rounded to `Integer`.
357
+ 2. `#abs` - Returns absolute value of the measurement quantity.
358
+ 3. `#floor` - Rounds quantity of the measurement to next lower integer.
359
+ 4. `#ceil` - Rounds quantity of the measurement to next higher integer.
360
+
361
+ ```ruby
362
+ UnitMeasurements::Weight.new(1, :g).convert_to(:st).round(4)
363
+ #=> 0.0002 st
364
+ UnitMeasurements::Length.new(-17.625, :m).abs
365
+ #=> 17.625 m
366
+ UnitMeasurements::Length.new(17.625, :m).floor
367
+ #=> 17 m
368
+ UnitMeasurements::Length.new(17.625, :m).ceil
369
+ #=> 18 m
370
+ ```
371
+
372
+ ### Conversions
373
+
374
+ You can convert measurement quantity directly to other numeric types viz.
375
+ `Integer`, `BigDecimal`, `Rational`, `Complex`, and `Float`.
376
+
377
+ ```ruby
378
+ UnitMeasurements::Weight.new(2.25567, :kg).to_i
379
+ #=> 2 kg
380
+ UnitMeasurements::Weight.new(2.25567, :kg).to_f
381
+ #=> 2.25567 kg
382
+ UnitMeasurements::Weight.new(2.25567, :kg).to_r
383
+ #=> 225567/100000 kg
384
+ UnitMeasurements::Weight.new(2.25567, :kg).to_d
385
+ #=> 2.25567 kg
386
+ UnitMeasurements::Weight.new(2.25567, :kg).to_c
387
+ #=> 2.25567+0i kg
388
+ ```
389
+
351
390
  ## Units
352
391
 
353
392
  The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
@@ -27,6 +27,8 @@ require "unit_measurements/unit"
27
27
  require "unit_measurements/unit_group"
28
28
  require "unit_measurements/arithmetic"
29
29
  require "unit_measurements/comparison"
30
+ require "unit_measurements/conversion"
31
+ require "unit_measurements/math"
30
32
  require "unit_measurements/normalizer"
31
33
  require "unit_measurements/parser"
32
34
  require "unit_measurements/formatter"
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ module Conversion
7
+ # Converts quantity of the measurement to +Integer+.
8
+ #
9
+ # @example
10
+ # UnitMeasurements::Weight.new(2.25567, :kg).to_i
11
+ # => 2 kg
12
+ #
13
+ # @return [Measurement]
14
+ def to_i
15
+ self.class.new(quantity.to_i, unit)
16
+ end
17
+
18
+ # Converts quantity of the measurement to +Float+.
19
+ #
20
+ # @example
21
+ # UnitMeasurements::Weight.new(2.25567, :kg).to_f
22
+ # => 2.25567 kg
23
+ #
24
+ # @return [Measurement]
25
+ def to_f
26
+ self.class.new(quantity.to_f, unit)
27
+ end
28
+
29
+ # Converts quantity of the measurement to +Rational+.
30
+ #
31
+ # @example
32
+ # UnitMeasurements::Weight.new(2.25567, :kg).to_r
33
+ # => 225567/100000 kg
34
+ #
35
+ # @return [Measurement]
36
+ def to_r
37
+ self.class.new(quantity.to_r, unit)
38
+ end
39
+
40
+ # Converts quantity of the measurement to +Complex+.
41
+ #
42
+ # @example
43
+ # UnitMeasurements::Weight.new(2.25567, :kg).to_c
44
+ # => 2.25567+0i kg
45
+ #
46
+ # @return [Measurement]
47
+ def to_c
48
+ self.class.new(quantity.to_c, unit)
49
+ end
50
+
51
+ # Converts quantity of the measurement to +BigDecimal+.
52
+ #
53
+ # @example
54
+ # UnitMeasurements::Weight.new(2.25567, :kg).to_d
55
+ # => 2.25567 kg
56
+ #
57
+ # @return [Measurement]
58
+ def to_d
59
+ self.class.new(quantity.to_d, unit)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ module Math
7
+ # Rounds quantity of the measurement. If `ndigits` is not specified,
8
+ # quantity is rounded to `Integer`.
9
+ #
10
+ # @example
11
+ # UnitMeasurements::Weight.new(1, :g).convert_to(:st).round(4)
12
+ # => 0.0002 st
13
+ #
14
+ # @param [Integer] ndigits
15
+ #
16
+ # @return [Measurement]
17
+ def round(ndigits = 0)
18
+ self.class.new(quantity.round(ndigits), unit)
19
+ end
20
+
21
+ # Returns absolute value of the measurement quantity.
22
+ #
23
+ # @example
24
+ # UnitMeasurements::Length.new(-17.625, :m).abs
25
+ # => 17.625 m
26
+ #
27
+ # @return [Measurement]
28
+ def abs
29
+ self.class.new(quantity.abs, unit)
30
+ end
31
+
32
+ # Rounds quantity of the measurement to next lower integer.
33
+ #
34
+ # @example
35
+ # UnitMeasurements::Length.new(17.625, :m).floor
36
+ # => 17 m
37
+ #
38
+ # @return [Measurement]
39
+ def floor(*args)
40
+ self.class.new(quantity.floor(*args), unit)
41
+ end
42
+
43
+ # Rounds quantity of the measurement to next higher integer.
44
+ #
45
+ # @example
46
+ # UnitMeasurements::Length.new(17.625, :m).ceil
47
+ # => 18 m
48
+ #
49
+ # @return [Measurement]
50
+ def ceil(*args)
51
+ self.class.new(quantity.ceil(*args), unit)
52
+ end
53
+ end
54
+ end
@@ -4,9 +4,11 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  class Measurement
7
- include Formatter
8
- include Comparison
9
7
  include Arithmetic
8
+ include Comparison
9
+ include Conversion
10
+ include Formatter
11
+ include Math
10
12
 
11
13
  CONVERSION_STRING_REGEXP = /(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
12
14
 
@@ -3,10 +3,10 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  UnitMeasurements::Length = UnitMeasurements.build do
6
- si_unit :m, aliases: %i[meter metre meters metres]
6
+ si_unit :m, aliases: [:meter, :metre, :meters, :metres]
7
7
 
8
- unit :in, value: "25.4 mm", aliases: %i[" inch inches]
9
- unit :ft, value: "12 in", aliases: %i[' foot feet]
10
- unit :yd, value: "3 ft", aliases: %i[yard yards]
11
- unit :mi, value: "1760 yd", aliases: %i[mile miles]
8
+ unit :in, value: "25.4 mm", aliases: [:'"', :inch, :inches]
9
+ unit :ft, value: "12 in", aliases: [:"'", :foot, :feet]
10
+ unit :yd, value: "3 ft", aliases: [:yard, :yards]
11
+ unit :mi, value: "1760 yd", aliases: [:mile, :miles]
12
12
  end
@@ -3,8 +3,8 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  UnitMeasurements::Weight = UnitMeasurements.build do
6
- si_unit :g, aliases: %i[gram grams gramme grammes]
6
+ si_unit :g, aliases: [:gram, :grams, :gramme, :grammes]
7
7
 
8
- unit :q, value: "100 kg", aliases: %i[quintal quintals]
9
- unit :t, value: "10 q", aliases: %i[tonne tonnes metric\ tonne metric\ tonnes]
8
+ unit :q, value: "100 kg", aliases: [:quintal, :quintals]
9
+ unit :t, value: "10 q", aliases: [:tonne, :tonnes, :"metric tonne", :"metric tonnes"]
10
10
  end
@@ -3,5 +3,5 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- VERSION = "1.5.1"
6
+ VERSION = "1.7.0"
7
7
  end
data/units.md CHANGED
@@ -5,7 +5,7 @@ bundled units before converting your measurements.
5
5
 
6
6
  **Notes:**
7
7
  1. Base unit for each unit group is highlighted.
8
- 2. Unit numbers suffixed with `*` support all [SI prefixes](README.md#si-units-support).
8
+ 2. Unit names suffixed with `*` support all [SI prefixes](README.md#si-units-support).
9
9
 
10
10
  Below are the units which are bundled in the unit_measurements.
11
11
 
@@ -13,9 +13,9 @@ Below are the units which are bundled in the unit_measurements.
13
13
 
14
14
  These units are defined in `UnitMeasurements::Length`.
15
15
 
16
- | # | Symbol | Aliases |
16
+ | # | Name | Aliases |
17
17
  |:--|:--|:--|
18
- | **1\*** | **m** | **meter, metre, meters, metres** |
18
+ | **1** | **m\*** | **meter, metre, meters, metres** |
19
19
  | 2 | in | ", inch, inches |
20
20
  | 3 | ft | ', foot, feet |
21
21
  | 4 | yd | yard, yards |
@@ -25,8 +25,8 @@ These units are defined in `UnitMeasurements::Length`.
25
25
 
26
26
  These units are defined in `UnitMeasurements::Weight`.
27
27
 
28
- | # | Symbol | Aliases |
28
+ | # | Name | Aliases |
29
29
  |--|--|--|
30
- | **1\*** | **g** | **gram, grams, gramme, grammes** |
30
+ | **1** | **g\*** | **gram, grams, gramme, grammes** |
31
31
  | 2 | q | quintal, quintals |
32
32
  | 3 | t | tonne, tonnes, metric tonne, metric tonnes |
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unit_measurements
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2023-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -106,10 +106,12 @@ files:
106
106
  - lib/unit_measurements/arithmetic.rb
107
107
  - lib/unit_measurements/base.rb
108
108
  - lib/unit_measurements/comparison.rb
109
+ - lib/unit_measurements/conversion.rb
109
110
  - lib/unit_measurements/errors/parse_error.rb
110
111
  - lib/unit_measurements/errors/unit_already_defined_error.rb
111
112
  - lib/unit_measurements/errors/unit_error.rb
112
113
  - lib/unit_measurements/formatter.rb
114
+ - lib/unit_measurements/math.rb
113
115
  - lib/unit_measurements/measurement.rb
114
116
  - lib/unit_measurements/normalizer.rb
115
117
  - lib/unit_measurements/parser.rb