Module: Textus::Protocol::Binder

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

Class Method Summary collapse

Class Method Details

.bind(spec, inputs) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/textus/protocol/binder.rb', line 16

def bind(spec, inputs)
  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.default.nil?
      h[a.name] = a.default
    end
  end
end

.inputs_from_ordered(spec, ordered_positionals, by_name_keywords) ⇒ Object



29
30
31
32
# File 'lib/textus/protocol/binder.rb', line 29

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



34
35
36
37
38
39
# File 'lib/textus/protocol/binder.rb', line 34

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