measured 2.0.0.pre2 → 2.0.0.pre3

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
  SHA1:
3
- metadata.gz: 10bd5a0ef33c55999eb30825b49399c3ab2ec183
4
- data.tar.gz: 9307127d9fb8b0e7610919aaa18f53f4b9465e75
3
+ metadata.gz: 31510659ee5ef4e555a6e90c6b02ccfbb2d00762
4
+ data.tar.gz: 6d4098c941cfeb91a625c7b535a8e68857f91d87
5
5
  SHA512:
6
- metadata.gz: 98b681bbf014999d0f328067af5d30259452deef6588b13b45d3db1afbadb117b20e19abcfb973620fe1a06984936807a9acea2a9c8433bc23e1332080f1b05e
7
- data.tar.gz: 4bb6841e99399a8bc73f1063f3973f0b3e92f7273540b18393547e162b941a3d4f6fbddcd846e5fda6f7e61b8b22292112914b3dfc59879f23060fa4d4a035c0
6
+ metadata.gz: faf9a4f655aca4e68679da9e1220ccb1c2f44ae41f7ae5fa194b711825735d3e38056e40cf8f992abaff14d6502f438d843645824951f00bbfc9cb75b77790a2
7
+ data.tar.gz: cff7887f432a42af3a7e0e19c487183baed8f7c2ba251d9329211efe5c1ae950d43a54d3a4bdccda035a2f1c3585cf3a31d657a3fc639d3a2bd27be53a7ee088
@@ -1,5 +1,17 @@
1
1
  class Measured::CaseInsensitiveUnit < Measured::Unit
2
2
  def initialize(name, aliases: [], value: nil, unit_system: nil)
3
- super(name.to_s.downcase, aliases: aliases.map(&:to_s).map!(&:downcase), value: value, unit_system: unit_system)
3
+ super(
4
+ name.to_s.downcase,
5
+ aliases: aliases.map(&:to_s).map!(&:downcase),
6
+ value: value,
7
+ unit_system: unit_system
8
+ )
9
+ end
10
+
11
+ private
12
+
13
+ def parse_value(tokens)
14
+ value, unit = super
15
+ [value, unit.downcase]
4
16
  end
5
17
  end
@@ -29,11 +29,18 @@ class Measured::Measurable < Numeric
29
29
  end
30
30
 
31
31
  def to_s
32
- @to_s ||= "#{value_string} #{unit}"
32
+ @to_s ||= "#{value_string} #{unit.name}"
33
+ end
34
+
35
+ def humanize
36
+ @humanize ||= begin
37
+ unit_string = value == 1 ? unit.name : ActiveSupport::Inflector.pluralize(unit.name)
38
+ "#{value_string} #{unit_string}"
39
+ end
33
40
  end
34
41
 
35
42
  def inspect
36
- @inspect ||= "#<#{self.class}: #{value_string} #{unit}>"
43
+ @inspect ||= "#<#{self.class}: #{value_string} #{unit.inspect}>"
37
44
  end
38
45
 
39
46
  def <=>(other)
data/lib/measured/unit.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  class Measured::Unit
2
2
  include Comparable
3
3
 
4
- attr_reader :name, :names, :conversion_amount, :conversion_unit, :unit_system
4
+ attr_reader :name, :aliases, :conversion_amount, :conversion_unit, :unit_system
5
5
 
6
6
  def initialize(name, aliases: [], value: nil, unit_system: nil)
7
- @name = name.to_s
8
- @names = ([@name] + aliases.map(&:to_s)).sort
7
+ @name = name.to_s.freeze
8
+ @aliases = aliases.map(&:to_s).map(&:freeze).freeze
9
9
  @conversion_amount, @conversion_unit = parse_value(value) if value
10
10
  @unit_system = unit_system
11
11
  end
@@ -13,52 +13,61 @@ class Measured::Unit
13
13
  def with_unit_system(unit_system)
14
14
  self.class.new(
15
15
  name,
16
- aliases: names - [name],
16
+ aliases: aliases,
17
17
  value: conversion_string,
18
18
  unit_system: unit_system
19
19
  )
20
20
  end
21
21
 
22
+ def names
23
+ @names ||= ([name] + aliases).sort!.freeze
24
+ end
25
+
22
26
  def to_s
23
- if conversion_string
24
- "#{ @name } (#{ conversion_string })"
27
+ @to_s ||= if conversion_string
28
+ "#{name} (#{conversion_string})".freeze
25
29
  else
26
- @name
30
+ name
27
31
  end
28
32
  end
29
33
 
30
34
  def inspect
31
- "#<Measured::Unit: #{ @name } (#{ @names.join(", ") }) #{ conversion_string }>"
35
+ @inspect ||= begin
36
+ pieces = [name]
37
+ pieces << "(#{aliases.join(", ")})" if aliases.any?
38
+ pieces << conversion_string if conversion_string
39
+ "#<#{self.class.name}: #{pieces.join(" ")}>".freeze
40
+ end
32
41
  end
33
42
 
34
43
  def <=>(other)
35
44
  if self.class == other.class
36
- names_comparison = @names <=> other.names
45
+ names_comparison = names <=> other.names
37
46
  if names_comparison != 0
38
47
  names_comparison
39
48
  else
40
- @conversion_amount <=> other.conversion_amount
49
+ conversion_amount <=> other.conversion_amount
41
50
  end
42
51
  else
43
- @name <=> other
52
+ name <=> other
44
53
  end
45
54
  end
46
55
 
47
56
  def inverse_conversion_amount
48
- 1 / conversion_amount.to_r
57
+ @inverse_conversion_amount ||= 1 / conversion_amount
49
58
  end
50
59
 
51
60
  private
52
61
 
53
62
  def conversion_string
54
- "#{ conversion_amount } #{ conversion_unit }" if @conversion_amount || @conversion_unit
63
+ @conversion_string ||= ("#{conversion_amount} #{conversion_unit}" if conversion_amount || conversion_unit)
55
64
  end
56
65
 
57
66
  def parse_value(tokens)
58
- tokens = tokens.split(" ") if tokens.is_a?(String)
67
+ tokens = tokens.split(" ", 2) if tokens.is_a?(String)
59
68
 
60
69
  raise Measured::UnitError, "Cannot parse 'number unit' or [number, unit] formatted tokens from #{tokens}." unless tokens.size == 2
61
70
 
62
- [tokens[0].to_r, tokens[1]]
71
+ [tokens[0].to_r, tokens[1].freeze]
63
72
  end
64
73
  end
@@ -1,3 +1,3 @@
1
1
  module Measured
2
- VERSION = "2.0.0.pre2"
2
+ VERSION = "2.0.0.pre3"
3
3
  end
@@ -16,11 +16,15 @@ class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
16
16
 
17
17
  test "#initialize parses out the unit and the number part" do
18
18
  assert_equal 10, @unit.conversion_amount
19
- assert_equal "Cake", @unit.conversion_unit
19
+ assert_equal "cake", @unit.conversion_unit
20
20
 
21
21
  unit = Measured::CaseInsensitiveUnit.new(:pie, value: "5.5 sweets")
22
22
  assert_equal BigDecimal("5.5"), unit.conversion_amount
23
23
  assert_equal "sweets", unit.conversion_unit
24
+
25
+ unit = Measured::CaseInsensitiveUnit.new(:pie, value: "1/3 Bitter Pies")
26
+ assert_equal Rational(1, 3), unit.conversion_amount
27
+ assert_equal "bitter pies", unit.conversion_unit
24
28
  end
25
29
 
26
30
  test "#initialize raises if the format of the value is incorrect" do
@@ -28,10 +32,6 @@ class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
28
32
  Measured::CaseInsensitiveUnit.new(:pie, value: "hello")
29
33
  end
30
34
 
31
- assert_raises Measured::UnitError do
32
- Measured::CaseInsensitiveUnit.new(:pie, value: "pie is delicious")
33
- end
34
-
35
35
  assert_raises Measured::UnitError do
36
36
  Measured::CaseInsensitiveUnit.new(:pie, value: "123456")
37
37
  end
@@ -43,8 +43,10 @@ class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
43
43
  end
44
44
 
45
45
  test "#inspect returns an expected string" do
46
- assert_equal "#<Measured::Unit: pie (pie) >", Measured::CaseInsensitiveUnit.new(:pie).inspect
47
- assert_equal "#<Measured::Unit: pie (cake, pie) 1/2 sweet>", Measured::CaseInsensitiveUnit.new(:pie, aliases: ["cake"], value: "1/2 sweet").inspect
46
+ assert_equal "#<Measured::CaseInsensitiveUnit: pie>", Measured::CaseInsensitiveUnit.new(:pie).inspect
47
+ assert_equal "#<Measured::CaseInsensitiveUnit: pie (cake)>", Measured::CaseInsensitiveUnit.new(:pie, aliases: ["CAKE"]).inspect
48
+ assert_equal "#<Measured::CaseInsensitiveUnit: pie 1/2 sweet>", Measured::CaseInsensitiveUnit.new(:Pie, value: "1/2 sweet").inspect
49
+ assert_equal "#<Measured::CaseInsensitiveUnit: pie (cake) 1/2 sweet>", Measured::CaseInsensitiveUnit.new(:pie, aliases: ["cake"], value: "1/2 sweet").inspect
48
50
  end
49
51
 
50
52
  test "includes Comparable mixin" do
@@ -138,13 +138,26 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
138
138
  end
139
139
 
140
140
  test "#to_s outputs the number and the unit" do
141
- assert_equal "10 fireball (2/3 magic_missile)", Magic.new(10, :fire).to_s
141
+ assert_equal "1 fireball", Magic.new("1", :fire).to_s
142
+ assert_equal "10 fireball", Magic.new(10, :fire).to_s
142
143
  assert_equal "1.234 magic_missile", Magic.new("1.234", :magic_missile).to_s
144
+ assert_equal "0.125 magic_missile", Magic.new(Rational(1, 8), :magic_missile).to_s
145
+ assert_equal "0.375 magic_missile", Magic.new(Rational(3, 8), :magic_missile).to_s
146
+ assert_equal "5 fireball", Magic.new(Rational(5, 1), :fire).to_s
147
+ end
148
+
149
+ test "#humanize outputs the number and the unit properly pluralized" do
150
+ assert_equal "1 fireball", Magic.new("1", :fire).humanize
151
+ assert_equal "10 fireballs", Magic.new(10, :fire).humanize
152
+ assert_equal "1.234 magic_missiles", Magic.new("1.234", :magic_missile).humanize
153
+ assert_equal "0.125 magic_missiles", Magic.new(Rational(1, 8), :magic_missile).humanize
154
+ assert_equal "0.375 magic_missiles", Magic.new(Rational(3, 8), :magic_missile).humanize
155
+ assert_equal "5 fireballs", Magic.new(Rational(5, 1), :fire).humanize
143
156
  end
144
157
 
145
158
  test "#inspect shows the number and the unit" do
146
- assert_equal "#<Magic: 10 fireball (2/3 magic_missile)>", Magic.new(10, :fire).inspect
147
- assert_equal "#<Magic: 1.234 magic_missile>", Magic.new(1.234, :magic_missile).inspect
159
+ assert_equal "#<Magic: 10 #<Measured::CaseInsensitiveUnit: fireball (fire, fireballs) 2/3 magic_missile>>", Magic.new(10, :fire).inspect
160
+ assert_equal "#<Magic: 1.234 #<Measured::CaseInsensitiveUnit: magic_missile (magic_missiles)>>", Magic.new(1.234, :magic_missile).inspect
148
161
  end
149
162
 
150
163
  test "#zero? always returns false" do
data/test/unit_test.rb CHANGED
@@ -18,9 +18,13 @@ class Measured::UnitTest < ActiveSupport::TestCase
18
18
  assert_equal 10, @unit.conversion_amount
19
19
  assert_equal "Cake", @unit.conversion_unit
20
20
 
21
- unit = Measured::Unit.new(:pie, value: "5.5 sweets")
21
+ unit = Measured::Unit.new(:pie, value: ["5.5", "sweets"])
22
22
  assert_equal BigDecimal("5.5"), unit.conversion_amount
23
23
  assert_equal "sweets", unit.conversion_unit
24
+
25
+ unit = Measured::Unit.new(:pie, value: "1/3 bitter pie")
26
+ assert_equal Rational(1, 3), unit.conversion_amount
27
+ assert_equal "bitter pie", unit.conversion_unit
24
28
  end
25
29
 
26
30
  test "#initialize raises if the format of the value is incorrect" do
@@ -28,10 +32,6 @@ class Measured::UnitTest < ActiveSupport::TestCase
28
32
  Measured::Unit.new(:pie, value: "hello")
29
33
  end
30
34
 
31
- assert_raises Measured::UnitError do
32
- Measured::Unit.new(:pie, value: "pie is delicious")
33
- end
34
-
35
35
  assert_raises Measured::UnitError do
36
36
  Measured::Unit.new(:pie, value: "123456")
37
37
  end
@@ -43,8 +43,10 @@ class Measured::UnitTest < ActiveSupport::TestCase
43
43
  end
44
44
 
45
45
  test "#inspect returns an expected string" do
46
- assert_equal "#<Measured::Unit: pie (pie) >", Measured::Unit.new(:pie).inspect
47
- assert_equal "#<Measured::Unit: pie (cake, pie) 1/2 sweet>", Measured::Unit.new(:pie, aliases: ["cake"], value: "1/2 sweet").inspect
46
+ assert_equal "#<Measured::Unit: pie>", Measured::Unit.new(:pie).inspect
47
+ assert_equal "#<Measured::Unit: pie (cake, semi-sweet)>", Measured::Unit.new(:pie, aliases: ["cake", "semi-sweet"]).inspect
48
+ assert_equal "#<Measured::Unit: pie 1/2 sweet>", Measured::Unit.new(:pie, value: "1/2 sweet").inspect
49
+ assert_equal "#<Measured::Unit: pie (cake) 1/2 sweet>", Measured::Unit.new(:pie, aliases: ["cake"], value: "1/2 sweet").inspect
48
50
  end
49
51
 
50
52
  test "includes Comparable mixin" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre2
4
+ version: 2.0.0.pre3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-19 00:00:00.000000000 Z
11
+ date: 2017-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport