Module: Hubbado::Sequence::Path

Defined in:
lib/hubbado/sequence/path.rb

Overview

Resolves a ctx path expressed as a single Symbol (one-key shorthand) or an Array of Symbols (nested fetch). Used by macros that need to read a value out of ctx at a configurable location.

‘missing:` selects how an absent key is reported:

:raise (default) — propagate KeyError. Right for Find/Validate/Build,
                   where a missing path is a wiring bug or a not-found.
:nil             — return nil. Right for Deserialize, which runs ahead
                   of validation and may legitimately encounter absent
                   params (e.g. a fresh GET before the form is posted).

Class Method Summary collapse

Class Method Details

.resolve(ctx, path, missing: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hubbado/sequence/path.rb', line 14

def self.resolve(ctx, path, missing: nil)
  missing ||= :raise

  unless %i[raise nil].include?(missing)
    raise ArgumentError, "unknown missing policy: #{missing.inspect}"
  end

  if path.is_a?(Array) && path.empty?
    raise ArgumentError, "path cannot be empty"
  end

  Array(path).reduce(ctx) do |acc, key|
    if missing == :nil
      acc.fetch(key) { return nil }
    else
      acc.fetch(key)
    end
  end
end