unit_measurements 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d1b9cef9a108d5a0930ca0fd0a2e156a5653893786eb97159c7c4c801e71970
4
- data.tar.gz: a4ef6d728ad141f2d19ab5151ea3205ce2098710b8facf1d979b4299e8a72530
3
+ metadata.gz: daf31532333a9d280d948318fe94079cfb5a8d341f6756a5c64b42843142c80f
4
+ data.tar.gz: 88a85aed303c4c5209330bdcf14b1ee5696e40b0e815b6b570a60ac6fd4b6db5
5
5
  SHA512:
6
- metadata.gz: 360ba9c4e48722b51fc9d9dd049ddd2217aac3c30d3de6afb66ee10d31d466d15e743f6437ceb6d7f232844378f6ca3be995447627e4264c1c25d1e7db9690e5
7
- data.tar.gz: 8bed02dfd85b3933e128e2c390a12f5f63f15950ddadd053063822f00be4b8c3464992ad0d51440bebcbad78c7b0df1b80cdcb341b1d1dbeea0d1171ed06ffdc
6
+ metadata.gz: 60febdff329999f4aeedb36fbb8aa70eccf1ab8feecc34c4318daca4949b2df16034a285ce8ac5bee2c99e59f991fe52f456154dba5ae1c945bdf6e732f2ed55
7
+ data.tar.gz: 8152670ce01518dac56686ccc17f51e5c3b898f6ec90710d9a5ebb66089fa234351a70a0a91a9b9e1adf02cfb0966df3490b02f625aa639239c63440d515e960
data/CHANGELOG.md CHANGED
@@ -1,8 +1,24 @@
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
+
9
+ ## [1.5.1](https://github.com/shivam091/unit_measurements/compare/v1.5.0...v1.5.1) - 2023-08-18
10
+
11
+ ### What's updated
12
+
13
+ - Added extra blank line between method code and last code statement (considered as return).
14
+
15
+ ----------
16
+
1
17
  ## [1.5.0](https://github.com/shivam091/unit_measurements/compare/v1.4.0...v1.5.0) - 2023-08-18
2
18
 
3
- ## What's fixed
19
+ ### What's fixed
4
20
 
5
- - Fixed precision in `Measurement#quantity` method
21
+ - Fixed precision in `Measurement#quantity` method.
6
22
 
7
23
  ----------
8
24
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.5.0)
4
+ unit_measurements (1.6.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -307,7 +307,6 @@ UnitMeasurements::Weight.unit_or_alias?(:gramme)
307
307
 
308
308
  You have ability to compare the measurements with the same or different units within the same unit group.
309
309
  For example, comparing weight with weight will work, comparing a weight with a area would fail.
310
- Allowed comparisons are `==`, `!=`, `>`, `>=`, `<`, and `<=`.
311
310
 
312
311
  ```ruby
313
312
  UnitMeasurements::Weight.new(1, "kg") == UnitMeasurements::Weight.new(1, :kg)
@@ -320,6 +319,10 @@ UnitMeasurements::Weight.parse("1 kg") <= UnitMeasurements::Weight.parse("0.5 kg
320
319
  #=> false
321
320
  UnitMeasurements::Weight.parse("1 kg") >= UnitMeasurements::Weight.parse("0.5 kg")
322
321
  #=> true
322
+ UnitMeasurements::Length.new(1, :ft).between?(UnitMeasurements::Length.new(12, :in), UnitMeasurements::Length.new(24, :in))
323
+ #=> true
324
+ UnitMeasurements::Length.new(1, :ft).clamp(UnitMeasurements::Length.new(13, :in), UnitMeasurements::Length.new(24, :in))
325
+ #=> 13 in
323
326
  ```
324
327
 
325
328
  ### Arithmetic
@@ -345,6 +348,27 @@ UnitMeasurements::Weight.new(4, :kg) / UnitMeasurements::Weight.new(2, :kg)
345
348
  #=> 2 kg
346
349
  ```
347
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
+
348
372
  ## Units
349
373
 
350
374
  The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
@@ -68,6 +68,7 @@ module UnitMeasurements
68
68
 
69
69
  def arithmetic_operation(other, operator)
70
70
  other, _ = coerce(other)
71
+
71
72
  self.class.new(self.quantity.public_send(operator, other.convert_to(self.unit).quantity), self.unit)
72
73
  end
73
74
  end
@@ -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"
@@ -8,6 +8,7 @@ module UnitMeasurements
8
8
 
9
9
  def initialize(string)
10
10
  @string = string
11
+
11
12
  super("Unable to parse: '#{string}'.")
12
13
  end
13
14
  end
@@ -8,6 +8,7 @@ module UnitMeasurements
8
8
 
9
9
  def initialize(unit)
10
10
  @unit = unit
11
+
11
12
  super("Unit already defined: '#{unit}'.")
12
13
  end
13
14
  end
@@ -8,6 +8,7 @@ module UnitMeasurements
8
8
 
9
9
  def initialize(unit)
10
10
  @unit = unit
11
+
11
12
  super("Invalid unit: '#{unit}'.")
12
13
  end
13
14
  end
@@ -22,6 +22,7 @@ module UnitMeasurements
22
22
  # @return [String]
23
23
  def format(format = nil)
24
24
  kwargs = {quantity: quantity, unit: unit.to_s}
25
+
25
26
  (format || DEFAULT_FORMAT) % kwargs
26
27
  end
27
28
  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,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
 
@@ -26,6 +27,7 @@ module UnitMeasurements
26
27
  return self if target_unit == unit
27
28
 
28
29
  conversion_factor = (unit.conversion_factor / target_unit.conversion_factor)
30
+
29
31
  self.class.new((quantity * conversion_factor), target_unit)
30
32
  end
31
33
  alias_method :to, :convert_to
@@ -33,6 +35,7 @@ module UnitMeasurements
33
35
  def convert_to!(target_unit)
34
36
  measurement = convert_to(target_unit)
35
37
  @quantity, @unit = measurement.quantity, measurement.unit
38
+
36
39
  self
37
40
  end
38
41
  alias_method :to!, :convert_to!
@@ -70,6 +73,7 @@ module UnitMeasurements
70
73
  def parse(input)
71
74
  input = Normalizer.normalize(input)
72
75
  source, target = input.match(CONVERSION_STRING_REGEXP)&.captures
76
+
73
77
  target ? _parse(source).convert_to(target) : _parse(source)
74
78
  end
75
79
 
@@ -77,6 +81,7 @@ module UnitMeasurements
77
81
 
78
82
  def _parse(string)
79
83
  quantity, unit = Parser.parse(string)
84
+
80
85
  new(quantity, unit)
81
86
  end
82
87
  end
@@ -92,6 +97,7 @@ module UnitMeasurements
92
97
  when String
93
98
  quantity = Normalizer.normalize(quantity)
94
99
  quantity, _ = Parser.parse(quantity)
100
+
95
101
  quantity
96
102
  else
97
103
  quantity
@@ -37,17 +37,20 @@ module UnitMeasurements
37
37
  end
38
38
  si_units << build_unit("#{short_prefix}#{name}", value: "#{multiplier} #{name}", aliases: si_aliases)
39
39
  end
40
+
40
41
  si_units
41
42
  end
42
43
 
43
44
  def build_unit(name, value:, aliases:)
44
45
  unit = Unit.new(name, value: value, aliases: aliases)
45
46
  check_for_duplicate_unit_names!(unit)
47
+
46
48
  unit
47
49
  end
48
50
 
49
51
  def check_for_duplicate_unit_names!(unit)
50
52
  names = @units.flat_map(&:names)
53
+
51
54
  if names.any? { |name| unit.names.include?(name) }
52
55
  raise UnitAlreadyDefinedError.new(unit.name)
53
56
  end
@@ -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.0"
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.0
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