unitwise 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.md +4 -3
- data/data/derived_unit.yaml +536 -536
- data/data/prefix.yaml +1 -1
- data/lib/unitwise.rb +1 -1
- data/lib/unitwise/atom.rb +1 -1
- data/lib/unitwise/base.rb +5 -0
- data/lib/unitwise/{composable.rb → compatible.rb} +6 -7
- data/lib/unitwise/functional.rb +1 -1
- data/lib/unitwise/measurement.rb +9 -9
- data/lib/unitwise/scale.rb +6 -1
- data/lib/unitwise/term.rb +7 -3
- data/lib/unitwise/unit.rb +1 -2
- data/lib/unitwise/version.rb +1 -1
- metadata +3 -3
data/data/prefix.yaml
CHANGED
data/lib/unitwise.rb
CHANGED
data/lib/unitwise/atom.rb
CHANGED
data/lib/unitwise/base.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Unitwise
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
module
|
2
|
+
# Compatible is used to establish compatibility between units, terms, or
|
3
|
+
# measurements. This is done by determining the objects atomic composition
|
4
|
+
# represented as a Signed Multiset.
|
5
|
+
module Compatible
|
6
6
|
|
7
7
|
# @api private
|
8
8
|
def self.included(base)
|
@@ -37,7 +37,7 @@ module Unitwise
|
|
37
37
|
# Determine if this instance is similar to or compatible with other
|
38
38
|
# @return [true false]
|
39
39
|
# @api public
|
40
|
-
def
|
40
|
+
def compatible_with?(other)
|
41
41
|
self.composition == other.composition
|
42
42
|
end
|
43
43
|
|
@@ -45,10 +45,9 @@ module Unitwise
|
|
45
45
|
# @return [-1 0 1]
|
46
46
|
# @api public
|
47
47
|
def <=>(other)
|
48
|
-
if other.respond_to?(:composition) &&
|
48
|
+
if other.respond_to?(:composition) && compatible_with?(other)
|
49
49
|
scalar <=> other.scalar
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
53
52
|
end
|
54
53
|
end
|
data/lib/unitwise/functional.rb
CHANGED
data/lib/unitwise/measurement.rb
CHANGED
@@ -28,10 +28,10 @@ module Unitwise
|
|
28
28
|
# @api public
|
29
29
|
def convert_to(other_unit)
|
30
30
|
other_unit = Unit.new(other_unit)
|
31
|
-
if
|
31
|
+
if compatible_with?(other_unit)
|
32
32
|
new(converted_value(other_unit), other_unit)
|
33
33
|
else
|
34
|
-
raise ConversionError, "Can't convert #{
|
34
|
+
raise ConversionError, "Can't convert #{self} to #{other_unit}."
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -42,7 +42,7 @@ module Unitwise
|
|
42
42
|
# measurement * some_other_measurement
|
43
43
|
# @api public
|
44
44
|
def *(other)
|
45
|
-
operate(:*, other) || raise(TypeError, "Can't multiply #{
|
45
|
+
operate(:*, other) || raise(TypeError, "Can't multiply #{self} by #{other}.")
|
46
46
|
end
|
47
47
|
|
48
48
|
# Divide this measurement by a number or another measurement
|
@@ -52,7 +52,7 @@ module Unitwise
|
|
52
52
|
# measurement / some_other_measurement
|
53
53
|
# @api public
|
54
54
|
def /(other)
|
55
|
-
operate(:/, other) || raise(TypeError, "Can't divide #{
|
55
|
+
operate(:/, other) || raise(TypeError, "Can't divide #{self} by #{other}")
|
56
56
|
end
|
57
57
|
|
58
58
|
# Add another measurement to this unit. Units must be compatible.
|
@@ -61,7 +61,7 @@ module Unitwise
|
|
61
61
|
# measurement + some_other_measurement
|
62
62
|
# @api public
|
63
63
|
def +(other)
|
64
|
-
combine(:+, other) || raise(TypeError, "Can't add #{other} to #{
|
64
|
+
combine(:+, other) || raise(TypeError, "Can't add #{other} to #{self}.")
|
65
65
|
end
|
66
66
|
|
67
67
|
# Subtract another measurement from this unit. Units must be compatible.
|
@@ -70,7 +70,7 @@ module Unitwise
|
|
70
70
|
# measurement - some_other_measurement
|
71
71
|
# @api public
|
72
72
|
def -(other)
|
73
|
-
combine(:-, other) || raise(TypeError, "Can't subtract #{other} from #{
|
73
|
+
combine(:-, other) || raise(TypeError, "Can't subtract #{other} from #{self}.")
|
74
74
|
end
|
75
75
|
|
76
76
|
# Raise a measurement to a numeric power.
|
@@ -82,7 +82,7 @@ module Unitwise
|
|
82
82
|
if number.is_a?(Numeric)
|
83
83
|
new( value ** number, unit ** number )
|
84
84
|
else
|
85
|
-
raise TypeError, "Can't raise #{
|
85
|
+
raise TypeError, "Can't raise #{self} to #{number} power."
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -171,7 +171,7 @@ module Unitwise
|
|
171
171
|
# Add or subtract other unit
|
172
172
|
# @api private
|
173
173
|
def combine(operator, other)
|
174
|
-
if
|
174
|
+
if other.respond_to?(:composition) && compatible_with?(other)
|
175
175
|
new(value.send(operator, other.convert_to(unit).value), unit)
|
176
176
|
end
|
177
177
|
end
|
@@ -182,7 +182,7 @@ module Unitwise
|
|
182
182
|
if other.is_a?(Numeric)
|
183
183
|
new(value.send(operator, other), unit)
|
184
184
|
elsif other.respond_to?(:composition)
|
185
|
-
if
|
185
|
+
if compatible_with?(other)
|
186
186
|
converted = other.convert_to(unit)
|
187
187
|
new(value.send(operator, converted.value), unit.send(operator, converted.unit))
|
188
188
|
else
|
data/lib/unitwise/scale.rb
CHANGED
data/lib/unitwise/term.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
require 'signed_multiset'
|
2
2
|
module Unitwise
|
3
3
|
class Term < Liner.new(:atom, :prefix, :factor, :exponent, :annotation)
|
4
|
+
include Unitwise::Compatible
|
4
5
|
|
5
|
-
|
6
|
+
def initialize(*args)
|
7
|
+
super(*args)
|
8
|
+
freeze
|
9
|
+
end
|
6
10
|
|
7
11
|
def atom=(value)
|
8
12
|
value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
|
@@ -25,11 +29,11 @@ module Unitwise
|
|
25
29
|
end
|
26
30
|
|
27
31
|
def factor
|
28
|
-
|
32
|
+
super || 1
|
29
33
|
end
|
30
34
|
|
31
35
|
def exponent
|
32
|
-
|
36
|
+
super || 1
|
33
37
|
end
|
34
38
|
|
35
39
|
def scalar
|
data/lib/unitwise/unit.rb
CHANGED
data/lib/unitwise/version.rb
CHANGED
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.5.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-
|
11
|
+
date: 2014-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: liner
|
@@ -142,7 +142,7 @@ files:
|
|
142
142
|
- lib/unitwise.rb
|
143
143
|
- lib/unitwise/atom.rb
|
144
144
|
- lib/unitwise/base.rb
|
145
|
-
- lib/unitwise/
|
145
|
+
- lib/unitwise/compatible.rb
|
146
146
|
- lib/unitwise/compound.rb
|
147
147
|
- lib/unitwise/errors.rb
|
148
148
|
- lib/unitwise/expression.rb
|