unitwise 0.3.1 → 0.3.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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +17 -1
- data/README.md +17 -1
- data/lib/unitwise/atom.rb +1 -1
- data/lib/unitwise/base.rb +6 -6
- data/lib/unitwise/expression.rb +16 -0
- data/lib/unitwise/expression/composer.rb +1 -1
- data/lib/unitwise/measurement.rb +90 -4
- data/lib/unitwise/scale.rb +42 -16
- data/lib/unitwise/standard.rb +4 -0
- data/lib/unitwise/term.rb +7 -26
- data/lib/unitwise/unit.rb +2 -5
- data/lib/unitwise/version.rb +1 -1
- data/test/support/scale_tests.rb +101 -0
- data/test/unitwise/measurement_test.rb +22 -47
- data/test/unitwise/scale_test.rb +7 -0
- data/test/unitwise/term_test.rb +1 -1
- metadata +28 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebcd2779e6a54ca23cc3cd644f3720bd9de8846d
|
4
|
+
data.tar.gz: 4bc1072fe92630814f05e9402e5cc5ac8983ce94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5de61cb9d586961307d03e8c3db35ba3d851d6cc8dcdfd8944c2c6d4b6e9e6833abe82c0fe498d1924bd3a23927c980a7c0fa252ba5e931d848afd10fa278672
|
7
|
+
data.tar.gz: 9b01d1fc8f63294e72bd20060f411c8a54d5ffffe7cb872b504728aa0ea8e4c189f2df696a50bb392b291c1582629c852e7dcbeab2634429b61f3883f4f95f46
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.1.0
|
data/.travis.yml
CHANGED
@@ -1,4 +1,20 @@
|
|
1
1
|
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
bundler_args: --without yard pry
|
2
4
|
rvm:
|
3
5
|
- 1.9.3
|
4
|
-
- 2.0.0
|
6
|
+
- 2.0.0
|
7
|
+
- 2.1.0
|
8
|
+
- ruby-head
|
9
|
+
- rbx
|
10
|
+
- rbx-2
|
11
|
+
matrix:
|
12
|
+
include:
|
13
|
+
- rvm: jruby-19mode
|
14
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
15
|
+
- rvm: jruby-head
|
16
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
17
|
+
allow_failure:
|
18
|
+
- rvm: ruby-head
|
19
|
+
- rvm: jruby-head
|
20
|
+
fast_finish: true
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ or require the core extensions for some syntactic sugar.
|
|
29
29
|
```ruby
|
30
30
|
require 'unitwise/ext'
|
31
31
|
|
32
|
-
1.convert(liter)
|
32
|
+
1.convert('liter')
|
33
33
|
# => <Unitwise::Measurement 1 liter>
|
34
34
|
|
35
35
|
4.teaspoon
|
@@ -180,6 +180,22 @@ You can also get the official list from the UCUM website in XML format at
|
|
180
180
|
or a YAML version within this repo
|
181
181
|
[github.com/joshwlewis/unitwise/tree/master/data](//github.com/joshwlewis/unitwise/tree/master/data).
|
182
182
|
|
183
|
+
### Supported Ruby Versions
|
184
|
+
|
185
|
+
This library aims to support and is tested against the following Ruby
|
186
|
+
implementations:
|
187
|
+
|
188
|
+
* Ruby 1.9.3
|
189
|
+
* Ruby 2.0.0
|
190
|
+
* Ruby 2.1.0
|
191
|
+
* [JRuby](http://jruby.org/)
|
192
|
+
* [Rubinius](http://rubini.us/)
|
193
|
+
|
194
|
+
If something doesn't work on one of these versions, it's a bug.
|
195
|
+
|
196
|
+
This library may inadvertently work (or seem to work) on other Ruby versions or
|
197
|
+
implementations, however support will only be provided for the implementations
|
198
|
+
listed above.
|
183
199
|
|
184
200
|
## Installation
|
185
201
|
|
data/lib/unitwise/atom.rb
CHANGED
@@ -116,7 +116,7 @@ module Unitwise
|
|
116
116
|
# depths. This method returns all of this atoms base level terms.
|
117
117
|
# @return [Array] An array containing base Unitwise::Term
|
118
118
|
def root_terms
|
119
|
-
base? ? [Term.new(
|
119
|
+
base? ? [Term.new(atom_code: primary_code)] : scale.root_terms
|
120
120
|
end
|
121
121
|
|
122
122
|
end
|
data/lib/unitwise/base.rb
CHANGED
@@ -7,12 +7,8 @@ module Unitwise
|
|
7
7
|
@all ||= data.map{|d| self.new d }
|
8
8
|
end
|
9
9
|
|
10
|
-
def self.find(
|
11
|
-
|
12
|
-
if found = find_by(method, string)
|
13
|
-
return found
|
14
|
-
end
|
15
|
-
end
|
10
|
+
def self.find(term)
|
11
|
+
all.find { |i| i.search_strings.any? { |string| string == term } }
|
16
12
|
end
|
17
13
|
|
18
14
|
def self.find_by(method, string)
|
@@ -46,5 +42,9 @@ module Unitwise
|
|
46
42
|
[primary_code, secondary_code, names, slugs, symbol].flatten.compact
|
47
43
|
end
|
48
44
|
|
45
|
+
def to_s
|
46
|
+
primary_code
|
47
|
+
end
|
48
|
+
|
49
49
|
end
|
50
50
|
end
|
data/lib/unitwise/expression.rb
CHANGED
@@ -7,12 +7,28 @@ require 'unitwise/expression/composer'
|
|
7
7
|
require 'unitwise/expression/decomposer'
|
8
8
|
|
9
9
|
module Unitwise
|
10
|
+
# The Expression module encompases all functions around encoding and decoding
|
11
|
+
# strings into Measurement::Units and vice-versa.
|
10
12
|
module Expression
|
11
13
|
class << self
|
14
|
+
# Build a string representation of a collection of terms
|
15
|
+
# @param terms [Array]
|
16
|
+
# @return [String]
|
17
|
+
# @example
|
18
|
+
# Unitwise::Expression.compose(terms) # => "m2/s2"
|
19
|
+
# @api public
|
12
20
|
def compose(terms)
|
13
21
|
Composer.new(terms).expression
|
14
22
|
end
|
15
23
|
|
24
|
+
# Convert a string representation of a unit, and turn it into a
|
25
|
+
# an array of terms
|
26
|
+
# @param expression [String] The string you wish to convert
|
27
|
+
# @return [Array]
|
28
|
+
# @example
|
29
|
+
# Unitwise::Expression.decompose("m2/s2")
|
30
|
+
# # => [<Unitwise::Term m2>, <Unitwise::Term s-2>]
|
31
|
+
# @api public
|
16
32
|
def decompose(expression)
|
17
33
|
expression = expression.to_s
|
18
34
|
@decompose ||= {}
|
data/lib/unitwise/measurement.rb
CHANGED
@@ -1,13 +1,31 @@
|
|
1
1
|
module Unitwise
|
2
|
+
# A Measurement is a combination of a numeric value and a unit. You can think
|
3
|
+
# of this as a type of vector where the direction is the unit designation and
|
4
|
+
# the value is the magnitued. This is the primary class that outside code
|
5
|
+
# will interact with. Comes with conversion, comparison, and math methods.
|
2
6
|
class Measurement < Scale
|
3
7
|
|
4
|
-
|
5
|
-
|
8
|
+
# Create a new Measurement
|
9
|
+
# @param value [Numeric] The scalar value for the measurement
|
10
|
+
# @param unit [String, Measurement::Unit] Either a string expression, or a
|
11
|
+
# Measurement::Unit
|
12
|
+
# @example
|
13
|
+
# Unitwise::Measurement.new(20, 'm/s') # => #<Unitwise::Measurement 20 m/s>
|
14
|
+
# @api public
|
15
|
+
def initialize(*args)
|
16
|
+
super(*args)
|
6
17
|
if terms.nil?
|
7
18
|
raise ExpressionError, "Could not evaluate `#{unit}`."
|
8
19
|
end
|
9
20
|
end
|
10
21
|
|
22
|
+
# Convert this measurement to a compatible unit.
|
23
|
+
# @param other_unit [String, Measurement::Unit] Either a string expression
|
24
|
+
# or a Measurement::Unit
|
25
|
+
# @example
|
26
|
+
# measurement1.convert('foot')
|
27
|
+
# measurement2.convert('kilogram')
|
28
|
+
# @api public
|
11
29
|
def convert(other_unit)
|
12
30
|
other_unit = Unit.new(other_unit)
|
13
31
|
if similar_to?(other_unit)
|
@@ -17,22 +35,49 @@ module Unitwise
|
|
17
35
|
end
|
18
36
|
end
|
19
37
|
|
38
|
+
# Multiply this measurement by a number or another measurement
|
39
|
+
# @param other [Numeric, Unitwise::Measurement]
|
40
|
+
# @example
|
41
|
+
# measurent * 5
|
42
|
+
# measurement * some_other_measurement
|
43
|
+
# @api public
|
20
44
|
def *(other)
|
21
45
|
operate(:*, other) || raise(TypeError, "Can't multiply #{inspect} by #{other}.")
|
22
46
|
end
|
23
47
|
|
48
|
+
# Divide this measurement by a number or another measurement
|
49
|
+
# @param (see #*)
|
50
|
+
# @example
|
51
|
+
# measurement / 2
|
52
|
+
# measurement / some_other_measurement
|
53
|
+
# @api public
|
24
54
|
def /(other)
|
25
55
|
operate(:/, other) || raise(TypeError, "Can't divide #{inspect} by #{other}")
|
26
56
|
end
|
27
57
|
|
58
|
+
# Add another measurement to this unit. Units must be compatible.
|
59
|
+
# @param other [Unitwise::Measurement]
|
60
|
+
# @example
|
61
|
+
# measurement + some_other_measurement
|
62
|
+
# @api public
|
28
63
|
def +(other)
|
29
64
|
combine(:+, other) || raise(TypeError, "Can't add #{other} to #{inspect}.")
|
30
65
|
end
|
31
66
|
|
67
|
+
# Subtract another measurement from this unit. Units must be compatible.
|
68
|
+
# @param (see #+)
|
69
|
+
# @example
|
70
|
+
# measurement - some_other_measurement
|
71
|
+
# @api public
|
32
72
|
def -(other)
|
33
73
|
combine(:-, other) || raise(TypeError, "Can't subtract #{other} from #{inspect}.")
|
34
74
|
end
|
35
75
|
|
76
|
+
# Raise a measurement to a numeric power.
|
77
|
+
# @param number [Numeric]
|
78
|
+
# @example
|
79
|
+
# measurement ** 2
|
80
|
+
# @api public
|
36
81
|
def **(number)
|
37
82
|
if number.is_a?(Numeric)
|
38
83
|
new( value ** number, unit ** number )
|
@@ -41,6 +86,12 @@ module Unitwise
|
|
41
86
|
end
|
42
87
|
end
|
43
88
|
|
89
|
+
# Coerce a numeric to a a measurement for mathematical operations
|
90
|
+
# @param other [Numeric]
|
91
|
+
# @example
|
92
|
+
# 2.5 * measurement
|
93
|
+
# 4 / measurement
|
94
|
+
# @api public
|
44
95
|
def coerce(other)
|
45
96
|
case other
|
46
97
|
when Numeric
|
@@ -50,6 +101,35 @@ module Unitwise
|
|
50
101
|
end
|
51
102
|
end
|
52
103
|
|
104
|
+
# Convert a measurement to an Integer.
|
105
|
+
# @example
|
106
|
+
# measurement.to_i # => 4
|
107
|
+
# @api public
|
108
|
+
def to_i
|
109
|
+
Integer(value)
|
110
|
+
end
|
111
|
+
alias :to_int :to_i
|
112
|
+
|
113
|
+
# Convert a measurement to a Float.
|
114
|
+
# @example
|
115
|
+
# measurement.to_f # => 4.25
|
116
|
+
# @api public
|
117
|
+
def to_f
|
118
|
+
Float(value)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Convert a measurement to a Rational.
|
122
|
+
# @example
|
123
|
+
# measurement.to_r # => (17/4)
|
124
|
+
# @api public
|
125
|
+
def to_r
|
126
|
+
Rational(value)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Will attempt to convert to a unit by the method name.
|
130
|
+
# @example
|
131
|
+
# measurement.foot # => <Unitwise::Measurement 4 foot>
|
132
|
+
# @api semipublic
|
53
133
|
def method_missing(meth, *args, &block)
|
54
134
|
if Unitwise::Expression.decompose(meth)
|
55
135
|
self.convert(meth)
|
@@ -60,10 +140,14 @@ module Unitwise
|
|
60
140
|
|
61
141
|
private
|
62
142
|
|
143
|
+
# Helper method to create a new instance from this instance
|
144
|
+
# @api private
|
63
145
|
def new(*args)
|
64
146
|
self.class.new(*args)
|
65
147
|
end
|
66
148
|
|
149
|
+
# Determine value of the unit after conversion to another unit
|
150
|
+
# @api private
|
67
151
|
def converted_value(other_unit)
|
68
152
|
if unit.special?
|
69
153
|
if other_unit.special?
|
@@ -80,14 +164,16 @@ module Unitwise
|
|
80
164
|
end
|
81
165
|
end
|
82
166
|
|
83
|
-
#
|
167
|
+
# Add or subtract other unit
|
168
|
+
# @api private
|
84
169
|
def combine(operator, other)
|
85
170
|
if similar_to?(other)
|
86
171
|
new(value.send(operator, other.convert(unit).value), unit)
|
87
172
|
end
|
88
173
|
end
|
89
174
|
|
90
|
-
#
|
175
|
+
# Multiply or divide other unit
|
176
|
+
# @api private
|
91
177
|
def operate(operator, other)
|
92
178
|
if other.is_a?(Numeric)
|
93
179
|
new(value.send(operator, other), unit)
|
data/lib/unitwise/scale.rb
CHANGED
@@ -1,65 +1,91 @@
|
|
1
1
|
module Unitwise
|
2
|
+
# A Unitwise::Scale represents a value and a unit, sort of like a vector, it
|
3
|
+
# has two components. In this case, it's a value and unit rather than a
|
4
|
+
# magnitude and direction. This class should be considered mostly privateish.
|
2
5
|
class Scale
|
3
|
-
|
6
|
+
liner :value, :unit
|
4
7
|
|
5
8
|
include Unitwise::Composable
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@unit = Unit.new(unit.to_s)
|
13
|
-
end
|
10
|
+
# The unit associated with this scale.
|
11
|
+
# @return [Unitwise::Unit]
|
12
|
+
# @api public
|
13
|
+
def unit
|
14
|
+
@unit.is_a?(Unit) ? @unit : Unit.new(@unit)
|
14
15
|
end
|
15
16
|
|
17
|
+
# Duplicate this instance
|
18
|
+
# @return [Unitwise::Unit]
|
19
|
+
# @api public
|
16
20
|
def dup
|
17
21
|
self.class.new(value, unit)
|
18
22
|
end
|
19
23
|
|
24
|
+
# List the atoms associated with this scale's unit.
|
25
|
+
# @return [Array]
|
26
|
+
# @api public
|
20
27
|
def atoms
|
21
28
|
unit.atoms
|
22
29
|
end
|
23
30
|
|
31
|
+
# List the atoms associated with this scale's unit.
|
32
|
+
# @return [Array]
|
33
|
+
# @api public
|
24
34
|
def terms
|
25
35
|
unit.terms
|
26
36
|
end
|
27
37
|
|
38
|
+
# Is this scale's unit special?
|
39
|
+
# @return [true, false]
|
40
|
+
# @api public
|
28
41
|
def special?
|
29
42
|
unit.special?
|
30
43
|
end
|
31
44
|
|
45
|
+
# Return a converted value for this scale, based on it's function for
|
46
|
+
# scales with special units.
|
47
|
+
# @param x [Numeric] Value to convert to or from
|
48
|
+
# @param forward [true, false] whether to convert to this unit or from it.
|
49
|
+
# @return [Numeric]
|
50
|
+
# @api public
|
32
51
|
def functional(x=value, forward=true)
|
33
52
|
unit.functional(x, forward)
|
34
53
|
end
|
35
54
|
|
55
|
+
# Return a scalar value for non-special units, this will be some ratio of a
|
56
|
+
# child base unit.
|
57
|
+
# @return [Numeric]
|
58
|
+
# @api public
|
36
59
|
def scalar
|
37
60
|
value * unit.scalar
|
38
61
|
end
|
39
62
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
63
|
+
# The base units this scale's unit is derived from
|
64
|
+
# @return [Array] An array of Unitwise::Term
|
65
|
+
# @api public
|
44
66
|
def root_terms
|
45
67
|
unit.root_terms
|
46
68
|
end
|
47
69
|
|
70
|
+
# How far away is this instances unit from the deepest leve atom.
|
71
|
+
# @return [Integer]
|
72
|
+
# @api public
|
48
73
|
def depth
|
49
74
|
unit.depth + 1
|
50
75
|
end
|
51
76
|
|
77
|
+
# Is this the deepest level scale in the scale chain?
|
78
|
+
# @return [true, false]
|
79
|
+
# @api public
|
52
80
|
def terminal?
|
53
81
|
depth <= 3
|
54
82
|
end
|
55
83
|
|
84
|
+
# Convert to a simple string representing the scale.
|
85
|
+
# @api public
|
56
86
|
def to_s
|
57
87
|
"#{value} #{unit}"
|
58
88
|
end
|
59
89
|
|
60
|
-
def inspect
|
61
|
-
"<#{self.class} #{to_s}>"
|
62
|
-
end
|
63
|
-
|
64
90
|
end
|
65
91
|
end
|
data/lib/unitwise/standard.rb
CHANGED
@@ -8,6 +8,10 @@ require 'unitwise/standard/scale'
|
|
8
8
|
require 'unitwise/standard/function'
|
9
9
|
|
10
10
|
module Unitwise
|
11
|
+
# The Standard module is responsible for fetching the UCUM specification unit
|
12
|
+
# standards and translating them into yaml files. This code is only used for
|
13
|
+
# by the rake task `rake unitwise:update_standard` and as such is not
|
14
|
+
# normally loaded.
|
11
15
|
module Standard
|
12
16
|
HOST = "unitsofmeasure.org"
|
13
17
|
PATH = "/ucum-essence.xml"
|
data/lib/unitwise/term.rb
CHANGED
@@ -1,24 +1,15 @@
|
|
1
1
|
require 'signed_multiset'
|
2
2
|
module Unitwise
|
3
|
-
class Term
|
4
|
-
liner :atom_code, :prefix_code, :atom, :prefix, :factor, :exponent, :annotation
|
3
|
+
class Term < Liner.new(:atom, :prefix, :factor, :exponent, :annotation)
|
5
4
|
|
6
5
|
include Unitwise::Composable
|
7
6
|
|
8
|
-
def
|
9
|
-
|
7
|
+
def atom=(value)
|
8
|
+
value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
|
10
9
|
end
|
11
10
|
|
12
|
-
def prefix
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
def atom_code
|
17
|
-
@atom_code ||= (@atom.primary_code if @atom)
|
18
|
-
end
|
19
|
-
|
20
|
-
def atom
|
21
|
-
@atom ||= (Atom.find(@atom_code) if @atom_code)
|
11
|
+
def prefix=(value)
|
12
|
+
value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s))
|
22
13
|
end
|
23
14
|
|
24
15
|
def special?
|
@@ -59,12 +50,6 @@ module Unitwise
|
|
59
50
|
end
|
60
51
|
end
|
61
52
|
|
62
|
-
def to_hash
|
63
|
-
[:prefix, :atom, :exponent, :factor, :annotation].inject({}) do |h, a|
|
64
|
-
h[a] = send a; h
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
53
|
def *(other)
|
69
54
|
if other.respond_to?(:terms)
|
70
55
|
Unit.new(other.terms << self)
|
@@ -90,12 +75,8 @@ module Unitwise
|
|
90
75
|
end
|
91
76
|
|
92
77
|
def to_s
|
93
|
-
[(factor if factor != 1),
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
def inspect
|
98
|
-
"<#{self.class} #{to_s}>"
|
78
|
+
[(factor if factor != 1), prefix.to_s,
|
79
|
+
atom.to_s, (exponent if exponent != 1)].compact.join('')
|
99
80
|
end
|
100
81
|
|
101
82
|
end
|
data/lib/unitwise/unit.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Unitwise
|
2
2
|
class Unit
|
3
|
+
liner :expression, :terms
|
4
|
+
|
3
5
|
include Unitwise::Composable
|
4
6
|
|
5
7
|
def initialize(input)
|
@@ -85,10 +87,5 @@ module Unitwise
|
|
85
87
|
def to_s
|
86
88
|
expression
|
87
89
|
end
|
88
|
-
|
89
|
-
def inspect
|
90
|
-
"<#{self.class} #{to_s}>"
|
91
|
-
end
|
92
|
-
|
93
90
|
end
|
94
91
|
end
|
data/lib/unitwise/version.rb
CHANGED
@@ -0,0 +1,101 @@
|
|
1
|
+
# Shared examples for Unitwise::Scale and Unitwise::Measurement
|
2
|
+
module ScaleTests
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
subject { described_class.new(4, "J") }
|
6
|
+
|
7
|
+
let(:mph) { Unitwise::Measurement.new(60, '[mi_i]/h') }
|
8
|
+
let(:kmh) { Unitwise::Measurement.new(100, 'km/h') }
|
9
|
+
let(:mile) { Unitwise::Measurement.new(3, '[mi_i]') }
|
10
|
+
let(:hpm) { Unitwise::Measurement.new(6, 'h/[mi_i]') }
|
11
|
+
let(:cui) { Unitwise::Measurement.new(12, "[in_i]3") }
|
12
|
+
let(:cel) { Unitwise::Measurement.new(22, 'Cel') }
|
13
|
+
let(:k) { Unitwise::Measurement.new(373.15, 'K') }
|
14
|
+
let(:f) { Unitwise::Measurement.new(98.6, '[degF]')}
|
15
|
+
let(:r) { Unitwise::Measurement.new(491.67, '[degR]') }
|
16
|
+
|
17
|
+
describe "#new" do
|
18
|
+
it "must set attributes" do
|
19
|
+
subject.value.must_equal(4)
|
20
|
+
subject.unit.to_s.must_equal('J')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#unit" do
|
25
|
+
it "must be a unit" do
|
26
|
+
subject.must_respond_to(:unit)
|
27
|
+
subject.unit.must_be_instance_of(Unitwise::Unit)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#root_terms" do
|
32
|
+
it "must be a collection of terms" do
|
33
|
+
subject.must_respond_to(:root_terms)
|
34
|
+
subject.root_terms.must_be_kind_of Enumerable
|
35
|
+
subject.root_terms.sample.must_be_instance_of(Unitwise::Term)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#dup" do
|
40
|
+
it "must return a new instance" do
|
41
|
+
subject.must_respond_to(:dup)
|
42
|
+
subject.dup.must_be_instance_of(described_class)
|
43
|
+
subject.dup.value.must_equal subject.value
|
44
|
+
subject.dup.unit.to_s.must_equal subject.unit.to_s
|
45
|
+
subject.dup.object_id.wont_equal subject.dup.object_id
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#terms" do
|
50
|
+
it "must return an array of terms" do
|
51
|
+
subject.terms.must_be_kind_of(Enumerable)
|
52
|
+
subject.terms.sample.must_be_kind_of(Unitwise::Term)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#atoms" do
|
57
|
+
it "must return an array of atoms" do
|
58
|
+
subject.atoms.must_be_kind_of(Enumerable)
|
59
|
+
subject.atoms.sample.must_be_kind_of(Unitwise::Atom)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#scalar" do
|
64
|
+
it "must return value relative to terminal atoms" do
|
65
|
+
subject.scalar.must_equal 4000
|
66
|
+
mph.scalar.must_equal 26.8224
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "#functional" do
|
71
|
+
it "must return a converted value" do
|
72
|
+
cel.functional(0,true).must_equal -273.15
|
73
|
+
end
|
74
|
+
it "must return a de-converted value" do
|
75
|
+
cel.functional(0,false).must_equal 273.15
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#special?" do
|
80
|
+
it "must return true when unit is special, false otherwise" do
|
81
|
+
subject.special?.must_equal false
|
82
|
+
cel.special?.must_equal true
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "#depth" do
|
87
|
+
it "must return a number indicating how far down the rabbit hole goes" do
|
88
|
+
subject.depth.must_equal 11
|
89
|
+
k.depth.must_equal 3
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "#terminal?" do
|
94
|
+
it "must return true for the last of kind in the chain" do
|
95
|
+
subject.terminal?.must_equal false
|
96
|
+
k.terminal?.must_equal true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -1,59 +1,16 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'support/scale_tests'
|
2
3
|
|
3
4
|
describe Unitwise::Measurement do
|
4
|
-
|
5
|
+
let(:described_class) { Unitwise::Measurement }
|
6
|
+
include ScaleTests
|
7
|
+
|
5
8
|
describe "#new" do
|
6
|
-
it "must set attributes" do
|
7
|
-
subject.value.must_equal(1)
|
8
|
-
subject.unit.to_s.must_equal('m/s')
|
9
|
-
end
|
10
9
|
it "should raise an error for unknown units" do
|
11
10
|
->{ Unitwise::Measurement.new(1,"funkitron") }.must_raise(Unitwise::ExpressionError)
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
15
|
-
describe "#unit" do
|
16
|
-
it "must be a unit" do
|
17
|
-
subject.must_respond_to(:unit)
|
18
|
-
subject.unit.must_be_instance_of(Unitwise::Unit)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "#root_terms" do
|
23
|
-
it "must be a collection of terms" do
|
24
|
-
subject.must_respond_to(:root_terms)
|
25
|
-
subject.root_terms.must_be_kind_of Enumerable
|
26
|
-
subject.root_terms.sample.must_be_instance_of(Unitwise::Term)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "#dup" do
|
31
|
-
it "must return a new instance" do
|
32
|
-
subject.must_respond_to(:dup)
|
33
|
-
subject.dup.must_be_instance_of(Unitwise::Measurement)
|
34
|
-
subject.dup.value.must_equal subject.value
|
35
|
-
subject.dup.unit.to_s.must_equal subject.unit.to_s
|
36
|
-
subject.dup.object_id.wont_equal subject.dup.object_id
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
let(:mph) { Unitwise::Measurement.new(60, '[mi_i]/h') }
|
41
|
-
let(:kmh) { Unitwise::Measurement.new(100, 'km/h') }
|
42
|
-
let(:mile) { Unitwise::Measurement.new(3, '[mi_i]') }
|
43
|
-
let(:hpm) { Unitwise::Measurement.new(6, 'h/[mi_i]') }
|
44
|
-
let(:cui) { Unitwise::Measurement.new(12, "[in_i]3") }
|
45
|
-
let(:cel) { Unitwise::Measurement.new(22, 'Cel') }
|
46
|
-
let(:k) { Unitwise::Measurement.new(373.15, 'K') }
|
47
|
-
let(:f) { Unitwise::Measurement.new(98.6, '[degF]')}
|
48
|
-
let(:r) { Unitwise::Measurement.new(491.67, '[degR]') }
|
49
|
-
|
50
|
-
describe "#scalar" do
|
51
|
-
it "must return value relative to terminal atoms" do
|
52
|
-
subject.scalar.must_equal 1
|
53
|
-
mph.scalar.must_equal 26.8224
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
14
|
describe "#convert" do
|
58
15
|
it "must convert to a similar unit code" do
|
59
16
|
mph.convert('km/h').value.must_equal 96.56063999999999
|
@@ -188,4 +145,22 @@ describe Unitwise::Measurement do
|
|
188
145
|
|
189
146
|
end
|
190
147
|
|
148
|
+
describe "#to_f" do
|
149
|
+
it "must convert to a float" do
|
150
|
+
f.to_f.must_be_kind_of(Float)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "#to_i" do
|
155
|
+
it "must convert to an integer" do
|
156
|
+
k.to_i.must_be_kind_of(Integer)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe "#to_r" do
|
161
|
+
it "must convert to a rational" do
|
162
|
+
cel.to_r.must_be_kind_of(Rational)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
191
166
|
end
|
data/test/unitwise/term_test.rb
CHANGED
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
describe Unitwise::Term do
|
4
4
|
describe "instance" do
|
5
|
-
subject { Unitwise::Term.new(
|
5
|
+
subject { Unitwise::Term.new(atom: 'J', prefix: 'k')}
|
6
6
|
describe "#atom" do
|
7
7
|
it "should be an atom" do
|
8
8
|
subject.atom.must_be_instance_of Unitwise::Atom
|
metadata
CHANGED
@@ -1,125 +1,125 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unitwise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
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-
|
11
|
+
date: 2014-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: liner
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 0.2.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.2.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: signed_multiset
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 0.2.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.2.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: parslet
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 1.5.0
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 1.5.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: nori
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: nokogiri
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: coveralls
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
description: Ruby implementation of the Unified Code for Units of Measure (UCUM)
|
@@ -129,9 +129,9 @@ executables: []
|
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
|
-
- .gitignore
|
133
|
-
- .ruby-version
|
134
|
-
- .travis.yml
|
132
|
+
- ".gitignore"
|
133
|
+
- ".ruby-version"
|
134
|
+
- ".travis.yml"
|
135
135
|
- Gemfile
|
136
136
|
- LICENSE.txt
|
137
137
|
- README.md
|
@@ -168,6 +168,7 @@ files:
|
|
168
168
|
- lib/unitwise/unit.rb
|
169
169
|
- lib/unitwise/version.rb
|
170
170
|
- simple_bench.rb
|
171
|
+
- test/support/scale_tests.rb
|
171
172
|
- test/test_helper.rb
|
172
173
|
- test/unitwise/atom_test.rb
|
173
174
|
- test/unitwise/base_test.rb
|
@@ -178,6 +179,7 @@ files:
|
|
178
179
|
- test/unitwise/functional_test.rb
|
179
180
|
- test/unitwise/measurement_test.rb
|
180
181
|
- test/unitwise/prefix_test.rb
|
182
|
+
- test/unitwise/scale_test.rb
|
181
183
|
- test/unitwise/term_test.rb
|
182
184
|
- test/unitwise/unit_test.rb
|
183
185
|
- test/unitwise_test.rb
|
@@ -192,22 +194,23 @@ require_paths:
|
|
192
194
|
- lib
|
193
195
|
required_ruby_version: !ruby/object:Gem::Requirement
|
194
196
|
requirements:
|
195
|
-
- -
|
197
|
+
- - ">="
|
196
198
|
- !ruby/object:Gem::Version
|
197
199
|
version: '0'
|
198
200
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
199
201
|
requirements:
|
200
|
-
- -
|
202
|
+
- - ">="
|
201
203
|
- !ruby/object:Gem::Version
|
202
204
|
version: '0'
|
203
205
|
requirements: []
|
204
206
|
rubyforge_project:
|
205
|
-
rubygems_version: 2.0
|
207
|
+
rubygems_version: 2.2.0
|
206
208
|
signing_key:
|
207
209
|
specification_version: 4
|
208
210
|
summary: Unitwise is a library for performing mathematical operations and conversions
|
209
211
|
on all units defined by the Unified Code for Units of Measure(UCUM).
|
210
212
|
test_files:
|
213
|
+
- test/support/scale_tests.rb
|
211
214
|
- test/test_helper.rb
|
212
215
|
- test/unitwise/atom_test.rb
|
213
216
|
- test/unitwise/base_test.rb
|
@@ -218,7 +221,7 @@ test_files:
|
|
218
221
|
- test/unitwise/functional_test.rb
|
219
222
|
- test/unitwise/measurement_test.rb
|
220
223
|
- test/unitwise/prefix_test.rb
|
224
|
+
- test/unitwise/scale_test.rb
|
221
225
|
- test/unitwise/term_test.rb
|
222
226
|
- test/unitwise/unit_test.rb
|
223
227
|
- test/unitwise_test.rb
|
224
|
-
has_rdoc:
|