measured 1.1.0 → 1.2.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 +4 -4
- data/README.md +12 -1
- data/lib/measured/base.rb +1 -0
- data/lib/measured/case_sensitive_measurable.rb +7 -0
- data/lib/measured/conversion.rb +8 -5
- data/lib/measured/unit.rb +20 -0
- data/lib/measured/version.rb +1 -1
- data/test/case_sensitive_measurable_test.rb +227 -0
- data/test/conversion_test.rb +37 -0
- data/test/support/fake_system.rb +20 -0
- data/test/unit_test.rb +64 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 764bee6ab868a789054d59899c4537f87683066f
|
4
|
+
data.tar.gz: c15508d99d6154c9b1d2cef708d45d914d7f7071
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abfd4a35d558578bbef37100de6c2f2f16d6e8557bc05f892c549a93c5adce7d61c5102bb7bad73c00fa3acf0a245e7cddefb26a86228d09fb0dd2dc66b0a4a6
|
7
|
+
data.tar.gz: 736f30253c7df7f6fd63ccf7d6c1db550ee09420bc0709473287aa8494ca6307b536bb3aa7fee74ff5cea1bdf6cb4ab84d4d1ef35cd0c14c75601d2f7c698564
|
data/README.md
CHANGED
@@ -185,12 +185,23 @@ class Measured::Thing < Measured::Measurable
|
|
185
185
|
|
186
186
|
conversion.add :different_unit
|
187
187
|
aliases: [:du],
|
188
|
-
value: [Rational(2
|
188
|
+
value: [Rational(2,3), "another_unit"] # Conversion rate can be Rational, otherwise it is coerced to BigDecimal
|
189
|
+
end
|
190
|
+
```
|
191
|
+
|
192
|
+
By default all names and aliases and case insensitive. If you would like to create a new unit with names and aliases that are case sensitive, you should subclass `Measured::CaseSensitiveMeasurable` instead. Other than case sensitivity, both classes are identical to each other.
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
class Measured::Thing < Measured::CaseSensitiveMeasurable
|
196
|
+
conversion.set_base :base_unit,
|
197
|
+
aliases: [:bu]
|
189
198
|
end
|
190
199
|
```
|
191
200
|
|
192
201
|
The base unit takes no value. Values for conversion units can be defined as a string with two tokens `"number unit"` or as an array with two elements. The numbers must be `Rational` or `BigDecimal`, else they will be coerced to `BigDecimal`. Conversion paths don't have to be direct as a conversion table will be built for all possible conversions using tree traversal.
|
193
202
|
|
203
|
+
The `case_sensitive` flag, which is false by default, gets taken into account any time you attempt to reference a unit by name or alias.
|
204
|
+
|
194
205
|
You can also open up the existing classes and add a new conversion:
|
195
206
|
|
196
207
|
```ruby
|
data/lib/measured/base.rb
CHANGED
data/lib/measured/conversion.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
class Measured::Conversion
|
2
2
|
ARBITRARY_CONVERSION_PRECISION = 20
|
3
|
-
def initialize
|
3
|
+
def initialize(case_sensitive: false)
|
4
4
|
@base_unit = nil
|
5
5
|
@units = []
|
6
|
+
@case_sensitive = case_sensitive
|
6
7
|
end
|
7
8
|
|
8
|
-
attr_reader :base_unit, :units
|
9
|
+
attr_reader :base_unit, :units, :case_sensitive
|
9
10
|
|
10
11
|
def set_base(unit_name, aliases: [])
|
11
12
|
add_new_unit(unit_name, aliases: aliases, base: true)
|
@@ -24,11 +25,13 @@ class Measured::Conversion
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def unit_or_alias?(name)
|
27
|
-
|
28
|
+
@units.each{|unit| return true if unit.names_include?(name, case_sensitive: @case_sensitive)}
|
29
|
+
false
|
28
30
|
end
|
29
31
|
|
30
32
|
def unit?(name)
|
31
|
-
|
33
|
+
@units.each{|unit| return true if unit.name_eql?(name, case_sensitive: @case_sensitive)}
|
34
|
+
false
|
32
35
|
end
|
33
36
|
|
34
37
|
def to_unit_name(name)
|
@@ -79,7 +82,7 @@ class Measured::Conversion
|
|
79
82
|
|
80
83
|
def unit_for(name)
|
81
84
|
@units.each do |unit|
|
82
|
-
return unit if unit.
|
85
|
+
return unit if unit.names_include?(name, case_sensitive: @case_sensitive)
|
83
86
|
end
|
84
87
|
|
85
88
|
raise Measured::UnitError, "Cannot find unit for #{ name }."
|
data/lib/measured/unit.rb
CHANGED
@@ -10,6 +10,22 @@ class Measured::Unit
|
|
10
10
|
|
11
11
|
attr_reader :name, :names, :conversion_amount, :conversion_unit
|
12
12
|
|
13
|
+
def name_eql?(name_to_compare, case_sensitive: false)
|
14
|
+
return false unless name_to_compare.present?
|
15
|
+
name_to_compare = name_to_compare.to_s
|
16
|
+
case_sensitive ? @name.eql?(name_to_compare) : case_insensitive(@name).include?(name_to_compare.downcase)
|
17
|
+
end
|
18
|
+
|
19
|
+
def names_include?(name_to_compare, case_sensitive: false)
|
20
|
+
return false unless name_to_compare.present?
|
21
|
+
name_to_compare = name_to_compare.to_s
|
22
|
+
case_sensitive ? @names.include?(name_to_compare) : case_insensitive(@names).include?(name_to_compare.downcase)
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_alias(aliases)
|
26
|
+
@names = (@names << aliases).flatten.sort unless aliases.nil? || aliases.empty?
|
27
|
+
end
|
28
|
+
|
13
29
|
def to_s
|
14
30
|
if conversion_string
|
15
31
|
"#{ @name } (#{ conversion_string })"
|
@@ -40,6 +56,10 @@ class Measured::Unit
|
|
40
56
|
|
41
57
|
private
|
42
58
|
|
59
|
+
def case_insensitive(comparison)
|
60
|
+
[comparison].flatten.map(&:downcase)
|
61
|
+
end
|
62
|
+
|
43
63
|
def conversion_string
|
44
64
|
"#{ conversion_amount } #{ conversion_unit }" if @conversion_amount || @conversion_unit
|
45
65
|
end
|
data/lib/measured/version.rb
CHANGED
@@ -0,0 +1,227 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class Measured::CaseSensitiveMeasurableTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
@magic = CaseSensitiveMagic.new(10, :magic_missile)
|
7
|
+
end
|
8
|
+
|
9
|
+
test "#initialize requires two params, the amount and the unit" do
|
10
|
+
assert_nothing_raised do
|
11
|
+
CaseSensitiveMagic.new(1, "fireball")
|
12
|
+
end
|
13
|
+
|
14
|
+
assert_raises ArgumentError do
|
15
|
+
CaseSensitiveMagic.new(1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test "#initialize converts unit to string from symbol" do
|
20
|
+
magic = CaseSensitiveMagic.new(1, :arcane)
|
21
|
+
assert_equal "arcane", magic.unit
|
22
|
+
end
|
23
|
+
|
24
|
+
test "#initialize raises if it is an unknown unit" do
|
25
|
+
assert_raises Measured::UnitError do
|
26
|
+
CaseSensitiveMagic.new(1, "slash")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
test "#initialize converts numbers and strings into BigDecimal" do
|
31
|
+
assert_equal BigDecimal(1), CaseSensitiveMagic.new(1, :arcane).value
|
32
|
+
assert_equal BigDecimal("2.3"), CaseSensitiveMagic.new("2.3", :arcane).value
|
33
|
+
assert_equal BigDecimal("5"), CaseSensitiveMagic.new("5", :arcane).value
|
34
|
+
end
|
35
|
+
|
36
|
+
test "#initialize converts floats to strings and then to BigDecimal so it does not raise" do
|
37
|
+
assert_equal BigDecimal("1.2345"), CaseSensitiveMagic.new(1.2345, :fire).value
|
38
|
+
end
|
39
|
+
|
40
|
+
test "#initialize converts numbers and strings BigDecimal and does not round large numbers" do
|
41
|
+
assert_equal BigDecimal(9.1234572342342, 14), CaseSensitiveMagic.new(9.1234572342342, :fire).value
|
42
|
+
assert_equal BigDecimal("9.1234572342342"), CaseSensitiveMagic.new(9.1234572342342, :fire).value
|
43
|
+
end
|
44
|
+
|
45
|
+
test "#initialize converts to the base unit name" do
|
46
|
+
assert_equal "fireball", CaseSensitiveMagic.new(1, :fire).unit
|
47
|
+
end
|
48
|
+
|
49
|
+
test "#initialize raises an expected error when initializing with nil value" do
|
50
|
+
exception = assert_raises(Measured::UnitError) do
|
51
|
+
CaseSensitiveMagic.new(nil, :fire)
|
52
|
+
end
|
53
|
+
assert_equal "Unit value cannot be nil", exception.message
|
54
|
+
end
|
55
|
+
|
56
|
+
test "#initialize raises an expected error when initializing with nil unit" do
|
57
|
+
exception = assert_raises(Measured::UnitError) do
|
58
|
+
CaseSensitiveMagic.new(1, nil)
|
59
|
+
end
|
60
|
+
assert_equal "Unit cannot be blank", exception.message
|
61
|
+
end
|
62
|
+
|
63
|
+
test "#initialize raises an expected error when initializing with empty string value" do
|
64
|
+
exception = assert_raises(Measured::UnitError) do
|
65
|
+
CaseSensitiveMagic.new("", :fire)
|
66
|
+
end
|
67
|
+
assert_equal "Unit value cannot be blank", exception.message
|
68
|
+
end
|
69
|
+
|
70
|
+
test "#initialize raises an expected error when initializing with empty string unit" do
|
71
|
+
exception = assert_raises(Measured::UnitError) do
|
72
|
+
CaseSensitiveMagic.new(1, "")
|
73
|
+
end
|
74
|
+
assert_equal "Unit cannot be blank", exception.message
|
75
|
+
end
|
76
|
+
|
77
|
+
test "#unit allows you to read the unit string" do
|
78
|
+
assert_equal "magic_missile", @magic.unit
|
79
|
+
end
|
80
|
+
|
81
|
+
test "#value allows you to read the numeric value" do
|
82
|
+
assert_equal BigDecimal(10), @magic.value
|
83
|
+
end
|
84
|
+
|
85
|
+
test ".conversion is set and cached" do
|
86
|
+
conversion = CaseSensitiveMagic.conversion
|
87
|
+
|
88
|
+
assert_instance_of Measured::Conversion, conversion
|
89
|
+
assert_equal conversion, CaseSensitiveMagic.conversion
|
90
|
+
end
|
91
|
+
|
92
|
+
test ".conversion is set to case sensitive" do
|
93
|
+
conversion = CaseSensitiveMagic.conversion
|
94
|
+
|
95
|
+
assert_instance_of Measured::Conversion, conversion
|
96
|
+
assert_equal conversion, CaseSensitiveMagic.conversion
|
97
|
+
assert_equal true, conversion.case_sensitive
|
98
|
+
end
|
99
|
+
|
100
|
+
test ".units returns just the base units" do
|
101
|
+
assert_equal ["arcane", "fireball", "ice", "magic_missile", "ultima"], CaseSensitiveMagic.units
|
102
|
+
end
|
103
|
+
|
104
|
+
test ".units_with_aliases returns all units" do
|
105
|
+
assert_equal ["arcane", "fire", "fireball", "fireballs", "ice", "magic_missile", "magic_missiles", "ultima"], CaseSensitiveMagic.units_with_aliases
|
106
|
+
end
|
107
|
+
|
108
|
+
test ".valid_unit? looks at the list of units and aliases" do
|
109
|
+
assert CaseSensitiveMagic.valid_unit?("fire")
|
110
|
+
assert CaseSensitiveMagic.valid_unit?("fireball")
|
111
|
+
assert CaseSensitiveMagic.valid_unit?(:ice)
|
112
|
+
refute CaseSensitiveMagic.valid_unit?("junk")
|
113
|
+
end
|
114
|
+
|
115
|
+
test ".name looks at the class name" do
|
116
|
+
module Example
|
117
|
+
class VeryComplexThing < Measured::Measurable ; end
|
118
|
+
end
|
119
|
+
|
120
|
+
assert_equal "case sensitive magic", CaseSensitiveMagic.name
|
121
|
+
assert_equal "measurable", Measured::Measurable.name
|
122
|
+
assert_equal "very complex thing", Example::VeryComplexThing.name
|
123
|
+
end
|
124
|
+
|
125
|
+
test "#convert_to raises on an invalid unit" do
|
126
|
+
assert_raises Measured::UnitError do
|
127
|
+
@magic.convert_to(:punch)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test "#convert_to returns a new object of the same type in the new unit" do
|
132
|
+
converted = @magic.convert_to(:arcane)
|
133
|
+
|
134
|
+
assert_equal converted, @magic
|
135
|
+
refute_equal converted.object_id, @magic.object_id
|
136
|
+
assert_equal BigDecimal(10), @magic.value
|
137
|
+
assert_equal "magic_missile", @magic.unit
|
138
|
+
assert_equal BigDecimal(1), converted.value
|
139
|
+
assert_equal "arcane", converted.unit
|
140
|
+
end
|
141
|
+
|
142
|
+
test "#convert_to from and to the same unit returns the same object" do
|
143
|
+
converted = @magic.convert_to(@magic.unit)
|
144
|
+
assert_equal converted.object_id, @magic.object_id
|
145
|
+
end
|
146
|
+
|
147
|
+
test "#convert_to! replaces the existing object with a new version in the new unit" do
|
148
|
+
converted = @magic.convert_to!(:arcane)
|
149
|
+
|
150
|
+
assert_equal converted, @magic
|
151
|
+
assert_equal BigDecimal(1), @magic.value
|
152
|
+
assert_equal "arcane", @magic.unit
|
153
|
+
assert_equal BigDecimal(1), converted.value
|
154
|
+
assert_equal "arcane", converted.unit
|
155
|
+
end
|
156
|
+
|
157
|
+
test "#to_s outputs the number and the unit" do
|
158
|
+
assert_equal "10 fireball", CaseSensitiveMagic.new(10, :fire).to_s
|
159
|
+
assert_equal "1.234 magic_missile", CaseSensitiveMagic.new("1.234", :magic_missile).to_s
|
160
|
+
end
|
161
|
+
|
162
|
+
test "#inspect shows the number and the unit" do
|
163
|
+
assert_equal "#<CaseSensitiveMagic: 10.0 fireball>", CaseSensitiveMagic.new(10, :fire).inspect
|
164
|
+
assert_equal "#<CaseSensitiveMagic: 1.234 magic_missile>", CaseSensitiveMagic.new(1.234, :magic_missile).inspect
|
165
|
+
end
|
166
|
+
|
167
|
+
test "#<=> compares regardless of the unit" do
|
168
|
+
assert_equal -1, @magic <=> CaseSensitiveMagic.new(10, :fire)
|
169
|
+
assert_equal 1, @magic <=> CaseSensitiveMagic.new(9, :magic_missile)
|
170
|
+
assert_equal 0, @magic <=> CaseSensitiveMagic.new(10, :magic_missile)
|
171
|
+
assert_equal -1, @magic <=> CaseSensitiveMagic.new(11, :magic_missile)
|
172
|
+
end
|
173
|
+
|
174
|
+
test "#<=> compares against zero" do
|
175
|
+
assert_equal 1, @magic <=> 0
|
176
|
+
assert_equal 1, @magic <=> BigDecimal.new(0)
|
177
|
+
assert_equal 1, @magic <=> 0.00
|
178
|
+
assert_equal -1, CaseSensitiveMagic.new(-1, :magic_missile) <=> 0
|
179
|
+
end
|
180
|
+
|
181
|
+
test "#== should be the same if the classes, unit, and amount match" do
|
182
|
+
assert @magic == @magic
|
183
|
+
assert CaseSensitiveMagic.new(10, :magic_missile) == CaseSensitiveMagic.new("10", "magic_missile")
|
184
|
+
assert CaseSensitiveMagic.new(1, :arcane) == CaseSensitiveMagic.new(10, :magic_missile)
|
185
|
+
refute CaseSensitiveMagic.new(1, :arcane) == CaseSensitiveMagic.new(10.1, :magic_missile)
|
186
|
+
end
|
187
|
+
|
188
|
+
test "#== should be the same if the classes and amount match but the unit does not so they convert" do
|
189
|
+
assert CaseSensitiveMagic.new(2, :magic_missile) == CaseSensitiveMagic.new("1", "ice")
|
190
|
+
end
|
191
|
+
|
192
|
+
test "#== compares against zero" do
|
193
|
+
assert CaseSensitiveMagic.new(0, :fire) == 0
|
194
|
+
assert CaseSensitiveMagic.new(0, :magic_missile) == 0
|
195
|
+
assert CaseSensitiveMagic.new(0, :fire) == BigDecimal.new(0)
|
196
|
+
assert CaseSensitiveMagic.new(0, :fire) == 0.00
|
197
|
+
refute @magic == 0
|
198
|
+
refute @magic == BigDecimal.new(0)
|
199
|
+
end
|
200
|
+
|
201
|
+
test "#> and #< should compare measurements" do
|
202
|
+
assert CaseSensitiveMagic.new(10, :magic_missile) < CaseSensitiveMagic.new(20, :magic_missile)
|
203
|
+
refute CaseSensitiveMagic.new(10, :magic_missile) > CaseSensitiveMagic.new(20, :magic_missile)
|
204
|
+
end
|
205
|
+
|
206
|
+
test "#> and #< should compare measurements of different units" do
|
207
|
+
assert CaseSensitiveMagic.new(10, :magic_missile) < CaseSensitiveMagic.new(100, :ice)
|
208
|
+
refute CaseSensitiveMagic.new(10, :magic_missile) > CaseSensitiveMagic.new(100, :ice)
|
209
|
+
end
|
210
|
+
|
211
|
+
test "#> and #< should compare against zero" do
|
212
|
+
assert @magic > 0
|
213
|
+
assert @magic > BigDecimal.new(0)
|
214
|
+
assert @magic > 0.00
|
215
|
+
assert CaseSensitiveMagic.new(-1, :arcane) < 0
|
216
|
+
refute @magic < 0
|
217
|
+
refute CaseSensitiveMagic.new(-1, :arcane) > 0
|
218
|
+
end
|
219
|
+
|
220
|
+
test "#eql? should be the same if the classes and amount match, and unit is converted" do
|
221
|
+
assert @magic == @magic
|
222
|
+
assert CaseSensitiveMagic.new(10, :magic_missile) == CaseSensitiveMagic.new("10", "magic_missile")
|
223
|
+
assert CaseSensitiveMagic.new(1, :arcane) == CaseSensitiveMagic.new(10, :magic_missile)
|
224
|
+
refute CaseSensitiveMagic.new(1, :arcane) == CaseSensitiveMagic.new(10.1, :magic_missile)
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
data/test/conversion_test.rb
CHANGED
@@ -66,20 +66,57 @@ class Measured::ConversionTest < ActiveSupport::TestCase
|
|
66
66
|
|
67
67
|
assert @conversion.unit?(:inch)
|
68
68
|
assert @conversion.unit?("m")
|
69
|
+
assert @conversion.unit?("M")
|
69
70
|
refute @conversion.unit?("in")
|
70
71
|
refute @conversion.unit?(:yard)
|
71
72
|
end
|
72
73
|
|
74
|
+
test "#unit? takes into account case_sensitive flag" do
|
75
|
+
conversion = Measured::Conversion.new(case_sensitive: true)
|
76
|
+
conversion.set_base :m
|
77
|
+
conversion.add :inch, aliases: [:in], value: "0.0254 meter"
|
78
|
+
|
79
|
+
assert conversion.unit?(:inch)
|
80
|
+
assert conversion.unit?("m")
|
81
|
+
refute conversion.unit?("M")
|
82
|
+
refute conversion.unit?("in")
|
83
|
+
end
|
84
|
+
|
85
|
+
test "#unit? with blank and nil arguments" do
|
86
|
+
refute @conversion.unit?("")
|
87
|
+
refute @conversion.unit?(nil)
|
88
|
+
end
|
89
|
+
|
73
90
|
test "#unit_or_alias? checks if the unit is part of the units but not aliases" do
|
74
91
|
@conversion.set_base :m
|
75
92
|
@conversion.add :inch, aliases: [:in], value: "0.0254 meter"
|
76
93
|
|
77
94
|
assert @conversion.unit_or_alias?(:inch)
|
78
95
|
assert @conversion.unit_or_alias?("m")
|
96
|
+
assert @conversion.unit_or_alias?(:IN)
|
79
97
|
assert @conversion.unit_or_alias?("in")
|
80
98
|
refute @conversion.unit_or_alias?(:yard)
|
81
99
|
end
|
82
100
|
|
101
|
+
test "#unit_or_alias? takes into account case_sensitive flag" do
|
102
|
+
conversion = Measured::Conversion.new(case_sensitive: true)
|
103
|
+
conversion.set_base :m
|
104
|
+
conversion.add :inch, aliases: [:in], value: "0.0254 meter"
|
105
|
+
|
106
|
+
assert conversion.unit_or_alias?(:inch)
|
107
|
+
assert conversion.unit_or_alias?("m")
|
108
|
+
refute conversion.unit_or_alias?(:M)
|
109
|
+
refute conversion.unit_or_alias?("IN")
|
110
|
+
end
|
111
|
+
|
112
|
+
test "#unit_or_alias? with blank and nil arguments" do
|
113
|
+
@conversion.set_base :m
|
114
|
+
@conversion.add :inch, aliases: [:in], value: "0.0254 meter"
|
115
|
+
|
116
|
+
refute @conversion.unit_or_alias?("")
|
117
|
+
refute @conversion.unit_or_alias?(nil)
|
118
|
+
end
|
119
|
+
|
83
120
|
test "#to_unit_name converts a unit name to its base unit" do
|
84
121
|
assert_equal "fireball", Magic.conversion.to_unit_name("fire")
|
85
122
|
end
|
data/test/support/fake_system.rb
CHANGED
@@ -18,6 +18,26 @@ class Magic < Measured::Measurable
|
|
18
18
|
|
19
19
|
end
|
20
20
|
|
21
|
+
class CaseSensitiveMagic < Measured::CaseSensitiveMeasurable
|
22
|
+
|
23
|
+
conversion.set_base :magic_missile,
|
24
|
+
aliases: [:magic_missiles]
|
25
|
+
|
26
|
+
conversion.add :fireball,
|
27
|
+
aliases: [:fire, :fireballs],
|
28
|
+
value: "2/3 magic_missile"
|
29
|
+
|
30
|
+
conversion.add :ice,
|
31
|
+
value: "2 magic_missile"
|
32
|
+
|
33
|
+
conversion.add :arcane,
|
34
|
+
value: "10 magic_missile"
|
35
|
+
|
36
|
+
conversion.add :ultima,
|
37
|
+
value: "10 arcane"
|
38
|
+
|
39
|
+
end
|
40
|
+
|
21
41
|
class OtherFakeSystem < Measured::Measurable
|
22
42
|
|
23
43
|
conversion.set_base :other_fake_base
|
data/test/unit_test.rb
CHANGED
@@ -13,6 +13,70 @@ class Measured::UnitTest < ActiveSupport::TestCase
|
|
13
13
|
assert_equal ["cake", "pie", "sweets"], Measured::Unit.new(:pie, aliases: ["cake", :sweets]).names
|
14
14
|
end
|
15
15
|
|
16
|
+
test "#name_eql?" do
|
17
|
+
assert @unit.name_eql?("pIe")
|
18
|
+
refute @unit.name_eql?("pastry")
|
19
|
+
end
|
20
|
+
|
21
|
+
test "#name_eql? with case_sensitive true" do
|
22
|
+
assert @unit.name_eql?("pie", case_sensitive: true)
|
23
|
+
refute @unit.name_eql?("Pie", case_sensitive: true)
|
24
|
+
end
|
25
|
+
|
26
|
+
test "#name_eql? with empty string" do
|
27
|
+
assert @unit.name_eql?("pie")
|
28
|
+
refute @unit.name_eql?("")
|
29
|
+
end
|
30
|
+
|
31
|
+
test "#names_include?" do
|
32
|
+
unit = Measured::Unit.new(:pie, aliases:["cake", "tart"])
|
33
|
+
assert unit.names_include?("pie")
|
34
|
+
assert unit.names_include?("caKe")
|
35
|
+
assert unit.names_include?("taRt")
|
36
|
+
refute unit.names_include?("pastry")
|
37
|
+
end
|
38
|
+
|
39
|
+
test "#names_include? with case_sensitive true" do
|
40
|
+
unit = Measured::Unit.new(:pie, aliases:["cake", "tart"])
|
41
|
+
assert unit.names_include?("pie", case_sensitive: true)
|
42
|
+
assert unit.names_include?("cake", case_sensitive: true)
|
43
|
+
refute unit.names_include?("TART", case_sensitive: true)
|
44
|
+
end
|
45
|
+
|
46
|
+
test "#names_include? with empty string" do
|
47
|
+
unit = Measured::Unit.new(:pie, aliases: ["cake", "tart"])
|
48
|
+
assert unit.names_include?("cake")
|
49
|
+
refute unit.names_include?("")
|
50
|
+
end
|
51
|
+
|
52
|
+
test "#add_alias with string" do
|
53
|
+
unit = Measured::Unit.new(:pie, aliases: ["cake"], value: "10 cake")
|
54
|
+
assert_equal ["cake", "pie"], unit.names
|
55
|
+
unit.add_alias("pastry")
|
56
|
+
assert_equal ["cake", "pastry", "pie"], unit.names
|
57
|
+
end
|
58
|
+
|
59
|
+
test "#add_alias with array" do
|
60
|
+
unit = Measured::Unit.new(:pie, aliases: ["cake"], value: "10 cake")
|
61
|
+
assert_equal ["cake", "pie"], unit.names
|
62
|
+
unit.add_alias(["pastry", "tart", "turnover"])
|
63
|
+
assert_equal ["cake", "pastry", "pie", "tart", "turnover"], unit.names
|
64
|
+
end
|
65
|
+
|
66
|
+
test "#add_alias with nil" do
|
67
|
+
unit = Measured::Unit.new(:pie, aliases: ["cake"], value: "10 cake")
|
68
|
+
assert_equal ["cake", "pie"], unit.names
|
69
|
+
unit.add_alias(nil)
|
70
|
+
assert_equal ["cake", "pie"], unit.names
|
71
|
+
end
|
72
|
+
|
73
|
+
test "#add_alias with empty string" do
|
74
|
+
unit = Measured::Unit.new(:pie, aliases: ["cake"], value: "10 cake")
|
75
|
+
assert_equal ["cake", "pie"], unit.names
|
76
|
+
unit.add_alias("")
|
77
|
+
assert_equal ["cake", "pie"], unit.names
|
78
|
+
end
|
79
|
+
|
16
80
|
test "#initialize parses out the unit and the number part" do
|
17
81
|
assert_equal BigDecimal(10), @unit.conversion_amount
|
18
82
|
assert_equal "cake", @unit.conversion_unit
|
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: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- lib/measured.rb
|
98
98
|
- lib/measured/arithmetic.rb
|
99
99
|
- lib/measured/base.rb
|
100
|
+
- lib/measured/case_sensitive_measurable.rb
|
100
101
|
- lib/measured/conversion.rb
|
101
102
|
- lib/measured/conversion_table.rb
|
102
103
|
- lib/measured/measurable.rb
|
@@ -108,6 +109,7 @@ files:
|
|
108
109
|
- repodb.yml
|
109
110
|
- shipit.rubygems.yml
|
110
111
|
- test/arithmetic_test.rb
|
112
|
+
- test/case_sensitive_measurable_test.rb
|
111
113
|
- test/conversion_table_test.rb
|
112
114
|
- test/conversion_test.rb
|
113
115
|
- test/measurable_test.rb
|
@@ -137,12 +139,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
139
|
version: '0'
|
138
140
|
requirements: []
|
139
141
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.2.
|
142
|
+
rubygems_version: 2.2.3
|
141
143
|
signing_key:
|
142
144
|
specification_version: 4
|
143
145
|
summary: Encapsulate measurements with their units in Ruby
|
144
146
|
test_files:
|
145
147
|
- test/arithmetic_test.rb
|
148
|
+
- test/case_sensitive_measurable_test.rb
|
146
149
|
- test/conversion_table_test.rb
|
147
150
|
- test/conversion_test.rb
|
148
151
|
- test/measurable_test.rb
|