unit_measurements 5.2.0 → 5.3.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: 006027cb5f557c69f5ba083f6624cf9e424fcb00cc794b8f50912907538a3689
4
+ data.tar.gz: 4db6ec5c12a752191933c5aa6d99837c1e2dc5e8269e5d05339a6e9b631d3e7f
5
5
  SHA512:
6
- metadata.gz: b50e6cf5076ed8a27b16b81246c864cb0b3dcbeba9e91b400d7e377f91bd9497667312f2ff551cb43abc4a3520ee11d31f4926b20bbf40010f0895d19d0c32e7
7
- data.tar.gz: 948c1541e6e57613839a88dee8c5f99e583f0137c2bfc3114fa1e9eb758bce608b6f194adbab2f8e85216768127630ec65c90bb4fd685054cbb69000fa0a9a9e
6
+ metadata.gz: fe13840ae30879e2068b4a0628e073259d8faab8efc615f0ff6da1b4fc7307b2a4285ebe44425fc4bedf26263e02a5f29e34df6ac792e3c16429f214262dbc31
7
+ data.tar.gz: d9f28aecb8b8dea511466a8e4f71288581c35407d841b20d65006a9bb8e3dad8d3ec53da4b2f83c0235d9a919a2dccf572371957c1010a8d449c7b11bdf1947f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [5.3.0](https://github.com/shivam091/unit_measurements/compare/v5.2.0...v5.3.0) - 2023-10-24
2
+
3
+ ### What's new
4
+
5
+ - Added ability set globally configurable options for **`unit_measurements`**.
6
+
7
+ ### What's improved
8
+
9
+ - Code coverage improvements.
10
+
11
+ ----------
12
+
1
13
  ## [5.2.0](https://github.com/shivam091/unit_measurements/compare/v5.1.1...v5.2.0) - 2023-10-22
2
14
 
3
15
  ### 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.3.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
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.2.0"
7
+ VERSION = "5.3.0"
8
8
  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: 5.2.0
4
+ version: 5.3.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-24 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