convertable_values 0.0.1 → 0.0.2
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.
- data/VERSION +1 -1
- data/convertable_values.gemspec +1 -1
- data/lib/convertable_values.rb +36 -8
- data/test/test_convertable_values.rb +1 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/convertable_values.gemspec
CHANGED
data/lib/convertable_values.rb
CHANGED
@@ -18,10 +18,17 @@ module ConvertableValues
|
|
18
18
|
|
19
19
|
if new_value && unit_str
|
20
20
|
# store the value converted to the base unit corresponding to the given unit
|
21
|
-
|
22
|
-
|
21
|
+
if respond_to?(:write_attribute)
|
22
|
+
write_attribute(value_attr.to_sym, new_value.send(unit_str))
|
23
|
+
else
|
24
|
+
instance_variable_set("@#{value_attr}".to_sym, new_value.send(unit_str))
|
25
|
+
end
|
23
26
|
else
|
24
|
-
|
27
|
+
if respond_to?(:write_attribute)
|
28
|
+
write_attribute(value_attr.to_sym, new_value)
|
29
|
+
else
|
30
|
+
instance_variable_set("@#{value_attr}".to_sym, new_value)
|
31
|
+
end
|
25
32
|
end
|
26
33
|
end
|
27
34
|
|
@@ -31,19 +38,40 @@ module ConvertableValues
|
|
31
38
|
|
32
39
|
if unit_str
|
33
40
|
# return the value converted back to whatever unit was stored
|
34
|
-
|
41
|
+
if respond_to?(:read_attribute)
|
42
|
+
read_attribute(value_attr.to_sym).to(unit_str.to_sym)
|
43
|
+
else
|
44
|
+
instance_variable_get("@#{value_attr}".to_sym).to(unit_str.to_sym)
|
45
|
+
end
|
35
46
|
else
|
36
|
-
|
47
|
+
if respond_to?(:read_attribute)
|
48
|
+
read_attribute(value_attr.to_sym)
|
49
|
+
else
|
50
|
+
instance_variable_get("@#{value_attr}".to_sym)
|
51
|
+
end
|
37
52
|
end
|
38
53
|
end
|
39
54
|
|
40
55
|
# Create override method for updating value when unit is set/changed
|
41
56
|
define_method "#{unit_attr}=".to_sym do |new_unit|
|
42
|
-
|
43
|
-
|
57
|
+
if respond_to?(:read_attribute)
|
58
|
+
old_unit = read_attribute(unit_attr.to_sym)
|
59
|
+
else
|
60
|
+
old_unit = instance_variable_get("@#{unit_attr}".to_sym)
|
61
|
+
end
|
62
|
+
|
63
|
+
if respond_to?(:write_attribute)
|
64
|
+
write_attribute(unit_attr.to_sym, new_unit)
|
65
|
+
else
|
66
|
+
instance_variable_set("@#{unit_attr}".to_sym, new_unit)
|
67
|
+
end
|
44
68
|
|
45
69
|
# Re-assign the value so it will be converted properly
|
46
|
-
|
70
|
+
if respond_to?(:read_attribute)
|
71
|
+
value = read_attribute(value_attr.to_sym)
|
72
|
+
else
|
73
|
+
value = instance_variable_get("@#{value_attr}".to_sym)
|
74
|
+
end
|
47
75
|
send("#{value_attr}=".to_sym, value) if value && old_unit.nil?
|
48
76
|
|
49
77
|
new_unit
|
@@ -50,7 +50,7 @@ class TestConvertableValues < Test::Unit::TestCase
|
|
50
50
|
assert_in_delta @obj.instance_variable_get(:@value), 5000, P # Bypass accessor
|
51
51
|
end
|
52
52
|
|
53
|
-
should "
|
53
|
+
should "return the inputted unit on normal access" do
|
54
54
|
assert_in_delta @obj.value, 3.10685596, P
|
55
55
|
end
|
56
56
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: convertable_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stephen Blankenship
|