Module: Textus::Contract::Sources

Defined in:
lib/textus/contract/sources.rb

Overview

CLI-only input acquisition. Transforms entries of the uniform ‘inputs` hash that declare a `source:`/`coerce:`, and builds `inputs` from a `cli_stdin` envelope — so put/propose/migrate/rule_lint/audit need no hand-authored CLI class (ADR 0068). MCP receives typed JSON, so these never run there.

Class Method Summary collapse

Class Method Details

.acquire(spec, inputs) ⇒ Object

Apply per-arg :file sources (value is a path -> file contents) and :coerce callables to a by-name inputs hash. Returns a new hash.



15
16
17
18
19
20
21
22
# File 'lib/textus/contract/sources.rb', line 15

def acquire(spec, inputs)
  spec.args.each_with_object(inputs.dup) do |a, h|
    next unless h.key?(a.name)

    h[a.name] = File.read(h[a.name]) if a.source == :file
    h[a.name] = a.coerce.call(h[a.name]) if a.coerce
  end
end

.from_stdin(spec, stream) ⇒ Object

Parse a cli_stdin :json envelope into a by-name inputs hash, mapping envelope keys (wire-names) to arg names.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/textus/contract/sources.rb', line 26

def from_stdin(spec, stream)
  return {} unless spec.cli_stdin == :json

  raw = stream.read.to_s
  return {} if raw.strip.empty? # no envelope piped -> required args surface as missing

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