Class: ActiveSupport::Duration::Scalar

Inherits:
Numeric show all
Defined in:
lib/active_support/duration.rb

Overview

:nodoc:

Constant Summary

Constants inherited from Numeric

Numeric::EXABYTE, Numeric::GIGABYTE, Numeric::KILOBYTE, Numeric::MEGABYTE, Numeric::PETABYTE, Numeric::TERABYTE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#as_json, #blank?, #bytes, #days, #exabytes, #fortnights, #gigabytes, #hours, #html_safe?, #in_milliseconds, #kilobytes, #megabytes, #minutes, #petabytes, #seconds, #terabytes, #weeks

Constructor Details

#initialize(value) ⇒ Scalar

Returns a new instance of Scalar.



18
19
20
# File 'lib/active_support/duration.rb', line 18

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/active_support/duration.rb', line 15

def value
  @value
end

Instance Method Details

#%(other) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/active_support/duration.rb', line 84

def %(other)
  if Duration === other
    Duration.build(value % other.value)
  else
    calculate(:%, other)
  end
end

#*(other) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/active_support/duration.rb', line 65

def *(other)
  if Duration === other
    new_parts = other.parts.transform_values { |other_value| value * other_value }
    new_value = value * other.value

    Duration.new(new_value, new_parts)
  else
    calculate(:*, other)
  end
end

#+(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_support/duration.rb', line 40

def +(other)
  if Duration === other
    seconds   = value + other.parts.fetch(:seconds, 0)
    new_parts = other.parts.merge(seconds: seconds)
    new_value = value + other.value

    Duration.new(new_value, new_parts)
  else
    calculate(:+, other)
  end
end

#-(other) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_support/duration.rb', line 52

def -(other)
  if Duration === other
    seconds   = value - other.parts.fetch(:seconds, 0)
    new_parts = other.parts.transform_values(&:-@)
    new_parts = new_parts.merge(seconds: seconds)
    new_value = value - other.value

    Duration.new(new_value, new_parts)
  else
    calculate(:-, other)
  end
end

#-@Object



26
27
28
# File 'lib/active_support/duration.rb', line 26

def -@
  Scalar.new(-value)
end

#/(other) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/active_support/duration.rb', line 76

def /(other)
  if Duration === other
    value / other.value
  else
    calculate(:/, other)
  end
end

#<=>(other) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/active_support/duration.rb', line 30

def <=>(other)
  if Scalar === other || Duration === other
    value <=> other.value
  elsif Numeric === other
    value <=> other
  else
    nil
  end
end

#coerce(other) ⇒ Object



22
23
24
# File 'lib/active_support/duration.rb', line 22

def coerce(other)
  [Scalar.new(other), self]
end