Module: Textus::Gate::Binder

Defined in:
lib/textus/gate/binder.rb

Overview

Validates and resolves a by-name inputs hash against a contract spec. Returns a flat hash with defaults and session_defaults filled in. Every caller receives the same shape — no positional/kwarg split.

Class Method Summary collapse

Class Method Details

.bind(spec, inputs, session: nil) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/textus/gate/binder.rb', line 22

def bind(spec, inputs, session: nil)
  missing = spec.required_args.reject { |a| inputs.key?(a.name) }
  raise MissingArgs.new(spec, missing) unless missing.empty?

  spec.args.each_with_object({}) do |a, h|
    if inputs.key?(a.name)
      h[a.name] = inputs[a.name]
    elsif a.session_default && session
      h[a.name] = session.public_send(a.session_default)
    elsif !a.default.nil?
      h[a.name] = a.default
    end
  end
end

.inputs_from_ordered(spec, ordered_positionals, by_name_keywords) ⇒ Object



37
38
39
40
# File 'lib/textus/gate/binder.rb', line 37

def inputs_from_ordered(spec, ordered_positionals, by_name_keywords)
  names = spec.args.select(&:positional).map(&:name)
  names.zip(ordered_positionals).to_h.compact.merge(by_name_keywords)
end

.inputs_from_wire(spec, raw) ⇒ Object



42
43
44
45
46
47
# File 'lib/textus/gate/binder.rb', line 42

def inputs_from_wire(spec, raw)
  raw ||= {}
  spec.args.each_with_object({}) do |a, h|
    h[a.name] = raw[a.wire.to_s] if raw.key?(a.wire.to_s)
  end
end