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, Numeric::ZETTABYTE

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, #zettabytes

Constructor Details

#initialize(value) ⇒ Scalar

Returns a new instance of Scalar.



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

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#%(other) ⇒ Object



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

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

#*(other) ⇒ Object



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

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, other.variable?)
  else
    calculate(:*, other)
  end
end

#+(other) ⇒ Object



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

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, other.variable?)
  else
    calculate(:+, other)
  end
end

#-(other) ⇒ Object



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

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, other.variable?)
  else
    calculate(:-, other)
  end
end

#-@Object



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

def -@
  Scalar.new(-value)
end

#/(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



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

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

#coerce(other) ⇒ Object



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

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

#variable?Boolean

:nodoc:

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_support/duration.rb', line 93

def variable? # :nodoc:
  false
end