unit_measurements 1.0.0 → 1.2.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: 286c6438c249edf7fe0872a2ad8956f42cb5150819885e8bebe353a7955c97c9
4
- data.tar.gz: 1e82f6cf7ec7dc4f51670be13980cc30ccfe56583fb2b94622f12614d9a0c91e
3
+ metadata.gz: 330be281aba063532010cf47f1e0065c35390510749002f368ff3139a22e6b5c
4
+ data.tar.gz: 26a8aa9db580205a117b11a8b653a5c609d1972be7b3eb76e2b91ec20f02b7be
5
5
  SHA512:
6
- metadata.gz: 5353470b9101426b12d5fd34474d41fe04de2e4ef8286dd42c78605f01f35116f62341626299708f5ea0cc53613977437876b906d7fd79d91888ed9f1221a6e9
7
- data.tar.gz: 7bda4b4f0598d53e2b3cf933da8d8485d8052ca6172d772bbaf55aa4400542782c62b99d583d0109ef5af2a7ed3504eb4991eb06b5a5f9cbcdcc4f87575ac919
6
+ metadata.gz: 2d67ddc3a66baec5ce04878b046541dd7b9f3d8da65a18fcf84179f4178c467b3e2276f1a236e22caac9d170815d6529be842fdac2ac45aed79e48b32d136de7
7
+ data.tar.gz: 331afbfc4616fd9cbccf13ed6c3d01266ba6fd49e9fe4ae06c9f8ed311210a77381056d0037415b4fc4c48477a5fcda48d2d64397c8df3888bcacf1b965c79c8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## [1.2.0](https://github.com/shivam091/unit_measurements/compare/v1.1.0...v1.2.0) - 2023-08-15
2
+
3
+ ### What's new
4
+
5
+ - Added support to specify unit value as an `array` of number and unit. viz.,
6
+
7
+ ----------
8
+
9
+ ## [1.1.0](https://github.com/shivam091/unit_measurements/compare/v1.0.0...v1.1.0) - 2023-08-14
10
+
11
+ ### What's new
12
+
13
+ - Added `#format` method to format quantity of the measurement.
14
+
15
+ ----------
16
+
1
17
  ## [1.0.0](https://github.com/shivam091/unit_measurements/compare/v0.1.0...v1.0.0) - 2023-09-14
2
18
 
3
19
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.0.0)
4
+ unit_measurements (1.2.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
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
@@ -385,6 +399,9 @@ UnitMeasurements::Time = UnitMeasurements.build do
385
399
  # You can also specify conversion string if it's converted against a unit other
386
400
  # than the unit group's base unit.
387
401
  unit :h, value: "60 min", aliases: [:hour, :hours]
402
+
403
+ # You can also specify unit value as an array.
404
+ unit :d, value: [24, :h], aliases: [:day, :days]
388
405
  end
389
406
  ```
390
407
 
@@ -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"
@@ -3,7 +3,7 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- class ParseError < StandardError
6
+ class ParseError < BaseError
7
7
  attr_reader :string
8
8
 
9
9
  def initialize(string)
@@ -3,7 +3,7 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- class UnitAlreadyDefinedError < StandardError
6
+ class UnitAlreadyDefinedError < BaseError
7
7
  attr_reader :unit
8
8
 
9
9
  def initialize(unit)
@@ -3,7 +3,7 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- class UnitError < StandardError
6
+ class UnitError < BaseError
7
7
  attr_reader :unit
8
8
 
9
9
  def initialize(unit)
@@ -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 ArgumentError, "Quantity cannot be blank." if quantity.blank?
13
- raise ArgumentError, "Unit cannot be blank." if unit.blank?
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)
@@ -38,13 +38,11 @@ module UnitMeasurements
38
38
  end
39
39
 
40
40
  def conversion_factor
41
- if value.is_a?(String)
42
- measurement_value, measurement_unit = Parser.parse(value)
43
- conversion_factor = unit_group.unit_for!(measurement_unit).conversion_factor
44
- conversion_factor * measurement_value
45
- else
46
- value
47
- end
41
+ return value if value.is_a?(Numeric)
42
+
43
+ measurement_value, measurement_unit = parse_value(value)
44
+ conversion_factor = unit_group.unit_for!(measurement_unit).conversion_factor
45
+ conversion_factor * measurement_value
48
46
  end
49
47
 
50
48
  private
@@ -75,5 +73,17 @@ module UnitMeasurements
75
73
  ["R", %w(ronna), 1e+27],
76
74
  ["Q", %w(quetta), 1e+30]
77
75
  ].map(&:freeze).freeze
76
+
77
+ def parse_value(tokens)
78
+ case tokens
79
+ when String
80
+ tokens = Parser.parse(value)
81
+ when Array
82
+ raise BaseError, "Cannot parse [number, unit] formatted tokens from #{tokens}." unless tokens.size == 2
83
+ else
84
+ raise BaseError, "Value of the unit must be defined as string or array, but received #{tokens}"
85
+ end
86
+ [tokens[0].to_r, tokens[1].freeze]
87
+ end
78
88
  end
79
89
  end
@@ -3,5 +3,5 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- VERSION = "1.0.0"
6
+ VERSION = "1.2.0"
7
7
  end
@@ -2,6 +2,10 @@
2
2
  # -*- frozen_string_literal: true -*-
3
3
  # -*- warn_indent: true -*-
4
4
 
5
+ module UnitMeasurements
6
+ class BaseError < StandardError; end
7
+ end
8
+
5
9
  require "unit_measurements/base"
6
10
 
7
11
  require "unit_measurements/unit_groups/all"
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.0.0
4
+ version: 1.2.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-14 00:00:00.000000000 Z
11
+ date: 2023-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -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