unite 1.2.0 → 1.3.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 +7 -0
- data/.bundle/config +2 -0
- data/.rspec +1 -1
- data/.ruby-version +1 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +15 -19
- data/README +33 -1
- data/bin/autospec +16 -0
- data/bin/byebug +16 -0
- data/bin/guard +16 -0
- data/bin/htmldiff +16 -0
- data/bin/ldiff +16 -0
- data/bin/rake2thor +16 -0
- data/bin/rspec +16 -0
- data/bin/thor +16 -0
- data/lib/unite/conversion.rb +23 -13
- data/lib/unite/dimension.rb +7 -3
- data/lib/unite/fraction.rb +106 -30
- data/lib/unite/lookup/definitions.rb +26 -10
- data/lib/unite/lookup/derived_unit.rb +4 -1
- data/lib/unite/lookup/simple_unit.rb +5 -1
- data/lib/unite/lookup.rb +12 -2
- data/lib/unite/quantity.rb +1 -1
- data/lib/unite/si_factor.rb +35 -0
- data/lib/unite/simplify.rb +33 -0
- data/lib/unite/unit.rb +34 -0
- data/lib/unite/version.rb +1 -1
- data/lib/unite.rb +3 -1
- data/spec/dimension_spec.rb +7 -2
- data/spec/lookup/derived_unit_spec.rb +0 -4
- data/spec/lookup/simple_unit_spec.rb +0 -2
- data/spec/lookup_spec.rb +18 -2
- data/spec/money_spec.rb +35 -0
- data/spec/quantity_spec.rb +1 -0
- data/spec/spec_helper.rb +1 -2
- data/spec/support/shared_examples/units/arithmetic.rb +21 -0
- data/spec/support/shared_examples/units/conversion.rb +106 -35
- data/spec/support/shared_examples/units/fractions.rb +25 -17
- data/spec/support/shared_examples/units/simplify.rb +26 -0
- data/spec/unit_spec.rb +44 -0
- metadata +49 -18
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aebeae667614cd23ebc795fa7930b6d55a95cce1
|
4
|
+
data.tar.gz: 62f3c66b9baec8a9ab0970322489ac43fa1dbd50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a5f0ee539de7142b8b26a510dd4f2aa6bb1d16fdc1a403f8c7434ca610562d319a2060284dedeefa67e835145e9e78f53c83f0ffa911cd8115e9a9e3e37190d
|
7
|
+
data.tar.gz: c88e6b42a7d06a16037a3a3c931df650a9852abffa00d39e4ffda5d4cc6971a59e4d6890de230c32a5f72b7b6344efdc304f2a2d860723fcc97fd6686d97613b
|
data/.bundle/config
ADDED
data/.rspec
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p247
|
data/Gemfile
CHANGED
@@ -6,13 +6,13 @@ gemspec
|
|
6
6
|
group :test do
|
7
7
|
gem 'SystemTimer', :platform => :mri_18
|
8
8
|
gem 'ruby-debug', :platform => :mri_18
|
9
|
-
gem '
|
10
|
-
gem '
|
11
|
-
gem 'ruby-debug19', :platform => :mri_19, :require => 'ruby-debug'
|
9
|
+
gem 'debugger', :platform => :mri_19
|
10
|
+
gem 'byebug', :platform => :mri_20
|
12
11
|
gem 'mocha'
|
13
12
|
gem 'rspec'
|
14
13
|
gem 'shoulda-matchers', :git => 'git://github.com/thoughtbot/shoulda-matchers.git', :require => 'false'
|
15
14
|
gem 'fabrication'
|
16
15
|
gem 'guard-rspec'
|
17
16
|
gem 'guard-bundler'
|
17
|
+
gem 'guard'
|
18
18
|
end
|
data/Gemfile.lock
CHANGED
@@ -8,7 +8,7 @@ GIT
|
|
8
8
|
PATH
|
9
9
|
remote: .
|
10
10
|
specs:
|
11
|
-
unite (1.
|
11
|
+
unite (1.3.0)
|
12
12
|
activemodel (>= 3.0.0)
|
13
13
|
activesupport (>= 3.0.0)
|
14
14
|
|
@@ -22,9 +22,17 @@ GEM
|
|
22
22
|
i18n (~> 0.6)
|
23
23
|
activesupport (3.1.3)
|
24
24
|
multi_json (~> 1.0)
|
25
|
-
|
26
|
-
|
25
|
+
builder (3.0.4)
|
26
|
+
byebug (2.3.1)
|
27
|
+
columnize (~> 0.3.6)
|
28
|
+
debugger-linecache (~> 1.2.0)
|
27
29
|
columnize (0.3.6)
|
30
|
+
debugger (1.6.2)
|
31
|
+
columnize (>= 0.3.1)
|
32
|
+
debugger-linecache (~> 1.2.0)
|
33
|
+
debugger-ruby_core_source (~> 1.2.3)
|
34
|
+
debugger-linecache (1.2.0)
|
35
|
+
debugger-ruby_core_source (1.2.3)
|
28
36
|
diff-lcs (1.1.3)
|
29
37
|
fabrication (1.2.0)
|
30
38
|
ffi (1.0.11)
|
@@ -36,11 +44,9 @@ GEM
|
|
36
44
|
guard (>= 0.2.2)
|
37
45
|
guard-rspec (0.6.0)
|
38
46
|
guard (>= 0.10.0)
|
39
|
-
i18n (0.6.
|
47
|
+
i18n (0.6.5)
|
40
48
|
linecache (0.46)
|
41
49
|
rbx-require-relative (> 0.0.4)
|
42
|
-
linecache19 (0.5.13)
|
43
|
-
ruby_core_source (>= 0.1.4)
|
44
50
|
metaclass (0.0.1)
|
45
51
|
mocha (0.10.1)
|
46
52
|
metaclass (~> 0.0.1)
|
@@ -59,16 +65,6 @@ GEM
|
|
59
65
|
ruby-debug-base (~> 0.10.4.0)
|
60
66
|
ruby-debug-base (0.10.4)
|
61
67
|
linecache (>= 0.3)
|
62
|
-
ruby-debug-base19 (0.11.26)
|
63
|
-
columnize (>= 0.3.1)
|
64
|
-
linecache19 (>= 0.5.11)
|
65
|
-
ruby_core_source (>= 0.1.4)
|
66
|
-
ruby-debug19 (0.11.6)
|
67
|
-
columnize (>= 0.3.1)
|
68
|
-
linecache19 (>= 0.5.11)
|
69
|
-
ruby-debug-base19 (>= 0.11.19)
|
70
|
-
ruby_core_source (0.1.5)
|
71
|
-
archive-tar-minitar (>= 0.5.2)
|
72
68
|
thor (0.14.6)
|
73
69
|
|
74
70
|
PLATFORMS
|
@@ -76,14 +72,14 @@ PLATFORMS
|
|
76
72
|
|
77
73
|
DEPENDENCIES
|
78
74
|
SystemTimer
|
75
|
+
byebug
|
76
|
+
debugger
|
79
77
|
fabrication
|
78
|
+
guard
|
80
79
|
guard-bundler
|
81
80
|
guard-rspec
|
82
|
-
linecache19 (>= 0.5.13)
|
83
81
|
mocha
|
84
82
|
rspec
|
85
83
|
ruby-debug
|
86
|
-
ruby-debug-base19 (>= 0.11.26)
|
87
|
-
ruby-debug19
|
88
84
|
shoulda-matchers!
|
89
85
|
unite!
|
data/README
CHANGED
@@ -1,3 +1,35 @@
|
|
1
1
|
gem install unite
|
2
2
|
|
3
|
-
gem 'unite'
|
3
|
+
gem 'unite'
|
4
|
+
|
5
|
+
|
6
|
+
Based on
|
7
|
+
|
8
|
+
http://www.cs.utexas.edu/users/novak/units95.html
|
9
|
+
|
10
|
+
|
11
|
+
Handles the following dimensions
|
12
|
+
|
13
|
+
[:length, :time, :temperature, :mass, :current, :substance, :luminosity, :money]
|
14
|
+
|
15
|
+
|
16
|
+
Note: Conversion of Money is not currently supported
|
17
|
+
|
18
|
+
|
19
|
+
Can use Unite::Quantity to represent values or include the functionality into your own objects.
|
20
|
+
|
21
|
+
Look at https://github.com/stellard/unite/blob/master/lib/unite/quantity.rb for a minum implementation
|
22
|
+
|
23
|
+
Examples: (needs more info here)
|
24
|
+
|
25
|
+
Unite::Quantity.init("1000*km")
|
26
|
+
Unite::Quantity.init("1000","km")
|
27
|
+
|
28
|
+
Unit definitions are found here
|
29
|
+
|
30
|
+
https://github.com/stellard/unite/blob/master/lib/unite/lookup/definitions.rb
|
31
|
+
|
32
|
+
Specs show good examples of use.
|
33
|
+
|
34
|
+
I will add more to readme
|
35
|
+
|
data/bin/autospec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'autospec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'autospec')
|
data/bin/byebug
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'byebug' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'byebug')
|
data/bin/guard
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'guard' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'guard')
|
data/bin/htmldiff
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'htmldiff' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'htmldiff')
|
data/bin/ldiff
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'ldiff' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'ldiff')
|
data/bin/rake2thor
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rake2thor' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'rake2thor')
|
data/bin/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'rspec')
|
data/bin/thor
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'thor' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('unite', 'thor')
|
data/lib/unite/conversion.rb
CHANGED
@@ -7,7 +7,7 @@ module Unite
|
|
7
7
|
|
8
8
|
extend ::ActiveSupport::Concern
|
9
9
|
include Comparison
|
10
|
-
include
|
10
|
+
include SiFactor
|
11
11
|
|
12
12
|
included do
|
13
13
|
|
@@ -18,27 +18,37 @@ module Unite
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def convert_to other
|
21
|
-
other = other
|
22
|
-
new_value =
|
21
|
+
other = cast_to_unit other
|
22
|
+
new_value, new_numerator, new_denominator = converted(other)
|
23
23
|
return nil if new_value.nil?
|
24
|
-
self.class.new :value => new_value, :numerator =>
|
24
|
+
self.class.new :value => new_value, :numerator => new_numerator, :denominator => new_denominator
|
25
|
+
end
|
26
|
+
|
27
|
+
def convert_to! other
|
28
|
+
other = cast_to_unit other
|
29
|
+
compatible! other
|
30
|
+
new_value, new_numerator, new_denominator = converted(other)
|
31
|
+
self.value = new_value
|
32
|
+
self.numerator = new_numerator
|
33
|
+
self.denominator = new_denominator
|
34
|
+
self
|
25
35
|
end
|
26
36
|
|
27
37
|
protected
|
28
38
|
|
29
|
-
def
|
30
|
-
|
31
|
-
self.si_factor / other.si_factor
|
39
|
+
def cast_to_unit other
|
40
|
+
other = Unit.init(other.is_a?(String) ? other : other.unit)
|
32
41
|
end
|
33
42
|
|
34
|
-
def
|
35
|
-
|
43
|
+
def converted other
|
44
|
+
new_value = self.converted_value(other)
|
45
|
+
return [nil, nil, nil] if new_value.nil?
|
46
|
+
return [new_value, other.numerator, other.denominator]
|
36
47
|
end
|
37
48
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
end.inject(BigDecimal.new(1)) {|product, factor| product*factor}
|
49
|
+
def converted_value other
|
50
|
+
return nil unless self.compatible?(other)
|
51
|
+
self.si_factor / other.si_factor
|
42
52
|
end
|
43
53
|
|
44
54
|
end
|
data/lib/unite/dimension.rb
CHANGED
@@ -23,12 +23,16 @@ module Unite
|
|
23
23
|
|
24
24
|
# also include the dimensionless dimension "none"
|
25
25
|
LIST = VECTOR_LIST + [:none]
|
26
|
-
UNITS = ['
|
26
|
+
UNITS = ['km', 's', 'K', 'kg', 'A', 'mol', 'cd', 'GBP', '']
|
27
27
|
INDICIES = Hash[*LIST.each_with_index.map{|d,i| [d,i]}.flatten]
|
28
28
|
|
29
29
|
|
30
|
-
def si_unit
|
31
|
-
|
30
|
+
def si_unit dimension_name_or_index
|
31
|
+
if dimension_name_or_index.kind_of? Fixnum
|
32
|
+
UNITS[dimension_name_or_index]
|
33
|
+
else
|
34
|
+
UNITS[INDICIES[dimension_name_or_index.to_sym]]
|
35
|
+
end
|
32
36
|
end
|
33
37
|
|
34
38
|
def blank_dimension_vector
|
data/lib/unite/fraction.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
module Unite
|
3
|
-
class InvalidFormat < RuntimeError
|
3
|
+
class InvalidFormat < RuntimeError
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
6
|
module Fraction
|
7
7
|
extend ::ActiveSupport::Concern
|
8
8
|
|
@@ -20,30 +20,18 @@ module Unite
|
|
20
20
|
self.denominator ||= []
|
21
21
|
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
raise(InvalidFormat, string) if num_denom.length > 2 || num_denom.empty?
|
28
|
-
self.numerator = num_denom[0].split('*').map(&:strip)
|
29
|
-
self.denominator = (num_denom[1].nil? ? [] : num_denom[1].split('*')).map(&:strip)
|
30
|
-
self.value = extract_value!(:numerator) / extract_value!(:denominator)
|
31
|
-
reduce
|
32
|
-
else
|
33
|
-
self.value = BigDecimal.new(0)
|
34
|
-
self.numerator = []
|
35
|
-
self.denominator = []
|
36
|
-
end
|
23
|
+
def expression= string
|
24
|
+
self.value, self.numerator, self.denominator = parse_expression string
|
25
|
+
reduce
|
26
|
+
expression
|
37
27
|
end
|
38
28
|
|
39
|
-
def expression
|
40
|
-
|
41
|
-
denominator.empty? ? numertor_string : "#{numertor_string}/#{denominator.join('*')}"
|
29
|
+
def expression precision = 2
|
30
|
+
denominator_expression.blank? ? numerator_expression(precision) : "#{numerator_expression(precision)}/#{denominator_expression}"
|
42
31
|
end
|
43
32
|
|
44
33
|
def unit
|
45
|
-
|
46
|
-
denominator.blank? ? numertor_string : "#{numertor_string}/#{denominator.join('*')}"
|
34
|
+
unit_array_to_string self.numerator, self.denominator
|
47
35
|
end
|
48
36
|
|
49
37
|
def expanded_numerator
|
@@ -60,20 +48,27 @@ module Unite
|
|
60
48
|
end
|
61
49
|
|
62
50
|
def reduce
|
63
|
-
|
51
|
+
new_unit = normalized_unit
|
52
|
+
convert_to!(normalized_unit) unless new_unit == unit || !self.respond_to?(:convert_to!)
|
53
|
+
end
|
64
54
|
|
65
|
-
|
66
|
-
if nindex = self.numerator.index(unit)
|
67
|
-
self.numerator.delete_at(nindex)
|
68
|
-
end
|
69
|
-
end
|
55
|
+
private
|
70
56
|
|
57
|
+
def with_expanded_units
|
58
|
+
expand
|
59
|
+
result = yield
|
71
60
|
self.numerator = reduce_unit_array(self.numerator)
|
72
61
|
self.denominator = reduce_unit_array(self.denominator)
|
62
|
+
result
|
63
|
+
end
|
73
64
|
|
65
|
+
def numerator_expression precision
|
66
|
+
([value.round(precision)] + (numerator || [])).join('*')
|
74
67
|
end
|
75
68
|
|
76
|
-
|
69
|
+
def denominator_expression
|
70
|
+
denominator.join('*')
|
71
|
+
end
|
77
72
|
|
78
73
|
def expand
|
79
74
|
self.numerator = expanded_numerator
|
@@ -84,9 +79,13 @@ module Unite
|
|
84
79
|
[].tap{|rejected| array.delete_if { |v| yield(v) && rejected << v }}
|
85
80
|
end
|
86
81
|
|
87
|
-
def
|
82
|
+
def split_string string
|
83
|
+
(string.nil? ? [] : string.split('*')).map(&:strip)
|
84
|
+
end
|
85
|
+
|
86
|
+
def extract_value! array
|
88
87
|
number_regex = /\A[-+]?\d*\.?\d+([eE][-+]?\d+)?\Z/
|
89
|
-
seperate!(
|
88
|
+
seperate!(array){|x| number_regex =~ x }.map{|n| BigDecimal.new(n)}.
|
90
89
|
inject(BigDecimal.new(1)){|product, number| product*number }
|
91
90
|
end
|
92
91
|
|
@@ -103,6 +102,25 @@ module Unite
|
|
103
102
|
end
|
104
103
|
end
|
105
104
|
|
105
|
+
def parse_expression string
|
106
|
+
unless string.blank? || string.strip == '/'
|
107
|
+
num_denom = string.split('/')
|
108
|
+
raise(InvalidFormat, string) if num_denom.length > 2 || num_denom.empty?
|
109
|
+
top = split_string(num_denom[0])
|
110
|
+
bottom = split_string(num_denom[1])
|
111
|
+
value = extract_value!(top) / extract_value!(bottom)
|
112
|
+
numerator = []
|
113
|
+
denominator = []
|
114
|
+
sift_units top, numerator, denominator
|
115
|
+
sift_units bottom, denominator, numerator
|
116
|
+
else
|
117
|
+
value = BigDecimal.new(0)
|
118
|
+
numerator = []
|
119
|
+
denominator = []
|
120
|
+
end
|
121
|
+
return [value, numerator, denominator]
|
122
|
+
end
|
123
|
+
|
106
124
|
def expand_unit_array array
|
107
125
|
array.collect do |unit|
|
108
126
|
if /\A([^\^\s]+)(\^(\d+))?\Z/ =~ unit
|
@@ -114,5 +132,63 @@ module Unite
|
|
114
132
|
end
|
115
133
|
end.flatten.compact
|
116
134
|
end
|
135
|
+
|
136
|
+
def sift_units array, top, bottom
|
137
|
+
#move negative exponents to the bottom and keep positive on the top
|
138
|
+
array.each do |u|
|
139
|
+
u.match(/\A^([^\^\s]+)(\^(-?)(\d+))?\z/)
|
140
|
+
unit = Regexp.last_match(1)
|
141
|
+
power = Regexp.last_match(4)
|
142
|
+
expression = "#{unit}#{power.blank? ? "": "^#{power}"}"
|
143
|
+
if Regexp.last_match(3) == "-"
|
144
|
+
bottom << expression
|
145
|
+
else
|
146
|
+
top << expression
|
147
|
+
end
|
148
|
+
end
|
149
|
+
return [top, bottom]
|
150
|
+
end
|
151
|
+
|
152
|
+
def normalized_unit
|
153
|
+
with_expanded_units do
|
154
|
+
numerator_lookup_array = lookup_units numerator
|
155
|
+
denominator_lookup_array = lookup_units denominator
|
156
|
+
|
157
|
+
units_in_use = {}
|
158
|
+
(numerator_lookup_array + denominator_lookup_array).each do |unit|
|
159
|
+
dimension_int = unit.dimension_int
|
160
|
+
units_in_use[dimension_int] ||= unit
|
161
|
+
end
|
162
|
+
|
163
|
+
new_numerator = normalize_units numerator_lookup_array, units_in_use
|
164
|
+
new_denominator = normalize_units denominator_lookup_array, units_in_use
|
165
|
+
|
166
|
+
|
167
|
+
new_denominator.delete_if do |unit|
|
168
|
+
if nindex = new_numerator.index(unit)
|
169
|
+
new_numerator.delete_at(nindex)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
new_denominator = reduce_unit_array(new_denominator)
|
174
|
+
new_numerator = reduce_unit_array(new_numerator)
|
175
|
+
|
176
|
+
unit_array_to_string new_numerator, new_denominator
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def normalize_units array, in_use_hash
|
181
|
+
array.map { |unit| in_use_hash[unit.dimension_int] || unit }.map(&:name)
|
182
|
+
end
|
183
|
+
|
184
|
+
def lookup_units array
|
185
|
+
array.map{ |unit| Lookup.find!(unit) }
|
186
|
+
end
|
187
|
+
|
188
|
+
def unit_array_to_string num, denom
|
189
|
+
numertor_string = (num.blank? && !denominator.blank?) ? '1.0' : (num || []).join('*')
|
190
|
+
denom.blank? ? numertor_string : "#{numertor_string}/#{denom.join('*')}"
|
191
|
+
end
|
192
|
+
|
117
193
|
end
|
118
194
|
end
|
@@ -12,15 +12,15 @@ module Unite
|
|
12
12
|
Lookup.add_property Property.new(:name => :none, :expression => '')
|
13
13
|
|
14
14
|
#TIME
|
15
|
-
Lookup.add SimpleUnit.new(:name => "s", :si_factor => 1, :dimension => :time)
|
16
|
-
Lookup.add SimpleUnit.new(:name => "
|
15
|
+
Lookup.add SimpleUnit.new(:name => "s", :si_factor => 1, :dimension => :time, :aliases => ['second', 'seconds'])
|
16
|
+
Lookup.add SimpleUnit.new(:name => "h", :si_factor => 3600, :dimension => :time, :aliases => ['hour', 'hours', 'hr'])
|
17
17
|
|
18
|
-
Lookup.add DerivedUnit.new(:name => "day", :expression => '24*hr')
|
18
|
+
Lookup.add DerivedUnit.new(:name => "day", :expression => '24*hr', :aliases => ['days'])
|
19
19
|
|
20
20
|
Lookup.add_property Property.new(:name => :time, :expression => 'hr')
|
21
21
|
|
22
22
|
#LENGTH
|
23
|
-
Lookup.add SimpleUnit.new(:name => "m", :si_factor => 1, :dimension => :length)
|
23
|
+
Lookup.add SimpleUnit.new(:name => "m", :si_factor => 1, :dimension => :length, :aliases => ['meter', 'meters'])
|
24
24
|
Lookup.add SimpleUnit.new(:name => "cm", :si_factor => 0.01, :dimension => :length)
|
25
25
|
Lookup.add SimpleUnit.new(:name => "dm", :si_factor => 0.1, :dimension => :length)
|
26
26
|
Lookup.add SimpleUnit.new(:name => "km", :si_factor => 1000, :dimension => :length)
|
@@ -36,19 +36,18 @@ module Unite
|
|
36
36
|
|
37
37
|
|
38
38
|
#MASS
|
39
|
-
Lookup.add SimpleUnit.new(:name => "g", :si_factor => 1, :dimension => :mass)
|
40
|
-
Lookup.add SimpleUnit.new(:name => "kg", :si_factor => 1000, :dimension => :mass)
|
41
|
-
Lookup.add SimpleUnit.new(:name => "
|
42
|
-
Lookup.add SimpleUnit.new(:name => "metric_tonne", :si_factor => 1000000, :dimension => :mass)
|
39
|
+
Lookup.add SimpleUnit.new(:name => "g", :si_factor => 1, :dimension => :mass, :aliases => ['gram'])
|
40
|
+
Lookup.add SimpleUnit.new(:name => "kg", :si_factor => 1000, :dimension => :mass, :aliases => ['kilogram'])
|
41
|
+
Lookup.add SimpleUnit.new(:name => "t", :si_factor => 1000000, :dimension => :mass, :aliases => ['tonne', 'metric_tonne'])
|
43
42
|
|
44
43
|
Lookup.add SimpleUnit.new(:name => "long_ton", :si_factor => 1.01605e6, :dimension => :mass)
|
45
44
|
Lookup.add SimpleUnit.new(:name => "short_ton", :si_factor => 907185, :dimension => :mass)
|
46
45
|
Lookup.add SimpleUnit.new(:name => "lb", :si_factor => 453.592, :dimension => :mass)
|
47
46
|
|
48
|
-
Lookup.add_property Property.new(:name => :mass, :expression => '
|
47
|
+
Lookup.add_property Property.new(:name => :mass, :expression => 'g')
|
49
48
|
|
50
49
|
|
51
|
-
#
|
50
|
+
#MONEY
|
52
51
|
Lookup.add SimpleUnit.new(:name => "GBP", :si_factor => 1, :dimension => :money)
|
53
52
|
|
54
53
|
#Right now only one currency is supported. It is too difficult to be able to convert them.
|
@@ -60,6 +59,23 @@ module Unite
|
|
60
59
|
Lookup.add_property Property.new(:name => :money, :expression => 'GBP')
|
61
60
|
|
62
61
|
|
62
|
+
#TEMPERATURE
|
63
|
+
Lookup.add SimpleUnit.new(:name => "K", :si_factor => 1, :dimension => :temperature, :aliases => ['kelvin'])
|
64
|
+
Lookup.add_property Property.new(:name => :temperature, :expression => 'K')
|
65
|
+
|
66
|
+
#LUMINOSITY
|
67
|
+
Lookup.add SimpleUnit.new(:name => "cd", :si_factor => 1, :dimension => :luminosity, :aliases => ['candela'])
|
68
|
+
Lookup.add_property Property.new(:name => :luminosity, :expression => 'cd')
|
69
|
+
|
70
|
+
#CURRENT
|
71
|
+
Lookup.add SimpleUnit.new(:name => "A", :si_factor => 1, :dimension => :current, :aliases => ['amp', 'ampere'])
|
72
|
+
Lookup.add_property Property.new(:name => :current, :expression => 'A')
|
73
|
+
|
74
|
+
#MOLE
|
75
|
+
Lookup.add SimpleUnit.new(:name => "mol", :si_factor => 1, :dimension => :substance, :aliases => ['mole'])
|
76
|
+
Lookup.add_property Property.new(:name => :substance, :expression => 'mol')
|
77
|
+
|
78
|
+
|
63
79
|
#DERIVED UNITS
|
64
80
|
|
65
81
|
#ENERGY
|
@@ -6,7 +6,7 @@ module Unite
|
|
6
6
|
include Dimension::Vector
|
7
7
|
include Conversion
|
8
8
|
|
9
|
-
attr_accessor :name, :numerator, :denominator, :value
|
9
|
+
attr_accessor :name, :numerator, :denominator, :value, :aliases
|
10
10
|
|
11
11
|
alias :to_s :name
|
12
12
|
validates_presence_of :name
|
@@ -19,6 +19,9 @@ module Unite
|
|
19
19
|
super
|
20
20
|
end
|
21
21
|
|
22
|
+
def aliases
|
23
|
+
@aliases ||= []
|
24
|
+
end
|
22
25
|
|
23
26
|
end
|
24
27
|
end
|
@@ -5,7 +5,7 @@ module Unite
|
|
5
5
|
include ActiveModel::Validations
|
6
6
|
include Dimension::Integer
|
7
7
|
|
8
|
-
attr_accessor :dimension, :name, :si_factor
|
8
|
+
attr_accessor :dimension, :name, :si_factor, :aliases
|
9
9
|
alias :to_s :name
|
10
10
|
|
11
11
|
validates_inclusion_of :dimension, :in => Dimension::LIST
|
@@ -22,5 +22,9 @@ module Unite
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
def aliases
|
26
|
+
@aliases ||= []
|
27
|
+
end
|
28
|
+
|
25
29
|
end
|
26
30
|
end
|