measured 2.8.0 → 2.8.1

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: 474f40f00059e15ec826a6ac74758ea6919f3b3c61a75bc42bcf181f9ac357dc
4
- data.tar.gz: 356026fb5443a773928dbe21ac20f28f97ed72a987f6708b682cb665eeb511fc
3
+ metadata.gz: 5ec3f063994e56291a70f5b2726890e03b43ef00e512894a901bc8ea924ae895
4
+ data.tar.gz: 442e6cca31eb154cbe318287a64f9379ab987b9ddcd59629a2780960b52ea00d
5
5
  SHA512:
6
- metadata.gz: be4796ff68ae8e99abdd8361c68c79624e5b6b10f143a3314edc834e28cd79aa8c6f2c5707d49564d42c8d80fd0521a3db38023cd688a70e733a8f737991ff22
7
- data.tar.gz: 56a1cf4956fc1eb5802bd74841dfcef1114eaf3514f1a0c3c3c9585aa7643e0c36f2ec287c8933c879ce4a5ecd2e1232ce2a4ea0e30f7cef7fc1a5cc90db69e8
6
+ metadata.gz: 141a1214a375f0af9ae4d92c1e9c51d77e2a98d2a174321ee9e85eb4387fa72c3c98c49dcc5448d6617f7cc7d6bdf67e9ca4f12772d7446cb00e827329108d4f
7
+ data.tar.gz: f5a34f7aa0baf05177f216089dd9b4443ca2b04eecda5c0b72bf5899973a9a5a2a462b2e4f11ea7087dffcc8927e35835e9555da211bc628d50f76fadd110c35
data/CHANGELOG.md CHANGED
@@ -1,6 +1,15 @@
1
1
  Unreleased
2
2
  -----
3
3
 
4
+
5
+
6
+ 2.8.1
7
+ -----
8
+
9
+ * Allow format without the conversion string. (@saraid)
10
+ * Add special case for zero coercion to allow direct use of `sum` method. (@jmortlock)
11
+ * Add useful numeric methods `finite?`, `infinite?`, `zero?`, `nonzero?`, `positive?` and `negative?`. (@jmortlock)
12
+
4
13
  2.8.0
5
14
  -----
6
15
 
data/README.md CHANGED
@@ -149,6 +149,15 @@ Measured::Weight.new("3.14", "grams").format("%.1<value>f %<unit>s")
149
149
 
150
150
  If no string is passed to the `format` method it defaults to `"%.2<value>f %<unit>s"`.
151
151
 
152
+ If the unit isn't the standard SI unit, it will include a conversion string.
153
+
154
+ ```ruby
155
+ Measured::Weight.new("3.14", "kg").format
156
+ > "3.14 kg (1000/1 g)"
157
+ Measured::Weight.new("3.14", "kg").format(with_conversion_string: false)
158
+ > "3.14 kg"
159
+ ```
160
+
152
161
  ## Units and conversions
153
162
 
154
163
  ### SI units support
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
  module Measured::Arithmetic
3
+ extend Forwardable
4
+ def_delegators :@value, :zero?, :positive?, :negative?, :finite?, :infinite?
5
+
3
6
  def +(other)
4
7
  arithmetic_operation(other, :+)
5
8
  end
@@ -19,6 +22,8 @@ module Measured::Arithmetic
19
22
  def coerce(other)
20
23
  if other.is_a?(self.class)
21
24
  [other, self]
25
+ elsif other.is_a?(Numeric) && other.zero?
26
+ [self.class.new(other, self.unit), self]
22
27
  else
23
28
  raise TypeError, "Cannot coerce #{other.class} to #{self.class}"
24
29
  end
@@ -28,6 +33,10 @@ module Measured::Arithmetic
28
33
  raise TypeError, "#{self.class} cannot be converted to an integer"
29
34
  end
30
35
 
36
+ def nonzero?
37
+ value.nonzero? ? self : false
38
+ end
39
+
31
40
  private
32
41
 
33
42
  def arithmetic_operation(other, operator)
@@ -43,10 +43,10 @@ class Measured::Measurable < Numeric
43
43
  self.class.new(new_value, new_unit)
44
44
  end
45
45
 
46
- def format(format_string=nil)
46
+ def format(format_string=nil, with_conversion_string: true)
47
47
  kwargs = {
48
48
  value: self.value,
49
- unit: self.unit,
49
+ unit: self.unit.to_s(with_conversion_string: with_conversion_string),
50
50
  }
51
51
  (format_string || DEFAULT_FORMAT_STRING) % kwargs
52
52
  end
data/lib/measured/unit.rb CHANGED
@@ -23,8 +23,8 @@ class Measured::Unit
23
23
  )
24
24
  end
25
25
 
26
- def to_s
27
- if @conversion_string
26
+ def to_s(with_conversion_string: true)
27
+ if with_conversion_string && @conversion_string
28
28
  "#{name} (#{@conversion_string})".freeze
29
29
  else
30
30
  name
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Measured
3
- VERSION = "2.8.0"
3
+ VERSION = "2.8.1"
4
4
  end
@@ -8,6 +8,41 @@ class Measured::ArithmeticTest < ActiveSupport::TestCase
8
8
  @four = Magic.new(4, :magic_missile)
9
9
  end
10
10
 
11
+ test "should be able to sum same units" do
12
+ assert_equal Magic.new(9, :magic_missile), [@two, @three, @four].sum
13
+ end
14
+
15
+ test 'can check for finite?' do
16
+ assert Magic.new(0, :magic_missile).finite?
17
+ refute Magic.new(Float::INFINITY, :magic_missile).finite?
18
+ end
19
+
20
+ test 'can check for infinite?' do
21
+ assert Magic.new(Float::INFINITY, :magic_missile).infinite?
22
+ refute Magic.new(0, :magic_missile).infinite?
23
+ end
24
+
25
+ test 'can check for zero?' do
26
+ assert Magic.new(0, :magic_missile).zero?
27
+ refute Magic.new(1, :magic_missile).zero?
28
+ end
29
+
30
+ test 'can check for nonzero?' do
31
+ assert_equal Magic.new(10, :magic_missile), Magic.new(10, :magic_missile).nonzero?
32
+ assert Magic.new(10, :magic_missile).nonzero?
33
+ refute Magic.new(0, :magic_missile).nonzero?
34
+ end
35
+
36
+ test 'can check for positive?' do
37
+ assert Magic.new(1, :magic_missile).positive?
38
+ refute Magic.new(-1, :magic_missile).positive?
39
+ end
40
+
41
+ test 'can check for negative?' do
42
+ assert Magic.new(-1, :magic_missile).negative?
43
+ refute Magic.new(1, :magic_missile).negative?
44
+ end
45
+
11
46
  test "#+ should add together same units" do
12
47
  assert_equal Magic.new(5, :magic_missile), @two + @three
13
48
  assert_equal Magic.new(5, :magic_missile), @three + @two
@@ -232,6 +232,11 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
232
232
  assert_equal "9.34 magic_missile", Magic.new(9.342, :magic_missile).format
233
233
  end
234
234
 
235
+ test "#format can drop the conversion amount" do
236
+ assert_equal "1.00 fireball (2/3 magic_missile)", Magic.new(1, :fireball).format
237
+ assert_equal "1.00 fireball", Magic.new(1, :fireball).format(with_conversion_string: false)
238
+ end
239
+
235
240
  test "#humanize outputs the number and the unit properly pluralized" do
236
241
  assert_equal "1 fireball", Magic.new("1", :fire).humanize
237
242
  assert_equal "10 fireballs", Magic.new(10, :fire).humanize
@@ -246,12 +251,6 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
246
251
  assert_equal "#<Magic: 1.234 #<Measured::Unit: magic_missile (magic_missiles, magic missile)>>", Magic.new(1.234, :magic_missile).inspect
247
252
  end
248
253
 
249
- test "#zero? always returns false" do
250
- refute_predicate Magic.new(0, :fire), :zero?
251
- refute_predicate Magic.new(0.0, :fire), :zero?
252
- refute_predicate Magic.new("0.0", :fire), :zero?
253
- end
254
-
255
254
  test "#<=> compares regardless of the unit" do
256
255
  assert_equal (-1), @magic <=> Magic.new(20, :fire)
257
256
  assert_equal 1, @magic <=> Magic.new(9, :magic_missile)
data/test/unit_test.rb CHANGED
@@ -43,6 +43,10 @@ class Measured::UnitTest < ActiveSupport::TestCase
43
43
  assert_equal "pie (1/2 sweet)", Measured::Unit.new(:pie, aliases: ["cake"], value: "0.5 sweet").to_s
44
44
  end
45
45
 
46
+ test "#to_s can drop the conversion amount" do
47
+ assert_equal "pie", Measured::Unit.new(:pie).to_s(with_conversion_string: false)
48
+ end
49
+
46
50
  test "#inspect returns an expected string" do
47
51
  assert_equal "#<Measured::Unit: pie>", Measured::Unit.new(:pie).inspect
48
52
  assert_equal "#<Measured::Unit: pie (cake, semi-sweet)>", Measured::Unit.new(:pie, aliases: ["cake", "semi-sweet"]).inspect
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.0
4
+ version: 2.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-11-08 00:00:00.000000000 Z
13
+ date: 2021-12-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport