Class: Weft::Params::Assembly

Inherits:
Object
  • Object
show all
Defined in:
lib/weft/params/assembly.rb

Overview

Composes a params bag from every source that can supply one. The single place that knows the source stack — components call it at construction for their own bag, and the Router calls it at the top of a request for the bag it hands the first verb block.

The stack, top wins. nil never wins a level: it means that source didn't have the key.

1. hand-off   a caller staged a value for this component (`receives`)
2. overlay    a verb block earlier in this request returned the key
3. own wire   the request supplied it and this class declares it
4. inherited  the bag this one branched from had it
5. derivation this class declares one (registered lazily, never run here)
6. default    the declaration's own fallback

The overlay speaks as the wire: its value replaces the wire's for that key, and an explicit nil clears — masking the wire so resolution falls below it. A derivation always "produces" (a thunk is never nil), so a same-key default sits unreachable behind one.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component_class, wire_source, hand_offs: {}, overlays: {}, branched_from: nil) ⇒ Assembly

branched_from is the bag this one inherits: a tree ancestor's during a render, the state already composed at the top of a request. Passing hand_offs: nil says the door does not exist (see .for_request); an empty hash says it exists and nobody staged anything.



49
50
51
52
53
54
55
56
57
# File 'lib/weft/params/assembly.rb', line 49

def initialize(component_class, wire_source, hand_offs: {}, overlays: {}, branched_from: nil)
  @component_class = component_class
  @received = hand_offs || {}
  @hand_offs = !hand_offs.nil?
  @overlays = overlays
  @wire = Weft::Resolver.resolve_present(component_class, wire_source)
  @inherited = branched_from ? branched_from.branch_data : {}
  @upstream_provenance = branched_from ? branched_from.provenance : {}
end

Class Method Details

.callObject



29
# File 'lib/weft/params/assembly.rb', line 29

def call(...) = new(...).bag

.for_request(component_class, wire_source) ⇒ Object

The state a request composes before any component of its own exists — what the first verb block sees. No caller and no enclosing build have run, so the hand-off door isn't merely unsatisfied, it isn't there: a receives-only key is absent, declared default and all. Dual it with a param or a defines to make the key visible here.



36
37
38
# File 'lib/weft/params/assembly.rb', line 36

def for_request(component_class, wire_source)
  call(component_class, wire_source, hand_offs: nil)
end

.warnedObject

One-time shadowing warnings, keyed [kind, class, key]. Set#add? races just double-warn; harmless.



42
# File 'lib/weft/params/assembly.rb', line 42

def warned = @warned ||= Set.new

Instance Method Details

#bagObject



59
60
61
62
63
64
# File 'lib/weft/params/assembly.rb', line 59

def bag
  data = @inherited.dup
  keys.each { |key| data[key] = stack_value(key) }
  report_shadowed_derivations(data)
  Weft::Params.new(data, provenance, defaults: declared_defaults)
end