unit_measurements 5.2.0 → 5.4.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: 30ecc167254df71dd7e6415ea85e4042837d37294041d5fc1791e395711a5774
4
- data.tar.gz: c7e5382424fbb305607a312e6fec931e0967c09d402f08ce88343909a7c0a89e
3
+ metadata.gz: e3db321077ce62eba3bf98f77fc58980cdfdef88789fba2a219a71655bb8a9af
4
+ data.tar.gz: 3c5b51f6d643d8cce56f67452fdd76643ae385f2d325b479151e1ce3686caeb3
5
5
  SHA512:
6
- metadata.gz: b50e6cf5076ed8a27b16b81246c864cb0b3dcbeba9e91b400d7e377f91bd9497667312f2ff551cb43abc4a3520ee11d31f4926b20bbf40010f0895d19d0c32e7
7
- data.tar.gz: 948c1541e6e57613839a88dee8c5f99e583f0137c2bfc3114fa1e9eb758bce608b6f194adbab2f8e85216768127630ec65c90bb4fd685054cbb69000fa0a9a9e
6
+ metadata.gz: 193497aecf9153a9098fb6cc496f7f72ed82cff90d6045a60a7567eed3bb3c07af9a03e62d0c49ce479ac517a59d45501336cceb66c12edaed3986811e9f12aa
7
+ data.tar.gz: 5a8d0f77ae6b7f9455f05e122db97f3ad4ddaf448c0bf45b9c0d086b7363b6fd7e65ad3d12288d0b1e5541d413270b8ac7a3bc7d3a4377ce7ea40c9bcbef366e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [5.4.0](https://github.com/shivam091/unit_measurements/compare/v5.3.0...v5.4.0) - 2023-10-27
2
+
3
+ ### What's new
4
+
5
+ - Added new units `Å`, `lnk`, `lea`, `hh`, `nl`, and `sft` in length.
6
+ - Added new units `N`, `s`, `drt`, and `lbt` in weight.
7
+
8
+ ----------
9
+
10
+ ## [5.3.0](https://github.com/shivam091/unit_measurements/compare/v5.2.0...v5.3.0) - 2023-10-24
11
+
12
+ ### What's new
13
+
14
+ - Added ability set globally configurable options for **`unit_measurements`**.
15
+
16
+ ### What's improved
17
+
18
+ - Code coverage improvements.
19
+
20
+ ----------
21
+
1
22
  ## [5.2.0](https://github.com/shivam091/unit_measurements/compare/v5.1.1...v5.2.0) - 2023-10-22
2
23
 
3
24
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.2.0)
4
+ unit_measurements (5.4.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -22,14 +22,16 @@ to numerous errors.
22
22
 
23
23
  The `unit_measurements` gem is designed to simplify the handling of units for scientific calculations.
24
24
 
25
- ## Features
26
-
27
- 1. Provides easy conversion between units.
28
- 2. Lightweight and easily extensible to include other units and conversions.
29
- 3. Built in support for various [unit groups](https://github.com/shivam091/unit_measurements/blob/main/units.md).
30
- 4. Ability to parse strings representing complex, fractional, mixed fractional, scientific numbers, and ratios.
31
- 5. Well organized and descriptive documentation published [here](https://shivam091.github.io/unit_measurements).
32
- 6. Supports caching of conversion factors between different units of the unit group.
25
+ ## Key Features
26
+ 1. **Simplified Measurement Conversion:** Easily convert measurements between compatible units, reducing the likelihood of errors in scientific calculations.
27
+ 2. **Extensible Unit Groups:** Effortlessly build own unit groups with specific units and conversions tailored to your needs.
28
+ 3. **Built-in Unit Groups:** Comes bundled with a wide range of standard [unit groups](https://github.com/shivam091/unit_measurements/blob/main/units.md),
29
+ covering various units.
30
+ 4. **String Parsing Capabilities:** Effortlessly parse strings representing complex, fractional, mixed fractional, scientific numbers, and ratios directly
31
+ saving you the hassle of manually extracting and converting them.
32
+ 5. **Comprehensive Documentation:** Well-organized and descriptive [documentation](https://shivam091.github.io/unit_measurements) for quick reference and implementation guidance.
33
+ 6. **Configurable Options:** Fine-tune behavior with configurable options, including caching for enhanced performance.
34
+ 7. **Error Handling:** Robust error handling ensures stability and reliability during conversions.
33
35
 
34
36
  ## Disclaimer
35
37
 
@@ -57,6 +59,23 @@ Or otherwise simply install it yourself as:
57
59
 
58
60
  `$ gem install unit_measurements`
59
61
 
62
+ ## Configuration
63
+
64
+ `unit_measurements` is designed to work out of the box, but you can customize its behavior by placing
65
+ the configuration block in an initializer file before requiring the library files:
66
+
67
+ ```ruby
68
+ UnitMeasurements.configure do |config|
69
+ config.use_cache = false
70
+ end
71
+ ```
72
+
73
+ The current available configurable options are:
74
+
75
+ | Option | Default value | Description |
76
+ | ------ | ------------- | ----------- |
77
+ | **use_cache** | false | Set to `true` to enable caching during conversions. |
78
+
60
79
  ## Usage
61
80
 
62
81
  The **`UnitMeasurements::Measurement`** class is responsible for conversion of quantity to various compatible units
@@ -400,10 +419,9 @@ gem "unit_measurements", require: ["unit_measurements/base", "unit_measurements/
400
419
 
401
420
  ### Building new unit groups
402
421
 
403
- This library provides a simpler way to define your own unit groups. Use the
404
- `UnitMeasurements.build` method to define units within it. You can also group
405
- units by the unit system using the `system` method and set the primitive unit for
406
- each unit group using the `primitive` method.
422
+ This library provides a simpler way to define your own unit groups. Use the `UnitMeasurements.build` method to define
423
+ units within it. You can group units by the unit system using the `system` method and set the primitive unit for the
424
+ unit group using the `primitive` method. You can specify cache file name in unit group definition using the `cache` method.
407
425
 
408
426
  ```ruby
409
427
  UnitMeasurements::Time = UnitMeasurements.build do
@@ -7,6 +7,13 @@ require "unit_measurements/version"
7
7
 
8
8
  module UnitMeasurements
9
9
  class << self
10
+ # Allows setting an instance of +Configuration+ containing values of desired
11
+ # configurable options.
12
+ #
13
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
14
+ # @since 5.3.0
15
+ attr_writer :configuration
16
+
10
17
  # Creates a new unit group based on the provided +block+ of instructions.
11
18
  #
12
19
  # The +build+ method allows you to define and create a custom unit group with
@@ -40,10 +47,6 @@ module UnitMeasurements
40
47
  # cache "length.json"
41
48
  # end
42
49
  #
43
- # @param block
44
- # A block of instructions for defining units and their conversions within
45
- # the unit group.
46
- #
47
50
  # @yield [builder]
48
51
  # A block that defines the units to be added to the unit group.
49
52
  # The block takes a {UnitGroupBuilder} instance as a parameter.
@@ -76,10 +79,58 @@ module UnitMeasurements
76
79
  @unit_group = builder.build
77
80
  end
78
81
  end
82
+
83
+ # Returns an instance of +Configuration+ with the values of desired configurable
84
+ # options of +*unit_measurements*+. If instance is not present, it initializes
85
+ # a new instance of {Configuration}.
86
+ #
87
+ # @return [Configuration] An instance of +Configuration+.
88
+ #
89
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
90
+ # @since 5.3.0
91
+ def configuration
92
+ @configuration ||= Configuration.new
93
+ end
94
+
95
+ # Reset the configuration to its default state.
96
+ #
97
+ # @example
98
+ # UnitMeasurements.reset
99
+ #
100
+ # @return [Configuration] A new +Configuration+ object.
101
+ #
102
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
103
+ # @since 5.3.0
104
+ def reset
105
+ @configuration = Configuration.new
106
+ end
107
+
108
+ # Configures options of the +*UnitMeasurements*+ module using a block. It
109
+ # yields the current +Configuration+ instance for updating default values of
110
+ # options by new values specified within a block.
111
+ #
112
+ # @example
113
+ # UnitMeasurements.configure do |config|
114
+ # config.use_cache = false
115
+ # end
116
+ #
117
+ # @yield [configuration] The current +Configuration+ instance.
118
+ #
119
+ # @yieldparam [Configuration] configuration
120
+ # An instance of +Configuration+ with the new values of options.
121
+ #
122
+ # @yieldreturn [Configuration] The updated +Configuration+ instance.
123
+ #
124
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
125
+ # @since 5.3.0
126
+ def configure
127
+ yield configuration
128
+ end
79
129
  end
80
130
  end
81
131
 
82
132
  # The following requires load various components of the unit measurements library.
133
+ require "unit_measurements/configuration"
83
134
  require "unit_measurements/cache"
84
135
  require "unit_measurements/unit_group_builder"
85
136
  require "unit_measurements/unit"
@@ -0,0 +1,64 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_stringing_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ # The +UnitMeasurements::Configuration+ class maintains and manages the globally
7
+ # configurable options of +*unit_measurements*+.
8
+ #
9
+ # @note
10
+ # This class is responsible for configuring globally configurable options of
11
+ # +*unit_measurements*+.
12
+ #
13
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
14
+ # @since 5.3.0
15
+ class Configuration
16
+ # Get the current value of the +use_cache+ option.
17
+ #
18
+ # @note
19
+ # This option controls whether caching is enabled for converting measurements.
20
+ # Defaults to +false+.
21
+ #
22
+ # @return [TrueClass|FalseClass]
23
+ # Returns +true+ if caching is enabled, otherwise +false+.
24
+ #
25
+ # @see Cache
26
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
27
+ # @since 5.3.0
28
+ attr_reader :use_cache
29
+
30
+ # Initializes a new +Configuration+ instance with default values of configurable
31
+ # options.
32
+ #
33
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
34
+ # @since 5.3.0
35
+ def initialize
36
+ self.use_cache = false
37
+ end
38
+
39
+ # Sets a value for the +use_cache+ option.
40
+ #
41
+ # It controls whether caching is enabled for converting measurements. When
42
+ # caching is enabled, previously computed conversion factors are stored for
43
+ # future use, improving conversion performance.
44
+ #
45
+ # @param [TrueClass|FalseClass] use_cache
46
+ # +true+ if caching should be used while converting the measurement otherwise
47
+ # +false+.
48
+ #
49
+ # @return [TrueClass|FalseClass] The updated value of +use_cache+.
50
+ #
51
+ # @raise [BaseError] if +use_cache+ is not a boolean value.
52
+ #
53
+ # @see Cache
54
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
55
+ # @since 5.3.0
56
+ def use_cache=(use_cache)
57
+ unless [true, false].include?(use_cache)
58
+ raise BaseError, "Configuration#use_cache= only accepts true or false, but received #{use_cache}"
59
+ end
60
+
61
+ @use_cache = use_cache
62
+ end
63
+ end
64
+ end
@@ -462,6 +462,8 @@ module UnitMeasurements
462
462
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
463
463
  # @since 5.2.0
464
464
  def calculate_conversion_factor(target_unit, use_cache)
465
+ use_cache = (UnitMeasurements.configuration.use_cache || use_cache)
466
+
465
467
  if use_cache && (cached_factor = self.class.cached.get(unit.name, target_unit.name))
466
468
  cached_factor
467
469
  else
@@ -7,19 +7,31 @@ UnitMeasurements::Length = UnitMeasurements.build do
7
7
 
8
8
  system :metric do
9
9
  si_unit "m", aliases: ["meter", "metre", "meters", "metres"]
10
+
11
+ unit "Å", value: "1e-10 m", aliases: ["angstrom", "angstroms", "ångström"]
10
12
  end
11
13
 
12
14
  system :imperial do
15
+ unit "h", value: "4 in", aliases: ["hh", "hand", "hands"]
16
+
13
17
  unit "in", value: "25.4 mm", aliases: ['"', "inch", "inches"]
14
18
  unit "ft", value: "12 in", aliases: ["'", "foot", "feet"]
15
19
  unit "yd", value: "3 ft", aliases: ["yard", "yards"]
16
- unit "mi", value: "1760 yd", aliases: ["mile", "miles", "statute mile", "international mile"]
20
+ unit "mi", value: "5280 ft", aliases: ["mile", "miles", "statute mile", "international mile"]
17
21
  unit "ch", value: "22 yd", aliases: ["chain", "chains"]
18
- unit "th", value: "1/1000 in", aliases: ["thou", "thousandth of an inch"]
22
+ unit "th", value: "1/1000 in", aliases: ["thou", "thousandth of an inch", "mil", "mils"]
23
+ unit "nl", value: "3 nmi", aliases: ["NL", "nleague", "nleagues", "nautical league", "nautical leagues"]
24
+
19
25
  unit "ftm", value: "6 ft", aliases: ["fathom", "fathoms"]
20
26
  unit "fur", value: "220 yd", aliases: ["furlong", "furlongs"]
21
27
  unit "nmi", value: "1852 m", aliases: ["NMI", "M", "NM", "nautical mile", "nautical miles"]
22
28
  unit "rod", value: "16 1/2 ft", aliases: ["rods", "perch", "pole", "lug"]
29
+ unit "lnk", value: "33/50 ft", aliases: ["l", "li", "link", "links"]
30
+ unit "lea", value: "3 mi", aliases: ["league", "leagues"]
31
+ end
32
+
33
+ system :us_customary do
34
+ unit "sft", value: "1200/3937 m", aliases: ["sfoot", "sfeet", "survey-foot", "survey-feet"]
23
35
  end
24
36
 
25
37
  system :astronomical do
@@ -10,27 +10,28 @@ UnitMeasurements::Weight = UnitMeasurements.build do
10
10
 
11
11
  unit "q", value: "100 kg", aliases: ["quintal", "quintals"]
12
12
  unit "t", value: "1000 kg", aliases: ["tonne", "tonnes", "metric tonne", "metric tonnes"]
13
+ unit "N", value: "0.10197162129779283 kg", aliases: ["newton", "newtons"]
14
+
13
15
  unit "ct", value: "200 mg", aliases: ["carat", "carats"]
14
16
  unit "Da", value: "1.660538921e-27 kg", aliases: ["dalton", "daltons"]
15
17
  end
16
18
 
17
19
  system :imperial do
18
- unit "lb", value: [Rational(45_359_237, 1e+8), "kg"], aliases: ["lbs", "lbm", "pound-mass", "pound", "pounds", "#"]
20
+ unit "s", value: "20 gr", aliases: ["scruple", "scruples"]
21
+
22
+ unit "lb", value: "0.45359237 kg", aliases: ["lbs", "lbm", "pound-mass", "pound", "pounds", "#"]
19
23
  unit "st", value: "14 lb", aliases: ["stone", "stones"]
20
- unit "oz", value: [Rational(1, 16), "lb"], aliases: ["ounce", "ounces"]
21
- end
24
+ unit "oz", value: "1/16 lb", aliases: ["ounce", "ounces"]
25
+ unit "dr", value: "1/16 oz", aliases: ["dram", "drams"]
26
+ unit "gr", value: "1/7000 lb", aliases: ["grain", "grains"]
22
27
 
23
- system :troy_weight do
24
- unit "gr", value: [Rational(1, 7000), "lb"], aliases: ["grain", "grains"]
28
+ unit "drt", value: "60 gr", aliases: ["troy dram", "troy drams"]
25
29
  unit "ozt", value: "480 gr", aliases: ["troy ounce", "troy ounces"]
26
- unit "dwt", value: [Rational(1, 20), "ozt"], aliases: ["pennyweight", "pennyweights"]
27
- end
28
-
29
- system :avoirdupois do
30
- unit "dr", value: [Rational(1, 16), "oz"], aliases: ["dram", "drams"]
30
+ unit "lbt", value: "5760 gr", aliases: ["troy pound", "troy pounds"]
31
+ unit "dwt", value: "1/20 ozt", aliases: ["pwt", "pennyweight", "pennyweights"]
31
32
  end
32
33
 
33
34
  system :foot_pound_second do
34
- unit "slug", value: "32.1740 lb", aliases: ["slugs"]
35
+ unit "slug", value: "32.17404856 lb", aliases: ["slugs"]
35
36
  end
36
37
  end
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.2.0"
7
+ VERSION = "5.4.0"
8
8
  end
data/units.md CHANGED
@@ -9,27 +9,33 @@ check below list of bundled units before converting your measurements.
9
9
  in addition to [Decimal SI prefixes](README.md#decimal-si-prefixes).
10
10
  3. Primitive unit of the unit group is in _emphasised typeface_.
11
11
 
12
- ## 1. Length/Distance
12
+ ## 1. Length or distance
13
13
 
14
14
  These units are defined in `UnitMeasurements::Length`.
15
15
 
16
16
  | # | Name | Aliases |
17
17
  |:--|:--|:--|
18
18
  | _1_ | _m*_ | _meter, metre, meters, metres_ |
19
- | 2 | in | ", inch, inches |
20
- | 3 | ft | ', foot, feet |
21
- | 4 | yd | yard, yards |
22
- | 5 | mi | mile, miles, statute mile, international mile |
23
- | 6 | ch | chain, chains |
24
- | 7 | th | thou, thousandth of an inch |
25
- | 8 | au | astronomical unit, astronomical units |
26
- | 9 | pc | parsec, parsecs |
27
- | 10 | ftm | fathom, fathoms |
28
- | 11 | fur | furlong, furlongs |
29
- | 12 | nmi | NMI, M, NM, nautical mile, nautical miles |
30
- | 13 | rod | rods, perch, pole, lug |
31
-
32
- ## 2. Weight/Mass
19
+ | 2 | Å | angstrom, angstroms, ångström |
20
+ | 3 | h | hh, hand, hands |
21
+ | 4 | in | ", inch, inches |
22
+ | 5 | ft | ', foot, feet |
23
+ | 6 | yd | yard, yards |
24
+ | 7 | mi | mile, miles, statute mile, international mile |
25
+ | 8 | ch | chain, chains |
26
+ | 9 | th | thou, thousandth of an inch, mil, mils |
27
+ | 10 | au | astronomical unit, astronomical units |
28
+ | 11 | pc | parsec, parsecs |
29
+ | 12 | nl | NL, nleague, nleagues, nautical league, nautical leagues |
30
+ | 13 | ftm | fm, fathom, fathoms |
31
+ | 14 | fur | furlong, furlongs |
32
+ | 15 | nmi | NMI, M, NM, nautical mile, nautical miles |
33
+ | 16 | rod | rods, perch, pole, lug |
34
+ | 17 | lnk | l, li, link, links |
35
+ | 18 | lea | league, leagues |
36
+ | 19 | sft | sfoot, sfeet, survey-foot, survey-feet |
37
+
38
+ ## 2. Weight or mass
33
39
 
34
40
  These units are defined in `UnitMeasurements::Weight`.
35
41
 
@@ -38,16 +44,20 @@ These units are defined in `UnitMeasurements::Weight`.
38
44
  | _1_ | _g*_ | _gram, grams, gramme, grammes_ |
39
45
  | 2 | q | quintal, quintals |
40
46
  | 3 | t | tonne, tonnes, metric tonne, metric tonnes |
41
- | 4 | ct | carat, carats |
42
- | 5 | Da | dalton, daltons |
43
- | 6 | st | stone, stones |
44
- | 7 | dr | dram, drams |
45
- | 8 | dwt | pennyweight, pennyweights |
46
- | 9 | ozt | troy ounce, troy ounces |
47
- | 10 | slug | slugs |
48
- | 11 | lb | lbs, lbm, pound-mass, pound, pounds, \# |
49
- | 12 | oz | ounce, ounces |
50
- | 13 | gr | grain, grains |
47
+ | 4 | N | newton, newtons |
48
+ | 5 | s | scruple, scruples |
49
+ | 6 | ct | carat, carats |
50
+ | 7 | Da | dalton, daltons |
51
+ | 8 | st | stone, stones |
52
+ | 9 | dr | dram, drams |
53
+ | 10 | lb | lbs, lbm, pound-mass, pound, pounds, \# |
54
+ | 11 | oz | ounce, ounces |
55
+ | 12 | gr | grain, grains |
56
+ | 13 | dwt | pennyweight, pennyweights |
57
+ | 14 | ozt | troy ounce, troy ounces |
58
+ | 15 | lbt | troy pound, troy pounds |
59
+ | 16 | drt | troy dram, troy drams |
60
+ | 17 | slug | slugs |
51
61
 
52
62
  ## 3. Time
53
63
 
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: 5.2.0
4
+ version: 5.4.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-10-22 00:00:00.000000000 Z
11
+ date: 2023-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -109,6 +109,7 @@ files:
109
109
  - lib/unit_measurements/base.rb
110
110
  - lib/unit_measurements/cache.rb
111
111
  - lib/unit_measurements/comparison.rb
112
+ - lib/unit_measurements/configuration.rb
112
113
  - lib/unit_measurements/conversion.rb
113
114
  - lib/unit_measurements/errors/parse_error.rb
114
115
  - lib/unit_measurements/errors/primitive_unit_already_set_error.rb