ruby-units 2.4.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- alias original_format %
10
- # format unit output using formating codes
11
- # @example '%0.2f' % '1 mm'.to_unit => '1.00 mm'
12
- # @return [String]
13
- def format_with_unit(*other)
14
- if other.first.is_a?(RubyUnits::Unit)
15
- other.first.to_s(self)
16
- else
17
- original_format(*other)
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
- # @param (see RubyUnits::Unit#convert_to)
23
- # @return (see RubyUnits::Unit#convert_to)
24
- def convert_to(other)
25
- to_unit.convert_to(other)
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
@@ -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
- # Convert a duration to a Time value by considering the duration to be the number of seconds since the
13
- # epoch
14
- # @param [Time] arg
15
- # @param [Integer] ms
16
- # @return [RubyUnits::Unit, Time]
17
- def self.at(arg, ms = nil)
18
- case arg
19
- when Time
20
- unit_time_at(arg)
21
- when RubyUnits::Unit
22
- if ms
23
- unit_time_at(arg.convert_to('s').scalar, ms)
24
- else
25
- unit_time_at(arg.convert_to('s').scalar)
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
- # @return (see RubyUnits::Unit#initialize)
33
- def to_unit(other = nil)
34
- other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
35
- end
36
-
37
- unless Time.public_method_defined?(:to_date)
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
- # @example
60
- # Time.in '5 min'
61
- # @return (see Time#+)
62
- def self.in(duration)
63
- Time.now + duration.to_unit
64
- end
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
- alias unit_sub -
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
- # @return [RubyUnits::Unit, Time]
69
- def -(other)
70
- case other
71
- when RubyUnits::Unit
72
- other = other.convert_to('d').round.convert_to('s') if %w[y decade century].include? other.units
73
- begin
74
- unit_sub(other.convert_to('s').scalar)
75
- rescue RangeError
76
- send(:to_datetime) - other
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