unitwise 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -6
- data/lib/unitwise.rb +1 -1
- data/lib/unitwise/measurement.rb +2 -0
- data/lib/unitwise/scale.rb +17 -2
- data/lib/unitwise/version.rb +1 -1
- data/test/support/scale_tests.rb +21 -0
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f22030e678a95cdbdb00f3c6218ffbae3c224c7
|
4
|
+
data.tar.gz: 14bbf7d6ca9cc4d60a099f307a3c52b29c37d1f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5884072087cb9a0261b2e22a7906ef2d27546bf30cb7c8a84fa8d8cc006fea840122bde7020e5ef23ea923afdf6c9c201f51b9c298315a900a32b3c9fad07dc
|
7
|
+
data.tar.gz: 2ef4a1a779b20492b8497bca65acbec5a0d24bba1bac0208c1784d0d366672b21ac16d07e28314ed605ac969607ad55492860210a88a4a5238021c484774f0ce
|
data/README.md
CHANGED
@@ -17,7 +17,7 @@ distance = 0.25.mile # => #<Unitwise::Measurement value=0.25 unit=mile>
|
|
17
17
|
time = 10.second # => #<Unitwise::Measurement value=10 unit=second>
|
18
18
|
mass = 2800.pound # => #<Unitwise::Measurement value=2800 unit=pound>
|
19
19
|
|
20
|
-
acceleration = 2 * distance / time ** 2
|
20
|
+
acceleration = 2.0 * distance / time ** 2
|
21
21
|
# => #<Unitwise::Measurement value=0.005 unit=[mi_us]/s2>
|
22
22
|
|
23
23
|
force = (mass * acceleration).to_lbf
|
@@ -26,7 +26,7 @@ force = (mass * acceleration).to_lbf
|
|
26
26
|
power = (force * distance / time).to_horsepower
|
27
27
|
# => #<Unitwise::Measurement value=551.4031264140402 unit=horsepower>
|
28
28
|
|
29
|
-
speed = ((2 * acceleration * distance) ** 0.5).convert_to("mile/hour")
|
29
|
+
speed = ((2.0 * acceleration * distance) ** 0.5).convert_to("mile/hour")
|
30
30
|
# => #<Unitwise::Measurement value=180.0 unit=mile/hour>
|
31
31
|
```
|
32
32
|
|
@@ -85,7 +85,7 @@ Unitwise is able to convert any unit within the UCUM spec to any other
|
|
85
85
|
compatible unit.
|
86
86
|
|
87
87
|
```ruby
|
88
|
-
5.kilometer.convert_to('mile')
|
88
|
+
5.0.kilometer.convert_to('mile')
|
89
89
|
# => #<Unitwise::Measurement value=3.106849747474748 unit=mile>
|
90
90
|
```
|
91
91
|
|
@@ -143,7 +143,7 @@ Unitwise(1000, 'kg.s-1.(m/s)2').to_kilowatt
|
|
143
143
|
You can add or subtract compatible measurements.
|
144
144
|
|
145
145
|
```ruby
|
146
|
-
2.meter + 3.inch - 1.yard
|
146
|
+
2.0.meter + 3.0.inch - 1.0.yard
|
147
147
|
# => #<Unitwise::Measurement value=1.1618 unit=meter>
|
148
148
|
```
|
149
149
|
|
@@ -166,7 +166,7 @@ Exponentiation is also supported.
|
|
166
166
|
|
167
167
|
```ruby
|
168
168
|
(10.cm ** 3).to_liter
|
169
|
-
# => #<Unitwise::Measurement value=1
|
169
|
+
# => #<Unitwise::Measurement value=1 unit=liter>
|
170
170
|
```
|
171
171
|
|
172
172
|
### Unit Names and Atom Codes
|
@@ -180,7 +180,7 @@ figure out most of the units by their name or symbol. If you find you need to
|
|
180
180
|
(or just want to be explicit) you use the UCUM atom codes without any
|
181
181
|
modification.
|
182
182
|
|
183
|
-
Just
|
183
|
+
Just for example, you can see here that there are actually a few versions of inch
|
184
184
|
and foot:
|
185
185
|
|
186
186
|
```ruby
|
data/lib/unitwise.rb
CHANGED
@@ -7,8 +7,8 @@ require 'bigdecimal'
|
|
7
7
|
|
8
8
|
require 'unitwise/version'
|
9
9
|
require 'unitwise/base'
|
10
|
-
require 'unitwise/expression'
|
11
10
|
require 'unitwise/compatible'
|
11
|
+
require 'unitwise/expression'
|
12
12
|
require 'unitwise/scale'
|
13
13
|
require 'unitwise/functional'
|
14
14
|
require 'unitwise/measurement'
|
data/lib/unitwise/measurement.rb
CHANGED
data/lib/unitwise/scale.rb
CHANGED
@@ -79,14 +79,29 @@ module Unitwise
|
|
79
79
|
end
|
80
80
|
memoize :depth
|
81
81
|
|
82
|
+
def simplified_value
|
83
|
+
if value.is_a?(Integer)
|
84
|
+
value
|
85
|
+
elsif (i = Integer(value)) == value
|
86
|
+
i
|
87
|
+
elsif value.is_a?(Float)
|
88
|
+
value
|
89
|
+
elsif (f = Float(value)) == value
|
90
|
+
f
|
91
|
+
else
|
92
|
+
value
|
93
|
+
end
|
94
|
+
end
|
95
|
+
memoize :simplified_value
|
96
|
+
|
82
97
|
# Convert to a simple string representing the scale.
|
83
98
|
# @api public
|
84
99
|
def to_s
|
85
|
-
"#{
|
100
|
+
"#{simplified_value} #{unit}"
|
86
101
|
end
|
87
102
|
|
88
103
|
def inspect
|
89
|
-
"#<#{self.class} value=#{
|
104
|
+
"#<#{self.class} value=#{simplified_value} unit=#{unit}>"
|
90
105
|
end
|
91
106
|
|
92
107
|
# Redefine hash for apropriate hash/key lookup
|
data/lib/unitwise/version.rb
CHANGED
data/test/support/scale_tests.rb
CHANGED
@@ -85,6 +85,27 @@ module ScaleTests
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
+
describe "#simplified_value" do
|
89
|
+
describe "when the value is equivalent to an Integer" do
|
90
|
+
it "should convert from a Rational" do
|
91
|
+
result = described_class.new(Rational(20,2), 'foot').simplified_value
|
92
|
+
result.must_equal 10
|
93
|
+
result.must_be_kind_of(Integer)
|
94
|
+
end
|
95
|
+
it "should convert from a Float" do
|
96
|
+
result = described_class.new(4.0, 'foot').simplified_value
|
97
|
+
result.must_equal 4
|
98
|
+
result.must_be_kind_of(Integer)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
describe "when the value is equivalent to a Float" do
|
102
|
+
it "should convert from a BigDecimal" do
|
103
|
+
result = described_class.new(BigDecimal("1.5"), 'foot').simplified_value
|
104
|
+
result.must_equal 1.5
|
105
|
+
result.must_be_kind_of(Float)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
88
109
|
end
|
89
110
|
end
|
90
111
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unitwise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lewis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: liner
|
@@ -270,4 +270,3 @@ test_files:
|
|
270
270
|
- test/unitwise/term_test.rb
|
271
271
|
- test/unitwise/unit_test.rb
|
272
272
|
- test/unitwise_test.rb
|
273
|
-
has_rdoc:
|