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 +4 -4
- data/lib/measured/case_insensitive_unit.rb +13 -1
- data/lib/measured/measurable.rb +9 -2
- data/lib/measured/unit.rb +24 -15
- data/lib/measured/version.rb +1 -1
- data/test/case_insensitive_unit_test.rb +9 -7
- data/test/measurable_test.rb +16 -3
- data/test/unit_test.rb +9 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31510659ee5ef4e555a6e90c6b02ccfbb2d00762
|
4
|
+
data.tar.gz: 6d4098c941cfeb91a625c7b535a8e68857f91d87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(
|
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
|
data/lib/measured/measurable.rb
CHANGED
@@ -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, :
|
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
|
-
@
|
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:
|
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
|
-
"#{
|
27
|
+
@to_s ||= if conversion_string
|
28
|
+
"#{name} (#{conversion_string})".freeze
|
25
29
|
else
|
26
|
-
|
30
|
+
name
|
27
31
|
end
|
28
32
|
end
|
29
33
|
|
30
34
|
def inspect
|
31
|
-
|
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 =
|
45
|
+
names_comparison = names <=> other.names
|
37
46
|
if names_comparison != 0
|
38
47
|
names_comparison
|
39
48
|
else
|
40
|
-
|
49
|
+
conversion_amount <=> other.conversion_amount
|
41
50
|
end
|
42
51
|
else
|
43
|
-
|
52
|
+
name <=> other
|
44
53
|
end
|
45
54
|
end
|
46
55
|
|
47
56
|
def inverse_conversion_amount
|
48
|
-
1 / conversion_amount
|
57
|
+
@inverse_conversion_amount ||= 1 / conversion_amount
|
49
58
|
end
|
50
59
|
|
51
60
|
private
|
52
61
|
|
53
62
|
def conversion_string
|
54
|
-
"#{
|
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
|
data/lib/measured/version.rb
CHANGED
@@ -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 "
|
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::
|
47
|
-
assert_equal "#<Measured::
|
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
|
data/test/measurable_test.rb
CHANGED
@@ -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 "
|
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
|
147
|
-
assert_equal "#<Magic: 1.234 magic_missile
|
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
|
47
|
-
assert_equal "#<Measured::Unit: pie (cake,
|
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.
|
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
|
11
|
+
date: 2017-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|