ruby-units 1.3.2 → 1.4.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.
- data/CHANGELOG.txt +25 -7
- data/LICENSE.txt +1 -1
- data/README.md +68 -55
- data/RakeFile +27 -18
- data/TODO +2 -1
- data/VERSION +1 -1
- data/lib/ruby-units.rb +2 -2
- data/lib/ruby_units.rb +2 -2
- data/lib/ruby_units/array.rb +4 -2
- data/lib/ruby_units/date.rb +17 -4
- data/lib/ruby_units/definition.rb +100 -0
- data/lib/ruby_units/fixnum.rb +6 -4
- data/lib/ruby_units/math.rb +32 -2
- data/lib/ruby_units/numeric.rb +2 -1
- data/lib/ruby_units/object.rb +8 -1
- data/lib/ruby_units/string.rb +10 -109
- data/lib/ruby_units/string/extra.rb +45 -11
- data/lib/ruby_units/time.rb +11 -2
- data/lib/ruby_units/unit.rb +722 -434
- data/lib/ruby_units/unit_definitions.rb +3 -252
- data/lib/ruby_units/unit_definitions/base.rb +103 -0
- data/lib/ruby_units/unit_definitions/prefix.rb +40 -0
- data/lib/ruby_units/unit_definitions/standard.rb +705 -0
- data/lib/ruby_units/version.rb +1 -0
- data/ruby-units.gemspec +15 -20
- metadata +46 -35
- data/Gemfile +0 -12
- data/Manifest.txt +0 -19
- data/autotest/discover.rb +0 -1
- data/spec/ruby-units/array_spec.rb +0 -14
- data/spec/ruby-units/complex_spec.rb +0 -37
- data/spec/ruby-units/date_spec.rb +0 -38
- data/spec/ruby-units/math_spec.rb +0 -63
- data/spec/ruby-units/numeric_spec.rb +0 -12
- data/spec/ruby-units/object_spec.rb +0 -7
- data/spec/ruby-units/string/extra_spec.rb +0 -45
- data/spec/ruby-units/string_spec.rb +0 -20
- data/spec/ruby-units/time_spec.rb +0 -28
- data/spec/ruby-units/unit_spec.rb +0 -965
- data/spec/spec_helper.rb +0 -5
- data/test/test_cache.rb +0 -26
- data/test/test_ruby-units.rb +0 -976
data/lib/ruby_units/fixnum.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
# this patch is necessary for ruby 1.8 because cases where
|
2
|
-
# Integers are divided by Units don't work quite right
|
3
|
-
|
4
1
|
if RUBY_VERSION < "1.9"
|
2
|
+
# :nocov_19:
|
5
3
|
class Fixnum
|
6
4
|
alias quo_without_units quo
|
7
|
-
|
5
|
+
|
6
|
+
# @note this patch is necessary for ruby 1.8 because cases where Integers are divided by Units don't work quite right
|
7
|
+
# @param [Numeric]
|
8
|
+
# @return [Unit, Integer]
|
8
9
|
def quo_with_units(other)
|
9
10
|
case other
|
10
11
|
when Unit
|
@@ -17,4 +18,5 @@ if RUBY_VERSION < "1.9"
|
|
17
18
|
alias quo quo_with_units
|
18
19
|
alias / quo_with_units
|
19
20
|
end
|
21
|
+
# :nocov_19:
|
20
22
|
end
|
data/lib/ruby_units/math.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
# Math will convert unit objects to radians and then attempt to use the value for
|
2
|
-
# trigonometric functions.
|
3
1
|
require 'mathn'
|
4
2
|
|
3
|
+
# Math will convert unit objects to radians and then attempt to use the value for
|
4
|
+
# trigonometric functions.
|
5
5
|
module Math
|
6
6
|
|
7
7
|
alias :unit_sqrt :sqrt
|
8
|
+
# @return [Numeric]
|
8
9
|
def sqrt(n)
|
9
10
|
if Unit === n
|
10
11
|
(n**(Rational(1,2))).to_unit
|
@@ -12,12 +13,15 @@ module Math
|
|
12
13
|
unit_sqrt(n)
|
13
14
|
end
|
14
15
|
end
|
16
|
+
# @return [Numeric]
|
15
17
|
module_function :unit_sqrt
|
18
|
+
# @return [Numeric]
|
16
19
|
module_function :sqrt
|
17
20
|
|
18
21
|
#:nocov:
|
19
22
|
if self.respond_to?(:cbrt)
|
20
23
|
alias :unit_cbrt :cbrt
|
24
|
+
# @return [Numeric]
|
21
25
|
def cbrt(n)
|
22
26
|
if Unit === n
|
23
27
|
(n**(Rational(1,3))).to_unit
|
@@ -25,55 +29,76 @@ module Math
|
|
25
29
|
unit_cbrt(n)
|
26
30
|
end
|
27
31
|
end
|
32
|
+
# @return [Numeric]
|
28
33
|
module_function :unit_cbrt
|
34
|
+
# @return [Numeric]
|
29
35
|
module_function :cbrt
|
30
36
|
end
|
31
37
|
#:nocov:
|
32
38
|
|
33
39
|
alias :unit_sin :sin
|
40
|
+
# @return [Numeric]
|
34
41
|
def sin(n)
|
35
42
|
Unit === n ? unit_sin(n.convert_to('radian').scalar) : unit_sin(n)
|
36
43
|
end
|
44
|
+
# @return [Numeric]
|
37
45
|
module_function :unit_sin
|
46
|
+
# @return [Numeric]
|
38
47
|
module_function :sin
|
39
48
|
|
40
49
|
alias :unit_cos :cos
|
50
|
+
# @return [Numeric]
|
41
51
|
def cos(n)
|
42
52
|
Unit === n ? unit_cos(n.convert_to('radian').scalar) : unit_cos(n)
|
43
53
|
end
|
54
|
+
# @return [Numeric]
|
44
55
|
module_function :unit_cos
|
56
|
+
# @return [Numeric]
|
45
57
|
module_function :cos
|
46
58
|
|
47
59
|
alias :unit_sinh :sinh
|
60
|
+
# @return [Numeric]
|
48
61
|
def sinh(n)
|
49
62
|
Unit === n ? unit_sinh(n.convert_to('radian').scalar) : unit_sinh(n)
|
50
63
|
end
|
64
|
+
# @return [Numeric]
|
51
65
|
module_function :unit_sinh
|
66
|
+
# @return [Numeric]
|
52
67
|
module_function :sinh
|
53
68
|
|
54
69
|
alias :unit_cosh :cosh
|
70
|
+
# @return [Numeric]
|
55
71
|
def cosh(n)
|
56
72
|
Unit === n ? unit_cosh(n.convert_to('radian').scalar) : unit_cosh(n)
|
57
73
|
end
|
74
|
+
# @return [Numeric]
|
58
75
|
module_function :unit_cosh
|
76
|
+
# @return [Numeric]
|
59
77
|
module_function :cosh
|
60
78
|
|
61
79
|
alias :unit_tan :tan
|
80
|
+
# @return [Numeric]
|
62
81
|
def tan(n)
|
63
82
|
Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
|
64
83
|
end
|
84
|
+
# @return [Numeric]
|
65
85
|
module_function :tan
|
86
|
+
# @return [Numeric]
|
66
87
|
module_function :unit_tan
|
67
88
|
|
68
89
|
alias :unit_tanh :tanh
|
90
|
+
# @return [Numeric]
|
69
91
|
def tanh(n)
|
70
92
|
Unit === n ? unit_tanh(n.convert_to('radian').scalar) : unit_tanh(n)
|
71
93
|
end
|
94
|
+
# @return [Numeric]
|
72
95
|
module_function :unit_tanh
|
96
|
+
# @return [Numeric]
|
73
97
|
module_function :tanh
|
74
98
|
|
75
99
|
alias :unit_hypot :hypot
|
76
100
|
# Convert parameters to consistent units and perform the function
|
101
|
+
# @return [Numeric]
|
77
102
|
def hypot(x,y)
|
78
103
|
if Unit === x && Unit === y
|
79
104
|
(x**2 + y**2)**(1/2)
|
@@ -81,10 +106,13 @@ module Math
|
|
81
106
|
unit_hypot(x,y)
|
82
107
|
end
|
83
108
|
end
|
109
|
+
# @return [Numeric]
|
84
110
|
module_function :unit_hypot
|
111
|
+
# @return [Numeric]
|
85
112
|
module_function :hypot
|
86
113
|
|
87
114
|
alias :unit_atan2 :atan2
|
115
|
+
# @return [Numeric]
|
88
116
|
def atan2(x,y)
|
89
117
|
case
|
90
118
|
when (x.is_a?(Unit) && y.is_a?(Unit)) && (x !~ y)
|
@@ -95,7 +123,9 @@ module Math
|
|
95
123
|
Math::unit_atan2(x,y)
|
96
124
|
end
|
97
125
|
end
|
126
|
+
# @return [Numeric]
|
98
127
|
module_function :unit_atan2
|
128
|
+
# @return [Numeric]
|
99
129
|
module_function :atan2
|
100
130
|
|
101
131
|
end
|
data/lib/ruby_units/numeric.rb
CHANGED
data/lib/ruby_units/object.rb
CHANGED
data/lib/ruby_units/string.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
require 'time'
|
2
|
-
# make a string into a unit
|
3
2
|
class String
|
3
|
+
# make a string into a unit
|
4
|
+
# @return (see Unit#initialize)
|
4
5
|
def to_unit(other = nil)
|
5
6
|
other ? Unit.new(self).convert_to(other) : Unit.new(self)
|
6
7
|
end
|
7
8
|
alias :unit :to_unit
|
8
9
|
alias :u :to_unit
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
|
11
|
+
|
12
|
+
alias :unit_format :%
|
13
|
+
# format unit output using formating codes
|
14
|
+
# @example '%0.2f' % '1 mm'.unit => '1.00 mm'
|
15
|
+
# @return [String]
|
12
16
|
def %(*args)
|
13
17
|
return "" if self.empty?
|
14
18
|
case
|
@@ -23,112 +27,9 @@ class String
|
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# alias :old_from :from
|
29
|
-
# end
|
30
|
-
|
31
|
-
# # "5 min".from("now")
|
32
|
-
# def from(time_point = ::Time.now)
|
33
|
-
# return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer)
|
34
|
-
# self.unit.from(time_point)
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# alias :after :from
|
38
|
-
|
39
|
-
# def from_now
|
40
|
-
# self.from('now')
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# # "5 min".ago
|
44
|
-
# def ago
|
45
|
-
# self.unit.ago
|
46
|
-
# end
|
47
|
-
#
|
48
|
-
# def before(time_point = ::Time.now)
|
49
|
-
# self.unit.before(time_point)
|
50
|
-
# end
|
51
|
-
#
|
52
|
-
# def before_now
|
53
|
-
# self.before('now')
|
54
|
-
# end
|
55
|
-
#
|
56
|
-
# def since(time_point = ::Time.now)
|
57
|
-
# self.unit.since(time_point)
|
58
|
-
# end
|
59
|
-
#
|
60
|
-
# def until(time_point = ::Time.now)
|
61
|
-
# self.unit.until(time_point)
|
62
|
-
# end
|
63
|
-
#
|
30
|
+
# @param (see Unit#convert_to)
|
31
|
+
# @return (see Unit#convert_to)
|
64
32
|
def convert_to(other)
|
65
33
|
self.unit.convert_to(other)
|
66
34
|
end
|
67
|
-
#
|
68
|
-
# unless String.instance_methods.include?(:to)
|
69
|
-
# def to(other)
|
70
|
-
# warn "string.to is deprecated, use string.convert_to if you must, but the best would be Unit('unit').convert_to('target unit')"
|
71
|
-
# convert_to(other)
|
72
|
-
# end
|
73
|
-
# end
|
74
|
-
#
|
75
|
-
# def time(options = {})
|
76
|
-
# self.to_time(options) rescue self.to_datetime(options)
|
77
|
-
# end
|
78
|
-
#
|
79
|
-
# def to_time(options = {})
|
80
|
-
# begin
|
81
|
-
# #raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input)
|
82
|
-
# r = Chronic.parse(self,options)
|
83
|
-
# raise(ArgumentError, 'Invalid Time String') unless r
|
84
|
-
# return r
|
85
|
-
# rescue Exception => e
|
86
|
-
# puts e.inspect
|
87
|
-
# case
|
88
|
-
# when self == "now"
|
89
|
-
# Time.now
|
90
|
-
# when Time.respond_to?(:parse)
|
91
|
-
# Time.parse(self)
|
92
|
-
# else
|
93
|
-
# Time.local(*ParseDate.parsedate(self))
|
94
|
-
# end
|
95
|
-
# end
|
96
|
-
# end
|
97
|
-
#
|
98
|
-
# def to_datetime(options = {})
|
99
|
-
# begin
|
100
|
-
# # raises an exception if Chronic.parse = nil or if Chronic not defined
|
101
|
-
# r = Chronic.parse(self,options).send(:to_datetime)
|
102
|
-
# rescue NameError => e
|
103
|
-
# r = case
|
104
|
-
# when self.to_s == "now"
|
105
|
-
# DateTime.now
|
106
|
-
# else
|
107
|
-
# DateTime.parse(self)
|
108
|
-
# end
|
109
|
-
# end
|
110
|
-
# raise RuntimeError, "Invalid Time String (#{self.to_s})" if r == DateTime.new
|
111
|
-
# return r
|
112
|
-
# end
|
113
|
-
#
|
114
|
-
# def to_date(options={})
|
115
|
-
# begin
|
116
|
-
# r = Chronic.parse(self,options).to_date
|
117
|
-
# rescue
|
118
|
-
# r = case
|
119
|
-
# when self == "today"
|
120
|
-
# Date.today
|
121
|
-
# when RUBY_VERSION < "1.9"
|
122
|
-
# Date.civil(*ParseDate.parsedate(self)[0..5].compact)
|
123
|
-
# else
|
124
|
-
# Date.parse(self)
|
125
|
-
# end
|
126
|
-
# end
|
127
|
-
# raise RuntimeError, 'Invalid Date String' if r == Date.new
|
128
|
-
# return r
|
129
|
-
# end
|
130
|
-
#
|
131
|
-
# def datetime(options = {})
|
132
|
-
# self.to_datetime(options) rescue self.to_time(options)
|
133
|
-
# end
|
134
35
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
1
3
|
#
|
2
4
|
# String Extras
|
3
5
|
#
|
@@ -6,16 +8,13 @@
|
|
6
8
|
#
|
7
9
|
# in most cases it is better to do something like
|
8
10
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# "1 m".convert_to("ft")
|
11
|
+
# @example
|
12
|
+
# Unit("1 m").convert_to("ft")
|
13
|
+
# instead of
|
14
|
+
# "1 m".convert_to("ft")
|
14
15
|
#
|
15
16
|
# to use these methods:
|
16
17
|
# require 'ruby-units/string/extra'
|
17
|
-
|
18
|
-
require 'time'
|
19
18
|
class String
|
20
19
|
|
21
20
|
#needed for compatibility with Rails, which defines a String.from method
|
@@ -23,7 +22,11 @@ class String
|
|
23
22
|
alias :old_from :from
|
24
23
|
end
|
25
24
|
|
26
|
-
#
|
25
|
+
# @example
|
26
|
+
# "5 min".from("now")
|
27
|
+
# @param [Time] time_point
|
28
|
+
# @return (see Unit#from)
|
29
|
+
# @deprecated
|
27
30
|
def from(time_point = ::Time.now)
|
28
31
|
return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer)
|
29
32
|
warn Kernel.caller.first + " called ruby-units/string#from, which is deprecated. Use Unit(string).from(string) instead"
|
@@ -32,49 +35,74 @@ class String
|
|
32
35
|
|
33
36
|
alias :after :from
|
34
37
|
|
38
|
+
# @example
|
39
|
+
# "5 min".from_now
|
40
|
+
# @return (see Unit#from)
|
41
|
+
# @deprecated
|
35
42
|
def from_now
|
36
43
|
warn Kernel.caller.first + " called ruby-units/string#from_now, which is deprecated. Use Unit(string).from(Time.now) instead"
|
37
44
|
self.from(Time.now)
|
38
45
|
end
|
39
46
|
|
40
|
-
#
|
47
|
+
# @example
|
48
|
+
# "5 min".ago
|
49
|
+
# @return (see Unit#ago)
|
50
|
+
# @deprecated
|
41
51
|
def ago
|
42
52
|
warn Kernel.caller.first + " called ruby-units/string#ago, which is deprecated. Use Unit(string).ago instead"
|
43
53
|
self.unit.ago
|
44
54
|
end
|
45
55
|
|
56
|
+
# @example
|
57
|
+
# "5 min".before("now")
|
58
|
+
# @param [Time] time_point
|
59
|
+
# @return (see Unit#before)
|
60
|
+
# @deprecated
|
46
61
|
def before(time_point = ::Time.now)
|
47
62
|
warn Kernel.caller.first + " called ruby-units/string#before, which is deprecated. Use Unit(string).before(Time.now) instead"
|
48
63
|
self.unit.before(time_point)
|
49
64
|
end
|
50
|
-
|
65
|
+
|
66
|
+
# @example
|
67
|
+
# "5 min".before_now
|
68
|
+
# @return (see Unit#before)
|
69
|
+
# @deprecated
|
51
70
|
def before_now
|
52
71
|
warn Kernel.caller.first + " called ruby-units/string#before_now, which is deprecated. Use Unit(string).before(Time.now) instead"
|
53
72
|
self.before(Time.now)
|
54
73
|
end
|
55
74
|
|
75
|
+
# @return (see Unit#since)
|
76
|
+
# @deprecated
|
56
77
|
def since(time_point = ::Time.now)
|
57
78
|
warn Kernel.caller.first + " called ruby-units/string#since, which is deprecated. Use Unit(string).since(Time.now) instead"
|
58
79
|
self.unit.since(time_point)
|
59
80
|
end
|
60
81
|
|
82
|
+
# @return (see Unit#until)
|
83
|
+
# @deprecated
|
61
84
|
def until(time_point = ::Time.now)
|
62
85
|
warn Kernel.caller.first + " called ruby-units/string#until, which is deprecated. Use Unit(string).until(Time.now) instead"
|
63
86
|
self.unit.until(time_point)
|
64
87
|
end
|
65
88
|
|
66
|
-
|
67
89
|
unless String.instance_methods.include?(:to)
|
90
|
+
# @deprecated
|
91
|
+
# @return (see #convert_to)
|
68
92
|
def to(other)
|
69
93
|
warn "string.to is deprecated, use string.convert_to if you must, but the best would be Unit('unit').convert_to('target unit')"
|
70
94
|
convert_to(other)
|
71
95
|
end
|
72
96
|
end
|
73
97
|
|
98
|
+
# @deprecated
|
99
|
+
# @return (see #to_time)
|
74
100
|
def time(options = {})
|
75
101
|
self.to_time(options) rescue self.to_datetime(options)
|
76
102
|
end
|
77
103
|
|
104
|
+
# @deprecated
|
105
|
+
# @return [Time]
|
78
106
|
def to_time(options = {})
|
79
107
|
begin
|
80
108
|
#raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input)
|
@@ -93,6 +121,8 @@ class String
|
|
93
121
|
end
|
94
122
|
end
|
95
123
|
|
124
|
+
# @deprecated
|
125
|
+
# @return [DateTime]
|
96
126
|
def to_datetime(options = {})
|
97
127
|
begin
|
98
128
|
# raises an exception if Chronic.parse = nil or if Chronic not defined
|
@@ -109,6 +139,8 @@ class String
|
|
109
139
|
return r
|
110
140
|
end
|
111
141
|
|
142
|
+
# @deprecated
|
143
|
+
# @return [Date]
|
112
144
|
def to_date(options={})
|
113
145
|
begin
|
114
146
|
r = Chronic.parse(self,options).to_date
|
@@ -126,6 +158,8 @@ class String
|
|
126
158
|
return r
|
127
159
|
end
|
128
160
|
|
161
|
+
# @deprecated
|
162
|
+
# @return (see #to_datetime)
|
129
163
|
def datetime(options = {})
|
130
164
|
self.to_datetime(options) rescue self.to_time(options)
|
131
165
|
end
|