ruby-units 4.0.3 → 4.1.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 +4 -4
- data/.github/workflows/tests.yml +9 -5
- data/.rubocop.yml +12 -8
- data/.rubocop_todo.yml +193 -0
- data/.ruby-version +1 -1
- data/.tool-versions +1 -1
- data/Gemfile +19 -17
- data/Gemfile.lock +47 -49
- data/Guardfile +7 -5
- data/README.md +9 -4
- data/Rakefile +4 -2
- data/lib/ruby-units.rb +3 -1
- data/lib/ruby_units/array.rb +2 -0
- data/lib/ruby_units/cache.rb +2 -0
- data/lib/ruby_units/configuration.rb +31 -1
- data/lib/ruby_units/date.rb +7 -5
- data/lib/ruby_units/definition.rb +5 -4
- data/lib/ruby_units/math.rb +13 -11
- data/lib/ruby_units/namespaced.rb +14 -12
- data/lib/ruby_units/numeric.rb +2 -0
- data/lib/ruby_units/string.rb +3 -1
- data/lib/ruby_units/time.rb +11 -9
- data/lib/ruby_units/unit.rb +137 -114
- data/lib/ruby_units/unit_definitions/base.rb +17 -15
- data/lib/ruby_units/unit_definitions/prefix.rb +32 -30
- data/lib/ruby_units/unit_definitions/standard.rb +270 -268
- data/lib/ruby_units/unit_definitions.rb +5 -3
- data/lib/ruby_units/version.rb +3 -1
- metadata +4 -3
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RubyUnits
|
2
4
|
class << self
|
3
5
|
attr_writer :configuration
|
@@ -7,6 +9,7 @@ module RubyUnits
|
|
7
9
|
@configuration ||= Configuration.new
|
8
10
|
end
|
9
11
|
|
12
|
+
# Reset the configuration to the default values
|
10
13
|
def self.reset
|
11
14
|
@configuration = Configuration.new
|
12
15
|
end
|
@@ -27,16 +30,43 @@ module RubyUnits
|
|
27
30
|
# Used to separate the scalar from the unit when generating output. A value
|
28
31
|
# of `true` will insert a single space, and `false` will prevent adding a
|
29
32
|
# space to the string representation of a unit.
|
33
|
+
#
|
34
|
+
# @!attribute [rw] separator
|
35
|
+
# @return [Boolean] whether to include a space between the scalar and the unit
|
30
36
|
attr_reader :separator
|
31
37
|
|
38
|
+
# The style of format to use by default when generating output. When set to `:exponential`, all units will be
|
39
|
+
# represented in exponential notation instead of using a numerator and denominator.
|
40
|
+
#
|
41
|
+
# @!attribute [rw] format
|
42
|
+
# @return [Symbol] the format to use when generating output (:rational or :exponential) (default: :rational)
|
43
|
+
attr_reader :format
|
44
|
+
|
32
45
|
def initialize
|
46
|
+
self.format = :rational
|
33
47
|
self.separator = true
|
34
48
|
end
|
35
49
|
|
50
|
+
# Use a space for the separator to use when generating output.
|
51
|
+
#
|
52
|
+
# @param value [Boolean] whether to include a space between the scalar and the unit
|
53
|
+
# @return [void]
|
36
54
|
def separator=(value)
|
37
55
|
raise ArgumentError, "configuration 'separator' may only be true or false" unless [true, false].include?(value)
|
38
56
|
|
39
|
-
@separator = value ?
|
57
|
+
@separator = value ? " " : nil
|
58
|
+
end
|
59
|
+
|
60
|
+
# Set the format to use when generating output.
|
61
|
+
# The `:rational` style will generate units string like `3 m/s^2` and the `:exponential` style will generate units
|
62
|
+
# like `3 m*s^-2`.
|
63
|
+
#
|
64
|
+
# @param value [Symbol] the format to use when generating output (:rational or :exponential)
|
65
|
+
# @return [void]
|
66
|
+
def format=(value)
|
67
|
+
raise ArgumentError, "configuration 'format' may only be :rational or :exponential" unless %i[rational exponential].include?(value)
|
68
|
+
|
69
|
+
@format = value
|
40
70
|
end
|
41
71
|
end
|
42
72
|
end
|
data/lib/ruby_units/date.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "date"
|
2
4
|
|
3
5
|
module RubyUnits
|
4
6
|
# Extra methods for [::Date] to allow it to be used as a [RubyUnits::Unit]
|
@@ -11,8 +13,8 @@ module RubyUnits
|
|
11
13
|
def +(other)
|
12
14
|
case other
|
13
15
|
when RubyUnits::Unit
|
14
|
-
other = other.convert_to(
|
15
|
-
super(other.convert_to(
|
16
|
+
other = other.convert_to("d").round if %w[y decade century].include? other.units
|
17
|
+
super(other.convert_to("day").scalar)
|
16
18
|
else
|
17
19
|
super
|
18
20
|
end
|
@@ -26,8 +28,8 @@ module RubyUnits
|
|
26
28
|
def -(other)
|
27
29
|
case other
|
28
30
|
when RubyUnits::Unit
|
29
|
-
other = other.convert_to(
|
30
|
-
super(other.convert_to(
|
31
|
+
other = other.convert_to("d").round if %w[y decade century].include? other.units
|
32
|
+
super(other.convert_to("day").scalar)
|
31
33
|
else
|
32
34
|
super
|
33
35
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class RubyUnits::Unit < Numeric
|
2
4
|
# Handle the definition of units
|
3
5
|
class Definition
|
@@ -34,8 +36,8 @@ class RubyUnits::Unit < Numeric
|
|
34
36
|
#
|
35
37
|
def initialize(name, definition = [])
|
36
38
|
yield self if block_given?
|
37
|
-
self.name ||= name.gsub(/[<>]/,
|
38
|
-
@aliases ||=
|
39
|
+
self.name ||= name.gsub(/[<>]/, "")
|
40
|
+
@aliases ||= definition[0] || [name]
|
39
41
|
@scalar ||= definition[1]
|
40
42
|
@kind ||= definition[2]
|
41
43
|
@numerator ||= definition[3] || RubyUnits::Unit::UNITY_ARRAY
|
@@ -55,7 +57,7 @@ class RubyUnits::Unit < Numeric
|
|
55
57
|
# @param name_value [String]
|
56
58
|
# @return [String]
|
57
59
|
def name=(name_value)
|
58
|
-
@name = name_value.gsub(/[<>]/,
|
60
|
+
@name = name_value.gsub(/[<>]/, "")
|
59
61
|
end
|
60
62
|
|
61
63
|
# alias array must contain the name of the unit and entries must be unique
|
@@ -73,7 +75,6 @@ class RubyUnits::Unit < Numeric
|
|
73
75
|
@kind = base.kind
|
74
76
|
@numerator = base.numerator
|
75
77
|
@denominator = base.denominator
|
76
|
-
self
|
77
78
|
end
|
78
79
|
|
79
80
|
# is this definition for a prefix?
|
data/lib/ruby_units/math.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RubyUnits
|
2
4
|
# Math will convert unit objects to radians and then attempt to use the value for
|
3
5
|
# trigonometric functions.
|
@@ -29,14 +31,14 @@ module RubyUnits
|
|
29
31
|
# @param angle [Numeric, RubyUnits::Unit]
|
30
32
|
# @return [Numeric]
|
31
33
|
def sin(angle)
|
32
|
-
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to(
|
34
|
+
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super
|
33
35
|
end
|
34
36
|
|
35
37
|
# @param number [Numeric, RubyUnits::Unit]
|
36
38
|
# @return [Numeric, RubyUnits::Unit]
|
37
39
|
def asin(number)
|
38
40
|
if number.is_a?(RubyUnits::Unit)
|
39
|
-
[super
|
41
|
+
[super, "radian"].to_unit
|
40
42
|
else
|
41
43
|
super
|
42
44
|
end
|
@@ -45,14 +47,14 @@ module RubyUnits
|
|
45
47
|
# @param angle [Numeric, RubyUnits::Unit]
|
46
48
|
# @return [Numeric]
|
47
49
|
def cos(angle)
|
48
|
-
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to(
|
50
|
+
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super
|
49
51
|
end
|
50
52
|
|
51
53
|
# @param number [Numeric, RubyUnits::Unit]
|
52
54
|
# @return [Numeric, RubyUnits::Unit]
|
53
55
|
def acos(number)
|
54
56
|
if number.is_a?(RubyUnits::Unit)
|
55
|
-
[super
|
57
|
+
[super, "radian"].to_unit
|
56
58
|
else
|
57
59
|
super
|
58
60
|
end
|
@@ -61,25 +63,25 @@ module RubyUnits
|
|
61
63
|
# @param number [Numeric, RubyUnits::Unit]
|
62
64
|
# @return [Numeric]
|
63
65
|
def sinh(number)
|
64
|
-
number.is_a?(RubyUnits::Unit) ? super(number.convert_to(
|
66
|
+
number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super
|
65
67
|
end
|
66
68
|
|
67
69
|
# @param number [Numeric, RubyUnits::Unit]
|
68
70
|
# @return [Numeric]
|
69
71
|
def cosh(number)
|
70
|
-
number.is_a?(RubyUnits::Unit) ? super(number.convert_to(
|
72
|
+
number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super
|
71
73
|
end
|
72
74
|
|
73
75
|
# @param angle [Numeric, RubyUnits::Unit]
|
74
76
|
# @return [Numeric]
|
75
77
|
def tan(angle)
|
76
|
-
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to(
|
78
|
+
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super
|
77
79
|
end
|
78
80
|
|
79
81
|
# @param number [Numeric, RubyUnits::Unit]
|
80
82
|
# @return [Numeric]
|
81
83
|
def tanh(number)
|
82
|
-
number.is_a?(RubyUnits::Unit) ? super(number.convert_to(
|
84
|
+
number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super
|
83
85
|
end
|
84
86
|
|
85
87
|
# @param x [Numeric, RubyUnits::Unit]
|
@@ -98,7 +100,7 @@ module RubyUnits
|
|
98
100
|
# @return [RubyUnits::Unit] if argument is a unit
|
99
101
|
def atan(number)
|
100
102
|
if number.is_a?(RubyUnits::Unit)
|
101
|
-
[super
|
103
|
+
[super, "radian"].to_unit
|
102
104
|
else
|
103
105
|
super
|
104
106
|
end
|
@@ -110,10 +112,10 @@ module RubyUnits
|
|
110
112
|
# @return [RubyUnits::Unit] if parameters are units
|
111
113
|
# @raise [ArgumentError] if parameters are not numbers or compatible units
|
112
114
|
def atan2(x, y)
|
113
|
-
raise ArgumentError,
|
115
|
+
raise ArgumentError, "Incompatible RubyUnits::Units" if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && !x.compatible?(y)
|
114
116
|
|
115
117
|
if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && x.compatible?(y)
|
116
|
-
[super(x.base_scalar, y.base_scalar),
|
118
|
+
[super(x.base_scalar, y.base_scalar), "radian"].to_unit
|
117
119
|
else
|
118
120
|
super
|
119
121
|
end
|
@@ -1,13 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# require_relative this file to avoid creating an class alias from Unit to RubyUnits::Unit
|
2
|
-
require_relative
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
13
|
-
require_relative
|
4
|
+
require_relative "version"
|
5
|
+
require_relative "configuration"
|
6
|
+
require_relative "definition"
|
7
|
+
require_relative "cache"
|
8
|
+
require_relative "array"
|
9
|
+
require_relative "date"
|
10
|
+
require_relative "time"
|
11
|
+
require_relative "math"
|
12
|
+
require_relative "numeric"
|
13
|
+
require_relative "string"
|
14
|
+
require_relative "unit"
|
15
|
+
require_relative "unit_definitions"
|
data/lib/ruby_units/numeric.rb
CHANGED
data/lib/ruby_units/string.rb
CHANGED
data/lib/ruby_units/time.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "time"
|
2
4
|
|
3
5
|
module RubyUnits
|
4
6
|
# Time math is handled slightly differently. The difference is considered to be an exact duration if
|
@@ -17,15 +19,15 @@ module RubyUnits
|
|
17
19
|
case args.first
|
18
20
|
when RubyUnits::Unit
|
19
21
|
options = args.last.is_a?(Hash) ? args.pop : kwargs
|
20
|
-
secondary_unit = args[2] ||
|
22
|
+
secondary_unit = args[2] || "microsecond"
|
21
23
|
case args[1]
|
22
24
|
when Numeric
|
23
|
-
super((args.first + RubyUnits::Unit.new(args[1], secondary_unit.to_s)).convert_to(
|
25
|
+
super((args.first + RubyUnits::Unit.new(args[1], secondary_unit.to_s)).convert_to("second").scalar, **options)
|
24
26
|
else
|
25
|
-
super(args.first.convert_to(
|
27
|
+
super(args.first.convert_to("second").scalar, **options)
|
26
28
|
end
|
27
29
|
else
|
28
|
-
super
|
30
|
+
super
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
@@ -52,9 +54,9 @@ module RubyUnits
|
|
52
54
|
def +(other)
|
53
55
|
case other
|
54
56
|
when RubyUnits::Unit
|
55
|
-
other = other.convert_to(
|
57
|
+
other = other.convert_to("d").round.convert_to("s") if %w[y decade century].include? other.units
|
56
58
|
begin
|
57
|
-
super(other.convert_to(
|
59
|
+
super(other.convert_to("s").scalar)
|
58
60
|
rescue RangeError
|
59
61
|
to_datetime + other
|
60
62
|
end
|
@@ -68,9 +70,9 @@ module RubyUnits
|
|
68
70
|
def -(other)
|
69
71
|
case other
|
70
72
|
when RubyUnits::Unit
|
71
|
-
other = other.convert_to(
|
73
|
+
other = other.convert_to("d").round.convert_to("s") if %w[y decade century].include? other.units
|
72
74
|
begin
|
73
|
-
super(other.convert_to(
|
75
|
+
super(other.convert_to("s").scalar)
|
74
76
|
rescue RangeError
|
75
77
|
public_send(:to_datetime) - other
|
76
78
|
end
|