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, #negative?, #petabytes, #positive?, #seconds, #terabytes, #weeks

Constructor Details

#initialize(value) ⇒ Scalar

Returns a new instance of Scalar.



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

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#*(other) ⇒ Object



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

def *(other)
  if Duration === other
    new_parts = other.parts.map { |part, other_value| [part, value * other_value] }.to_h
    new_value = value * other.value

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

#+(other) ⇒ Object



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

def +(other)
  if Duration === other
    seconds   = value + other.parts[:seconds]
    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



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

def -(other)
  if Duration === other
    seconds   = value - other.parts[:seconds]
    new_parts = other.parts.map { |part, other_value| [part, -other_value] }.to_h
    new_parts = new_parts.merge(seconds: seconds)
    new_value = value - other.value

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

#-@Object



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

def -@
  Scalar.new(-value)
end

#/(other) ⇒ Object



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

def /(other)
  if Duration === other
    new_parts = other.parts.map { |part, other_value| [part, value / other_value] }.to_h
    new_value = new_parts.inject(0) { |total, (part, value)| total + value * Duration::PARTS_IN_SECONDS[part] }

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

#<=>(other) ⇒ Object



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

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

#coerce(other) ⇒ Object



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

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