Class: SuperSettings::TimePrecision

Inherits:
Object
  • Object
show all
Defined in:
lib/super_settings/time_precision.rb

Overview

Helper class for truncating timestamps to a specific precision. This is used by storage engines to ensure that timestamps are stored and compared with the same precision.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, precision = :microsecond) ⇒ TimePrecision

Create a new TimePrecision object.

Parameters:

  • time (Time, Numeric)

    the time to apply precision to

  • precision (Symbol) (defaults to: :microsecond)

    the precision level (:microsecond or :millisecond)

Raises:

  • (ArgumentError)

    if precision is not valid



15
16
17
18
19
# File 'lib/super_settings/time_precision.rb', line 15

def initialize(time, precision = :microsecond)
  raise ArgumentError.new("Invalid precision: #{precision}") unless valid_precision?(precision)

  @time = time_with_precision(time.to_f, precision) if time
end

Instance Attribute Details

#timeObject (readonly)

The time value with applied precision.



8
9
10
# File 'lib/super_settings/time_precision.rb', line 8

def time
  @time
end

Instance Method Details

#to_fFloat

Convert the time to a float.

Returns:

  • (Float)

    the time as a floating point number



24
25
26
# File 'lib/super_settings/time_precision.rb', line 24

def to_f
  @time.to_f
end