Module: Horologium

Defined in:
lib/horologium.rb,
lib/horologium/error.rb,
lib/horologium/instant.rb,
lib/horologium/version.rb,
lib/horologium/duration.rb,
lib/horologium/configuration.rb,
lib/horologium/numeric/exact.rb,
lib/horologium/precise_value.rb,
lib/horologium/numeric/precision.rb,
lib/horologium/numeric/two_part_float.rb,
sig/horologium.rbs,
sig/horologium/instant.rbs,
sig/horologium/numeric.rbs,
sig/horologium/duration.rbs,
sig/horologium/configuration.rbs,
sig/horologium/numeric/exact.rbs,
sig/horologium/precise_value.rbs,
sig/horologium/numeric/precision.rbs,
sig/horologium/numeric/two_part_float.rbs

Overview

Horologium is a Ruby library dedicated to scientific time: the time scales, high-precision instants, Julian Dates, intervals, and rigorous conversions between scales.

Defined Under Namespace

Modules: Numeric, PreciseValue Classes: Configuration, ConfigurationError, DimensionalError, Duration, Error, Instant, UnknownPrecisionError

Constant Summary collapse

VERSION =

Returns:

  • (String)
"0.0.2"

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Returns the current configuration, built with defaults if the library has not been configured yet.

Returns:

  • (Configuration)

    the current configuration, built with defaults if the library has not been configured yet



49
50
51
# File 'lib/horologium/configuration.rb', line 49

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Configuration

Configures the library. The yielded configuration is frozen when the block returns, so it can be set once at boot and not changed again.

Examples:

Horologium.configure do |c|
  c.default_precision = :exact
end

Yield Parameters:

Returns:



41
42
43
44
45
# File 'lib/horologium/configuration.rb', line 41

def configure
  config = configuration
  yield config if block_given?
  config.freeze
end

.current_precisionSymbol

The precision in effect right now: the one set by with_precision if a scope is open, otherwise the default. This is what a constructor consults when it is not given a precision of its own.

Returns:

  • (Symbol)

    :standard or :exact



63
64
65
# File 'lib/horologium/configuration.rb', line 63

def current_precision
  Thread.current[:horologium_current_precision] || default_precision
end

.default_precisionSymbol

Returns the configured default precision.

Returns:

  • (Symbol)

    the configured default precision



54
55
56
# File 'lib/horologium/configuration.rb', line 54

def default_precision
  configuration.default_precision
end

.reset_configuration!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Clears the configuration and any open precision scope. Meant for test isolation, so one test's configuration does not carry into another.



95
96
97
98
# File 'lib/horologium/configuration.rb', line 95

def reset_configuration!
  @configuration = nil
  Thread.current[:horologium_current_precision] = nil
end

.with_precision(precision) ⇒ Object

Runs the block with a chosen precision in effect, then restores whatever was in effect before. The scope is per-fiber, so it is safe to use in a threaded or fibered context and cannot leak into other work. It does not touch the frozen default.

Examples:

Horologium.with_precision(:exact) do
  # instants built here default to :exact
end

Parameters:

  • precision (Symbol)

    :standard or :exact

Returns:

  • (Object)

    the block's return value

Raises:



79
80
81
82
83
84
85
86
87
88
# File 'lib/horologium/configuration.rb', line 79

def with_precision(precision)
  Numeric::Precision.validate!(precision)
  previous = Thread.current[:horologium_current_precision]
  Thread.current[:horologium_current_precision] = precision
  begin
    yield
  ensure
    Thread.current[:horologium_current_precision] = previous
  end
end