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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 286c6438c249edf7fe0872a2ad8956f42cb5150819885e8bebe353a7955c97c9
4
- data.tar.gz: 1e82f6cf7ec7dc4f51670be13980cc30ccfe56583fb2b94622f12614d9a0c91e
3
+ metadata.gz: 24b2504d7feb77f54138260f7e0947bd86e00225dbe3c6d9e575da3050a24eeb
4
+ data.tar.gz: 7d9b84113fbf2a2cd5ca1c5a46467296b15f1bf1016c1775b854098245998cee
5
5
  SHA512:
6
- metadata.gz: 5353470b9101426b12d5fd34474d41fe04de2e4ef8286dd42c78605f01f35116f62341626299708f5ea0cc53613977437876b906d7fd79d91888ed9f1221a6e9
7
- data.tar.gz: 7bda4b4f0598d53e2b3cf933da8d8485d8052ca6172d772bbaf55aa4400542782c62b99d583d0109ef5af2a7ed3504eb4991eb06b5a5f9cbcdcc4f87575ac919
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.0.0)
4
+ unit_measurements (1.1.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
@@ -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)
@@ -3,5 +3,5 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- VERSION = "1.0.0"
6
+ VERSION = "1.1.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,7 +1,7 @@
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.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