ruby-units 2.4.1 → 3.0.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/codeql-analysis.yml +3 -3
- data/.solargraph.yml +1 -1
- data/.tool-versions +3 -0
- data/CHANGELOG.txt +3 -1
- data/Gemfile.lock +11 -10
- data/lib/ruby_units/array.rb +17 -7
- data/lib/ruby_units/cache.rb +27 -14
- data/lib/ruby_units/configuration.rb +8 -8
- data/lib/ruby_units/date.rb +46 -53
- data/lib/ruby_units/math.rb +136 -113
- data/lib/ruby_units/numeric.rb +21 -5
- data/lib/ruby_units/string.rb +34 -21
- data/lib/ruby_units/time.rb +76 -69
- data/lib/ruby_units/unit.rb +193 -172
- data/lib/ruby_units/version.rb +1 -1
- metadata +3 -2
data/lib/ruby_units/string.rb
CHANGED
@@ -1,27 +1,40 @@
|
|
1
1
|
require 'time'
|
2
|
-
class String
|
3
|
-
# make a string into a unit
|
4
|
-
# @return (see RubyUnits::Unit#initialize)
|
5
|
-
def to_unit(other = nil)
|
6
|
-
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
7
|
-
end
|
8
2
|
|
9
|
-
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
3
|
+
module RubyUnits
|
4
|
+
# Extra methods for converting [String] objects to [RubyUnits::Unit] objects
|
5
|
+
# and using string formatting with Units.
|
6
|
+
module String
|
7
|
+
# Make a string into a unit
|
8
|
+
#
|
9
|
+
# @param other [RubyUnits::Unit, String] unit to convert to
|
10
|
+
# @return [RubyUnits::Unit]
|
11
|
+
def to_unit(other = nil)
|
12
|
+
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Format unit output using formatting codes
|
16
|
+
# @example '%0.2f' % '1 mm'.to_unit => '1.00 mm'
|
17
|
+
#
|
18
|
+
# @param other [RubyUnits::Unit, Object]
|
19
|
+
# @return [String]
|
20
|
+
def %(*other)
|
21
|
+
if other.first.is_a?(RubyUnits::Unit)
|
22
|
+
other.first.to_s(self)
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
18
26
|
end
|
19
|
-
end
|
20
|
-
alias % format_with_unit
|
21
27
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
28
|
+
# @param (see RubyUnits::Unit#convert_to)
|
29
|
+
# @return (see RubyUnits::Unit#convert_to)
|
30
|
+
def convert_to(other)
|
31
|
+
to_unit.convert_to(other)
|
32
|
+
end
|
26
33
|
end
|
27
34
|
end
|
35
|
+
|
36
|
+
# @note Do this instead of String.prepend(RubyUnits::String) to avoid YARD warnings
|
37
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
38
|
+
class String
|
39
|
+
prepend(RubyUnits::String)
|
40
|
+
end
|
data/lib/ruby_units/time.rb
CHANGED
@@ -1,82 +1,89 @@
|
|
1
1
|
require 'time'
|
2
|
-
#
|
3
|
-
# Time math is handled slightly differently. The difference is considered to be an exact duration if
|
4
|
-
# the subtracted value is in hours, minutes, or seconds. It is rounded to the nearest day if the offset
|
5
|
-
# is in years, decades, or centuries. This leads to less precise values, but ones that match the
|
6
|
-
# calendar better.
|
7
|
-
class Time
|
8
|
-
class << self
|
9
|
-
alias unit_time_at at
|
10
|
-
end
|
11
2
|
|
12
|
-
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
3
|
+
module RubyUnits
|
4
|
+
# Time math is handled slightly differently. The difference is considered to be an exact duration if
|
5
|
+
# the subtracted value is in hours, minutes, or seconds. It is rounded to the nearest day if the offset
|
6
|
+
# is in years, decades, or centuries. This leads to less precise values, but ones that match the
|
7
|
+
# calendar better.
|
8
|
+
module Time
|
9
|
+
# Class methods for [Time] objects
|
10
|
+
module ClassMethods
|
11
|
+
# Convert a duration to a [::Time] object by considering the duration to be
|
12
|
+
# the number of seconds since the epoch
|
13
|
+
#
|
14
|
+
# @param [Array<RubyUnits::Unit, Numeric, Symbol, Hash>] args
|
15
|
+
# @return [::Time]
|
16
|
+
def at(*args, **kwargs)
|
17
|
+
case args.first
|
18
|
+
when RubyUnits::Unit
|
19
|
+
options = args.last.is_a?(Hash) ? args.pop : kwargs
|
20
|
+
secondary_unit = args[2] || 'microsecond'
|
21
|
+
case args[1]
|
22
|
+
when Numeric
|
23
|
+
super((args.first + RubyUnits::Unit.new(args[1], secondary_unit.to_s)).convert_to('second').scalar, **options)
|
24
|
+
else
|
25
|
+
super(args.first.convert_to('second').scalar, **options)
|
26
|
+
end
|
27
|
+
else
|
28
|
+
super(*args, **kwargs)
|
29
|
+
end
|
26
30
|
end
|
27
|
-
else
|
28
|
-
ms.nil? ? unit_time_at(arg) : unit_time_at(arg, ms)
|
29
|
-
end
|
30
|
-
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
# :nocov_19:
|
39
|
-
public :to_date
|
40
|
-
# :nocov_19:
|
41
|
-
end
|
42
|
-
|
43
|
-
alias unit_add +
|
44
|
-
# @return [RubyUnits::Unit, Time]
|
45
|
-
def +(other)
|
46
|
-
case other
|
47
|
-
when RubyUnits::Unit
|
48
|
-
other = other.convert_to('d').round.convert_to('s') if %w[y decade century].include? other.units
|
49
|
-
begin
|
50
|
-
unit_add(other.convert_to('s').scalar)
|
51
|
-
rescue RangeError
|
52
|
-
to_datetime + other
|
32
|
+
# @example
|
33
|
+
# Time.in '5 min'
|
34
|
+
# @param duration [#to_unit]
|
35
|
+
# @return [::Time]
|
36
|
+
def in(duration)
|
37
|
+
::Time.now + duration.to_unit
|
53
38
|
end
|
54
|
-
else
|
55
|
-
unit_add(other)
|
56
39
|
end
|
57
|
-
end
|
58
40
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
41
|
+
# Convert a [::Time] object to a [RubyUnits::Unit] object. The time is
|
42
|
+
# considered to be a duration with the number of seconds since the epoch.
|
43
|
+
#
|
44
|
+
# @param other [String, RubyUnits::Unit]
|
45
|
+
# @return [RubyUnits::Unit]
|
46
|
+
def to_unit(other = nil)
|
47
|
+
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
48
|
+
end
|
65
49
|
|
66
|
-
|
50
|
+
# @param other [::Time, RubyUnits::Unit]
|
51
|
+
# @return [RubyUnits::Unit, ::Time]
|
52
|
+
def +(other)
|
53
|
+
case other
|
54
|
+
when RubyUnits::Unit
|
55
|
+
other = other.convert_to('d').round.convert_to('s') if %w[y decade century].include? other.units
|
56
|
+
begin
|
57
|
+
super(other.convert_to('s').scalar)
|
58
|
+
rescue RangeError
|
59
|
+
to_datetime + other
|
60
|
+
end
|
61
|
+
else
|
62
|
+
super
|
63
|
+
end
|
64
|
+
end
|
67
65
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
66
|
+
# @param other [::Time, RubyUnits::Unit]
|
67
|
+
# @return [RubyUnits::Unit, ::Time]
|
68
|
+
def -(other)
|
69
|
+
case other
|
70
|
+
when RubyUnits::Unit
|
71
|
+
other = other.convert_to('d').round.convert_to('s') if %w[y decade century].include? other.units
|
72
|
+
begin
|
73
|
+
super(other.convert_to('s').scalar)
|
74
|
+
rescue RangeError
|
75
|
+
public_send(:to_datetime) - other
|
76
|
+
end
|
77
|
+
else
|
78
|
+
super
|
77
79
|
end
|
78
|
-
else
|
79
|
-
unit_sub(other)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|
83
|
+
|
84
|
+
# @note Do this instead of Time.prepend(RubyUnits::Time) to avoid YARD warnings
|
85
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
86
|
+
class Time
|
87
|
+
prepend(RubyUnits::Time)
|
88
|
+
singleton_class.prepend(RubyUnits::Time::ClassMethods)
|
89
|
+
end
|