measured 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 +4 -4
- data/README.md +0 -3
- data/lib/measured/conversion.rb +9 -11
- data/lib/measured/unit.rb +2 -20
- data/lib/measured/version.rb +1 -1
- data/test/conversion_test.rb +0 -22
- data/test/unit_test.rb +0 -47
- 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: 8c85f59c2c66ee777904c96802d3c647887dd1fb
|
4
|
+
data.tar.gz: 59e9b89428cab80194f786aa714cb9bb4396473c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afaf1dc4ad37532af6235a2b4737801096c7cfb0984f8c38c3038659db3558febc09b5770325b0cb246ed916fca852e21503bac853d0bbdd7de7aea4c579fde3
|
7
|
+
data.tar.gz: ad503908ae66eebe29f56830a414c072b75753fe4698b36402d73c58e2a8b53b333f249a27a325af0510a2610f36f8294036c769f1aa1cd1aabb8de7335916b2
|
data/README.md
CHANGED
@@ -181,7 +181,6 @@ class Measured::Thing < Measured::Measurable
|
|
181
181
|
|
182
182
|
conversion.add :another_unit, # Add a second unit to the system
|
183
183
|
aliases: [:au], # All units allow aliases, as long as they are unique
|
184
|
-
case_sensitive: true, # Defaults to false; applies to name and aliases
|
185
184
|
value: ["1.5 base_unit"] # The conversion rate to another unit
|
186
185
|
|
187
186
|
conversion.add :different_unit
|
@@ -192,8 +191,6 @@ end
|
|
192
191
|
|
193
192
|
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.
|
194
193
|
|
195
|
-
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.
|
196
|
-
|
197
194
|
You can also open up the existing classes and add a new conversion:
|
198
195
|
|
199
196
|
```ruby
|
data/lib/measured/conversion.rb
CHANGED
@@ -7,12 +7,12 @@ class Measured::Conversion
|
|
7
7
|
|
8
8
|
attr_reader :base_unit, :units
|
9
9
|
|
10
|
-
def set_base(unit_name, aliases: []
|
11
|
-
add_new_unit(unit_name, aliases: aliases,
|
10
|
+
def set_base(unit_name, aliases: [])
|
11
|
+
add_new_unit(unit_name, aliases: aliases, base: true)
|
12
12
|
end
|
13
13
|
|
14
|
-
def add(unit_name, aliases: [],
|
15
|
-
add_new_unit(unit_name, aliases: aliases,
|
14
|
+
def add(unit_name, aliases: [], value:)
|
15
|
+
add_new_unit(unit_name, aliases: aliases, value: value)
|
16
16
|
end
|
17
17
|
|
18
18
|
def unit_names_with_aliases
|
@@ -24,13 +24,11 @@ class Measured::Conversion
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def unit_or_alias?(name)
|
27
|
-
|
28
|
-
false
|
27
|
+
unit_names_with_aliases.include?(name.to_s)
|
29
28
|
end
|
30
29
|
|
31
30
|
def unit?(name)
|
32
|
-
|
33
|
-
false
|
31
|
+
unit_names.include?(name.to_s)
|
34
32
|
end
|
35
33
|
|
36
34
|
def to_unit_name(name)
|
@@ -55,7 +53,7 @@ class Measured::Conversion
|
|
55
53
|
|
56
54
|
private
|
57
55
|
|
58
|
-
def add_new_unit(unit_name, aliases:,
|
56
|
+
def add_new_unit(unit_name, aliases:, value: nil, base: false)
|
59
57
|
if base && @base_unit
|
60
58
|
raise Measured::UnitError, "Can only have one base unit. Adding #{ unit_name } but already defined #{ @base_unit }."
|
61
59
|
elsif !base && !@base_unit
|
@@ -64,7 +62,7 @@ class Measured::Conversion
|
|
64
62
|
|
65
63
|
check_for_duplicate_unit_names([unit_name] + aliases)
|
66
64
|
|
67
|
-
unit = Measured::Unit.new(unit_name, aliases: aliases,
|
65
|
+
unit = Measured::Unit.new(unit_name, aliases: aliases, value: value)
|
68
66
|
@units << unit
|
69
67
|
@base_unit = unit if base
|
70
68
|
|
@@ -81,7 +79,7 @@ class Measured::Conversion
|
|
81
79
|
|
82
80
|
def unit_for(name)
|
83
81
|
@units.each do |unit|
|
84
|
-
return unit if unit.
|
82
|
+
return unit if unit.names.include?(name.to_s)
|
85
83
|
end
|
86
84
|
|
87
85
|
raise Measured::UnitError, "Cannot find unit for #{ name }."
|
data/lib/measured/unit.rb
CHANGED
@@ -1,27 +1,14 @@
|
|
1
1
|
class Measured::Unit
|
2
2
|
include Comparable
|
3
3
|
|
4
|
-
def initialize(name, aliases: [],
|
4
|
+
def initialize(name, aliases: [], value: nil)
|
5
5
|
@name = name.to_s
|
6
6
|
@names = ([@name] + aliases.map{|n| n.to_s }).sort
|
7
7
|
|
8
|
-
@case_sensitive = case_sensitive
|
9
8
|
@conversion_amount, @conversion_unit = parse_value(value) if value
|
10
9
|
end
|
11
10
|
|
12
|
-
attr_reader :name, :names, :
|
13
|
-
|
14
|
-
def name_eql?(name_to_compare)
|
15
|
-
with_case_sensitivity(self.name).include?(with_case_sensitivity(name_to_compare).join)
|
16
|
-
end
|
17
|
-
|
18
|
-
def names_include?(name_to_compare)
|
19
|
-
with_case_sensitivity(self.names).include?(with_case_sensitivity(name_to_compare).join)
|
20
|
-
end
|
21
|
-
|
22
|
-
def add_alias(aliases)
|
23
|
-
@names = (@names << aliases).flatten.sort
|
24
|
-
end
|
11
|
+
attr_reader :name, :names, :conversion_amount, :conversion_unit
|
25
12
|
|
26
13
|
def to_s
|
27
14
|
if conversion_string
|
@@ -53,11 +40,6 @@ class Measured::Unit
|
|
53
40
|
|
54
41
|
private
|
55
42
|
|
56
|
-
def with_case_sensitivity(comparison)
|
57
|
-
comparison = [comparison].flatten
|
58
|
-
case_sensitive ? comparison : comparison.map(&:downcase)
|
59
|
-
end
|
60
|
-
|
61
43
|
def conversion_string
|
62
44
|
"#{ conversion_amount } #{ conversion_unit }" if @conversion_amount || @conversion_unit
|
63
45
|
end
|
data/lib/measured/version.rb
CHANGED
data/test/conversion_test.rb
CHANGED
@@ -66,42 +66,20 @@ class Measured::ConversionTest < ActiveSupport::TestCase
|
|
66
66
|
|
67
67
|
assert @conversion.unit?(:inch)
|
68
68
|
assert @conversion.unit?("m")
|
69
|
-
assert @conversion.unit?("M")
|
70
69
|
refute @conversion.unit?("in")
|
71
70
|
refute @conversion.unit?(:yard)
|
72
71
|
end
|
73
72
|
|
74
|
-
test "#unit? takes into account case_sensitive flag" do
|
75
|
-
@conversion.set_base :m, case_sensitive: true
|
76
|
-
@conversion.add :inch, aliases: [:in], value: "0.0254 meter", case_sensitive: true
|
77
|
-
|
78
|
-
assert @conversion.unit?(:inch)
|
79
|
-
assert @conversion.unit?("m")
|
80
|
-
refute @conversion.unit?("M")
|
81
|
-
refute @conversion.unit?("in")
|
82
|
-
end
|
83
|
-
|
84
73
|
test "#unit_or_alias? checks if the unit is part of the units but not aliases" do
|
85
74
|
@conversion.set_base :m
|
86
75
|
@conversion.add :inch, aliases: [:in], value: "0.0254 meter"
|
87
76
|
|
88
77
|
assert @conversion.unit_or_alias?(:inch)
|
89
78
|
assert @conversion.unit_or_alias?("m")
|
90
|
-
assert @conversion.unit_or_alias?(:IN)
|
91
79
|
assert @conversion.unit_or_alias?("in")
|
92
80
|
refute @conversion.unit_or_alias?(:yard)
|
93
81
|
end
|
94
82
|
|
95
|
-
test "#unit_or_alias? takes into account case_sensitive flag" do
|
96
|
-
@conversion.set_base :m, case_sensitive: true
|
97
|
-
@conversion.add :inch, aliases: [:in], value: "0.0254 meter", case_sensitive: true
|
98
|
-
|
99
|
-
assert @conversion.unit_or_alias?(:inch)
|
100
|
-
assert @conversion.unit_or_alias?("m")
|
101
|
-
refute @conversion.unit_or_alias?(:M)
|
102
|
-
refute @conversion.unit_or_alias?("IN")
|
103
|
-
end
|
104
|
-
|
105
83
|
test "#to_unit_name converts a unit name to its base unit" do
|
106
84
|
assert_equal "fireball", Magic.conversion.to_unit_name("fire")
|
107
85
|
end
|
data/test/unit_test.rb
CHANGED
@@ -13,53 +13,6 @@ 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 "case_sensitive flag default to false" do
|
17
|
-
assert_equal false, @unit.case_sensitive
|
18
|
-
end
|
19
|
-
|
20
|
-
test "#name_eql?" do
|
21
|
-
assert_equal true, @unit.name_eql?("pIe")
|
22
|
-
assert_equal false, @unit.name_eql?("pastry")
|
23
|
-
end
|
24
|
-
|
25
|
-
test "#names_include?" do
|
26
|
-
unit = Measured::Unit.new(:pie, aliases:["cake", "tart"])
|
27
|
-
assert_equal true, unit.names_include?("pie")
|
28
|
-
assert_equal true, unit.names_include?("caKe")
|
29
|
-
assert_equal true, unit.names_include?("taRt")
|
30
|
-
assert_equal false, unit.names_include?("pastry")
|
31
|
-
end
|
32
|
-
|
33
|
-
test "case_sensitive flag set to false" do
|
34
|
-
assert_equal false, Measured::Unit.new(:pie, case_sensitive: false).case_sensitive
|
35
|
-
end
|
36
|
-
|
37
|
-
test "case_sensitive flag set to true, #name_eql?" do
|
38
|
-
unit = Measured::Unit.new(:pie, case_sensitive: true)
|
39
|
-
assert_equal true, unit.name_eql?("pie")
|
40
|
-
assert_equal false, unit.name_eql?("pIe")
|
41
|
-
end
|
42
|
-
|
43
|
-
test "case_sensitive flag set to true, #names_include?" do
|
44
|
-
unit = Measured::Unit.new(:pie, aliases: ["cake", "tart", "pastry"], case_sensitive: true)
|
45
|
-
assert_equal true, unit.names_include?("cake")
|
46
|
-
assert_equal false, unit.names_include?("tArt")
|
47
|
-
end
|
48
|
-
|
49
|
-
test "#add_alias with string" do
|
50
|
-
unit = Measured::Unit.new(:pie, aliases: ["cake"], value: "10 cake")
|
51
|
-
assert_equal ["cake", "pie"], unit.names
|
52
|
-
unit.add_alias("pastry")
|
53
|
-
assert_equal ["cake", "pastry", "pie"], unit.names
|
54
|
-
end
|
55
|
-
|
56
|
-
test "#add_alias with array" do
|
57
|
-
unit = Measured::Unit.new(:pie, aliases: ["cake"], value: "10 cake")
|
58
|
-
assert_equal ["cake", "pie"], unit.names
|
59
|
-
unit.add_alias(["pastry", "tart", "turnover"])
|
60
|
-
assert_equal ["cake", "pastry", "pie", "tart", "turnover"], unit.names
|
61
|
-
end
|
62
|
-
|
63
16
|
test "#initialize parses out the unit and the number part" do
|
64
17
|
assert_equal BigDecimal(10), @unit.conversion_amount
|
65
18
|
assert_equal "cake", @unit.conversion_unit
|
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: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
137
|
version: '0'
|
138
138
|
requirements: []
|
139
139
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.2.
|
140
|
+
rubygems_version: 2.2.2
|
141
141
|
signing_key:
|
142
142
|
specification_version: 4
|
143
143
|
summary: Encapsulate measurements with their units in Ruby
|