unit_measurements 1.0.0 → 1.1.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 +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -0
- data/lib/unit_measurements/base.rb +1 -0
- data/lib/unit_measurements/errors/parse_error.rb +1 -1
- data/lib/unit_measurements/errors/unit_already_defined_error.rb +1 -1
- data/lib/unit_measurements/errors/unit_error.rb +1 -1
- data/lib/unit_measurements/formatter.rb +28 -0
- data/lib/unit_measurements/measurement.rb +3 -2
- data/lib/unit_measurements/version.rb +1 -1
- data/lib/unit_measurements.rb +4 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24b2504d7feb77f54138260f7e0947bd86e00225dbe3c6d9e575da3050a24eeb
|
4
|
+
data.tar.gz: 7d9b84113fbf2a2cd5ca1c5a46467296b15f1bf1016c1775b854098245998cee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5fc7c6e1b2b580fcd5477941e239e8d06a79416b90a7c36145f8d50d32c40c7430b83273038b2974ec51df90673e46672207f8d8ae975c31e99b3612cd3b446
|
7
|
+
data.tar.gz: 7730a9813366e4b36dbf98873a575cf89a33963ee998da6c84d7e6b5fd7c7f5c197530e23f1acaeb2a44e339667f9665d7e3bc62c5164579df06765e01426d85
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## [1.1.0](https://github.com/shivam091/unit_measurements/compare/v1.0.0...v1.1.0) - 2023-08-14
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added `#format` method to format quantity of the measurement.
|
6
|
+
|
7
|
+
----------
|
8
|
+
|
1
9
|
## [1.0.0](https://github.com/shivam091/unit_measurements/compare/v0.1.0...v1.0.0) - 2023-09-14
|
2
10
|
|
3
11
|
### What's new
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -219,6 +219,20 @@ UnitMeasurements::Weight.parse("2e⁻² kg to g")
|
|
219
219
|
|
220
220
|
Supported special characters for exponents are `⁰`, `¹`, `²`, `³`, `⁴`, `⁵`, `⁶`, `⁷`, `⁸`, `⁹`, `⁺`, `⁻`.
|
221
221
|
|
222
|
+
**Formatting measurement:**
|
223
|
+
|
224
|
+
If you want to format measurement to certain format, you can use `#format` method.
|
225
|
+
If format is not specified, it defaults to `"%.2<value>f %<unit>s"`.
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
UnitMeasurements::Weight.parse("2 kg").to(:st).format
|
229
|
+
#=> "0.31 st"
|
230
|
+
UnitMeasurements::Weight.parse("2 kg").to(:st).format("%.4<quantity>f %<unit>s")
|
231
|
+
#=> "0.3149 st"
|
232
|
+
UnitMeasurements::Weight.parse("2 kg").to(:st).format("%.4<quantity>f")
|
233
|
+
#=> "0.3149"
|
234
|
+
```
|
235
|
+
|
222
236
|
**Extract the unit and the quantity from measurement:**
|
223
237
|
|
224
238
|
```ruby
|
@@ -27,6 +27,7 @@ require "unit_measurements/unit"
|
|
27
27
|
require "unit_measurements/unit_group"
|
28
28
|
require "unit_measurements/normalizer"
|
29
29
|
require "unit_measurements/parser"
|
30
|
+
require "unit_measurements/formatter"
|
30
31
|
require "unit_measurements/measurement"
|
31
32
|
|
32
33
|
require "unit_measurements/errors/unit_error"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
module UnitMeasurements
|
6
|
+
module Formatter
|
7
|
+
# The default format for formatting measurements.
|
8
|
+
DEFAULT_FORMAT = "%.2<quantity>f %<unit>s".freeze
|
9
|
+
|
10
|
+
# Formats measurement to certain formatted string specified by +format+.
|
11
|
+
# If +format+ is not specified, it uses +DEFAULT_FORMAT+ for
|
12
|
+
# formatting the measurement
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# UnitMeasurements::Weight.parse("2 kg").to(:st).format
|
16
|
+
# => "0.31 st"
|
17
|
+
# UnitMeasurements::Weight.parse("2 kg").to(:st).format("%.4<quantity>f %<unit>s")
|
18
|
+
# => "0.3149 st"
|
19
|
+
#
|
20
|
+
# @param [String] format
|
21
|
+
#
|
22
|
+
# @return [String]
|
23
|
+
def format(format = nil)
|
24
|
+
kwargs = {quantity: quantity, unit: unit.to_s}
|
25
|
+
(format || DEFAULT_FORMAT) % kwargs
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -4,13 +4,14 @@
|
|
4
4
|
|
5
5
|
module UnitMeasurements
|
6
6
|
class Measurement
|
7
|
+
include Formatter
|
7
8
|
CONVERSION_STRING_REGEXP = /(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
|
8
9
|
|
9
10
|
attr_reader :quantity, :unit
|
10
11
|
|
11
12
|
def initialize(quantity, unit)
|
12
|
-
raise
|
13
|
-
raise
|
13
|
+
raise BaseError, "Quantity cannot be blank." if quantity.blank?
|
14
|
+
raise BaseError, "Unit cannot be blank." if unit.blank?
|
14
15
|
|
15
16
|
@quantity = convert_quantity(quantity)
|
16
17
|
@unit = unit_from_unit_or_name!(unit)
|
data/lib/unit_measurements.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unit_measurements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harshal LADHE
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/unit_measurements/errors/parse_error.rb
|
108
108
|
- lib/unit_measurements/errors/unit_already_defined_error.rb
|
109
109
|
- lib/unit_measurements/errors/unit_error.rb
|
110
|
+
- lib/unit_measurements/formatter.rb
|
110
111
|
- lib/unit_measurements/measurement.rb
|
111
112
|
- lib/unit_measurements/normalizer.rb
|
112
113
|
- lib/unit_measurements/parser.rb
|