Class: Weft::Params

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

Overview

Value object representing a component's resolved input bag. Provides method-style access with a clear collision-resolution rule: declared param names win, then the underlying Hash API is available for any name not declared as a param.

Entries may be lazy: a derives declaration registers a Thunk that runs (at most once per bag) when its key is first read, and never runs if the key goes unread. to_h and delegated Hash-API calls materialize every remaining thunk first — the eager escape hatch.

Action callables receive a ready-made instance (the sole argument to a +performs+/+transfers+ block); you don't construct these yourself:

params.status   # => "shipped"  (declared param)
params.count    # => 42         (declared param — wins over Hash#count)
params[:status] # => "shipped"  (explicit hash access)
params.select { ... }           # delegates to the underlying hash (materializes)
params.to_h     # => the underlying hash (explicit escape hatch; materializes)

Defined Under Namespace

Classes: Assembly, Thunk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, provenance = {}, defaults: {}) ⇒ Params

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Constructed internally (components self-resolve via the source stack; the Router wraps bags for action callables and recovery blocks). provenance maps derives-born keys to their block's source_location — retained through forcing so divergence stays detectable. defaults are the declaring class's own fallbacks, consulted when a read finds nothing. They are never stored as values, so they never ride a branch: a default belongs to whoever declared it, and a component deeper in the tree — or downstream of a hand-off — falls back to its own, not to the one above it.



48
49
50
51
52
53
# File 'lib/weft/params.rb', line 48

def initialize(data, provenance = {}, defaults: {})
  @data = data
  @provenance = provenance
  @defaults = defaults
  @forcing = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/weft/params.rb', line 95

def method_missing(name, *args, **kwargs, &block)
  if key?(name) && args.empty? && kwargs.empty? && !block
    self[name]
  elsif @data.respond_to?(name)
    materialized.public_send(name, *args, **kwargs, &block)
  else
    super
  end
end

Instance Attribute Details

#provenanceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
# File 'lib/weft/params.rb', line 56

def provenance
  @provenance
end

Instance Method Details

#[](key) ⇒ Object

nil means no source had this key — so the read falls to the declared fallback, exactly as it falls past a nil at any other level of the stack.



79
80
81
82
83
# File 'lib/weft/params.rb', line 79

def [](key)
  value = @data[key]
  value = force!(key, value) if value.is_a?(Thunk)
  value.nil? ? @defaults[key] : value
end

#branch_dataObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A branchable snapshot for the inheritance axis: forced values and still-lazy thunks both ride (thunks are shared objects — forcing happens per bag, which is what makes the memo copy-on-branch); nils don't ride (nil means "nobody had it" and must not shadow a descendant's own defaults).



64
65
66
# File 'lib/weft/params.rb', line 64

def branch_data
  @data.compact
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/weft/params.rb', line 85

def key?(key)
  @data.key?(key) || @defaults.key?(key)
end

#overlay(values) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A same-bag copy with values overlaid at their keys. Unlike to_h-then-merge, nothing materializes: untouched thunks stay lazy, nil entries stay resolved-absent, provenance rides. The plain-context hand-off fallback lands received values through this.



73
74
75
# File 'lib/weft/params.rb', line 73

def overlay(values)
  self.class.new(@data.merge(values), @provenance, defaults: @defaults)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/weft/params.rb', line 91

def respond_to_missing?(name, include_private = false)
  key?(name) || @data.respond_to?(name, include_private) || super
end

#to_hObject



89
# File 'lib/weft/params.rb', line 89

def to_h = materialized