123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::Duration::Scalar

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Numeric
Instance Chain:
self, ::Numeric
Inherits: Numeric
Defined in: activesupport/lib/active_support/duration.rb

Constant Summary

::Numeric - Inherited

EXABYTE, GIGABYTE, KILOBYTE, MEGABYTE, PETABYTE, TERABYTE, ZETTABYTE

Class Method Summary

Instance Attribute Summary

::Numeric - Inherited

#html_safe?,
#blank?

No number is blank:

Instance Method Summary

::Numeric - Inherited

#byte

Alias for Numeric#bytes.

#bytes

Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes.

#day

Alias for Numeric#days.

#days

Returns a ::ActiveSupport::Duration instance matching the number of days provided.

#exabyte

Alias for Numeric#exabytes.

#exabytes

Returns the number of bytes equivalent to the exabytes provided.

#fortnight
#fortnights

Returns a ::ActiveSupport::Duration instance matching the number of fortnights provided.

#gigabyte
#gigabytes

Returns the number of bytes equivalent to the gigabytes provided.

#hour

Alias for Numeric#hours.

#hours

Returns a ::ActiveSupport::Duration instance matching the number of hours provided.

#in_milliseconds

Returns the number of milliseconds equivalent to the seconds provided.

#kilobyte
#kilobytes

Returns the number of bytes equivalent to the kilobytes provided.

#megabyte
#megabytes

Returns the number of bytes equivalent to the megabytes provided.

#minute

Alias for Numeric#minutes.

#minutes

Returns a ::ActiveSupport::Duration instance matching the number of minutes provided.

#petabyte
#petabytes

Returns the number of bytes equivalent to the petabytes provided.

#second

Alias for Numeric#seconds.

#seconds

Returns a ::ActiveSupport::Duration instance matching the number of seconds provided.

#terabyte
#terabytes

Returns the number of bytes equivalent to the terabytes provided.

#week

Alias for Numeric#weeks.

#weeks

Returns a ::ActiveSupport::Duration instance matching the number of weeks provided.

#zettabyte
#zettabytes

Returns the number of bytes equivalent to the zettabytes provided.

#as_json

Constructor Details

.new(value) ⇒ Scalar

[ GitHub ]

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

def initialize(value)
  @value = value
end

Instance Attribute Details

#value (readonly)

[ GitHub ]

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

attr_reader :value

#variable?Boolean (readonly)

[ GitHub ]

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

def variable? # :nodoc:
  false
end

Instance Method Details

#%(other)

[ GitHub ]

  
# File 'activesupport/lib/active_support/duration.rb', line 85

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

#*(other)

[ GitHub ]

  
# File 'activesupport/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)

[ GitHub ]

  
# File 'activesupport/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)

[ GitHub ]

  
# File 'activesupport/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

#-@

[ GitHub ]

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

def -@
  Scalar.new(-value)
end

#/(other)

[ GitHub ]

  
# File 'activesupport/lib/active_support/duration.rb', line 77

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

#<=>(other)

[ GitHub ]

  
# File 'activesupport/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

#calculate(op, other) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/duration.rb', line 98

def calculate(op, other)
  if Scalar === other
    Scalar.new(value.public_send(op, other.value))
  elsif Numeric === other
    Scalar.new(value.public_send(op, other))
  else
    raise_type_error(other)
  end
end

#coerce(other)

[ GitHub ]

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

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

#raise_type_error(other) (private)

Raises:

  • (TypeError)
[ GitHub ]

  
# File 'activesupport/lib/active_support/duration.rb', line 108

def raise_type_error(other)
  raise TypeError, "no implicit conversion of #{other.class} into #{self.class}"
end

#to_f

[ GitHub ]

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

delegate :to_i, :to_f, :to_s, to: :value

#to_i

[ GitHub ]

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

delegate :to_i, :to_f, :to_s, to: :value

#to_s

[ GitHub ]

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

delegate :to_i, :to_f, :to_s, to: :value