unit_measurements 1.6.0 → 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: daf31532333a9d280d948318fe94079cfb5a8d341f6756a5c64b42843142c80f
4
- data.tar.gz: 88a85aed303c4c5209330bdcf14b1ee5696e40b0e815b6b570a60ac6fd4b6db5
3
+ metadata.gz: bbb7978f79ac8f4fac738ef87936cf1bf20397c74772c35120205aed22d1ac1c
4
+ data.tar.gz: 1b99cb2665a071d0e721f78cabad86fc50a39e2eda8349bd5813ba80c48afe89
5
5
  SHA512:
6
- metadata.gz: 60febdff329999f4aeedb36fbb8aa70eccf1ab8feecc34c4318daca4949b2df16034a285ce8ac5bee2c99e59f991fe52f456154dba5ae1c945bdf6e732f2ed55
7
- data.tar.gz: 8152670ce01518dac56686ccc17f51e5c3b898f6ec90710d9a5ebb66089fa234351a70a0a91a9b9e1adf02cfb0966df3490b02f625aa639239c63440d515e960
6
+ metadata.gz: 27da14501c804050def9568138cfcab08b7b81eeab9d47ba3c23117534d2f32f33ed42260c9199d95f1c5cc71e715d1844cc493be34e347989c9fb71932fb058
7
+ data.tar.gz: e6c5e47ab28b199599cc7b86b748205d3124c52fbbdfb83a38849876177764e280ce6d324336908ebdff0eff6a3acdb98b8cf8a8bc94ef8f325deca2d378f560
data/CHANGELOG.md CHANGED
@@ -1,6 +1,15 @@
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
+
1
10
  ## [1.6.0](https://github.com/shivam091/unit_measurements/compare/v1.5.1...v1.6.0) - 2023-08-19
2
11
 
3
- ## What's new
12
+ ### What's new
4
13
 
5
14
  - Added support to perform mathematical operations on the measurements.
6
15
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.6.0)
4
+ unit_measurements (1.7.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -369,6 +369,24 @@ UnitMeasurements::Length.new(17.625, :m).ceil
369
369
  #=> 18 m
370
370
  ```
371
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
+
372
390
  ## Units
373
391
 
374
392
  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/conversion"
30
31
  require "unit_measurements/math"
31
32
  require "unit_measurements/normalizer"
32
33
  require "unit_measurements/parser"
@@ -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
@@ -6,6 +6,7 @@ module UnitMeasurements
6
6
  class Measurement
7
7
  include Arithmetic
8
8
  include Comparison
9
+ include Conversion
9
10
  include Formatter
10
11
  include Math
11
12
 
@@ -3,5 +3,5 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- VERSION = "1.6.0"
6
+ VERSION = "1.7.0"
7
7
  end
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.6.0
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-19 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,6 +106,7 @@ 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