unit_measurements 1.5.1 → 1.6.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: daf31532333a9d280d948318fe94079cfb5a8d341f6756a5c64b42843142c80f
4
+ data.tar.gz: 88a85aed303c4c5209330bdcf14b1ee5696e40b0e815b6b570a60ac6fd4b6db5
5
5
  SHA512:
6
- metadata.gz: 5435fba092be97c1c8407b917857c1990db07d1aa1729653ecf14519d98dcec2a4c5e5ad0c4997586eb2f72b17d1099506068aa99eb592ee35ddd8da4a2f8ba7
7
- data.tar.gz: 5938944868b729168d49d1f5e7e888029931bb17237fd72c66980bf6b47d7fe58f8eefdd94f1927652d9b5d1e3e480e8df746f5c7607995390ea7caeec186634
6
+ metadata.gz: 60febdff329999f4aeedb36fbb8aa70eccf1ab8feecc34c4318daca4949b2df16034a285ce8ac5bee2c99e59f991fe52f456154dba5ae1c945bdf6e732f2ed55
7
+ data.tar.gz: 8152670ce01518dac56686ccc17f51e5c3b898f6ec90710d9a5ebb66089fa234351a70a0a91a9b9e1adf02cfb0966df3490b02f625aa639239c63440d515e960
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [1.6.0](https://github.com/shivam091/unit_measurements/compare/v1.5.1...v1.6.0) - 2023-08-19
2
+
3
+ ## What's new
4
+
5
+ - Added support to perform mathematical operations on the measurements.
6
+
7
+ ----------
8
+
1
9
  ## [1.5.1](https://github.com/shivam091/unit_measurements/compare/v1.5.0...v1.5.1) - 2023-08-18
2
10
 
3
11
  ### 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.6.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -348,6 +348,27 @@ 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
+
351
372
  ## Units
352
373
 
353
374
  The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
@@ -27,6 +27,7 @@ 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/math"
30
31
  require "unit_measurements/normalizer"
31
32
  require "unit_measurements/parser"
32
33
  require "unit_measurements/formatter"
@@ -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,10 @@
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 Formatter
10
+ include Math
10
11
 
11
12
  CONVERSION_STRING_REGEXP = /(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
12
13
 
@@ -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.6.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.6.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-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -110,6 +110,7 @@ files:
110
110
  - lib/unit_measurements/errors/unit_already_defined_error.rb
111
111
  - lib/unit_measurements/errors/unit_error.rb
112
112
  - lib/unit_measurements/formatter.rb
113
+ - lib/unit_measurements/math.rb
113
114
  - lib/unit_measurements/measurement.rb
114
115
  - lib/unit_measurements/normalizer.rb
115
116
  - lib/unit_measurements/parser.rb