Module: Tins::Duration::ConnectorResolver

Included in:
Tins::Duration, Tins::Duration
Defined in:
lib/tins/duration.rb

Overview

Resolves a HUMAN_CONNECTORS preset to its connector hash.

Shared by #to_human and parse_human so that both accept the same connector: keyword argument without duplicating the nil / Symbol / Hash dispatch.

Instance Method Summary collapse

Instance Method Details

#resolve_connector(connector, style) ⇒ Hash

Resolves a connector specification to a Hash with keys :words, :two_words, and :last_word.

Parameters:

  • connector (Symbol, Hash, nil) —

    A preset name from HUMAN_CONNECTORS, a custom hash, or nil (derive from style via STYLE_DEFAULT_CONNECTOR).

  • style (Symbol, Hash) —

    The label style passed alongside, used only when connector is nil.

Returns:

  • (Hash) —

    The resolved connector hash.

Raises:

  • (ArgumentError) —

    If connector is neither nil, a Symbol, nor a Hash.

See Also:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tins/duration.rb', line 43

def resolve_connector(connector, style)
  case connector
  when nil
    key = style.is_a?(Symbol) ? style : :abbrev
    HUMAN_CONNECTORS.fetch(STYLE_DEFAULT_CONNECTOR.fetch(key))
  when Symbol then HUMAN_CONNECTORS.fetch(connector)
  when Hash   then connector
  else raise ArgumentError,
    "connector must be a Symbol or Hash, got #{connector.class}"
  end
end