Module: Weft::DSL::Params::ClassMethods

Defined in:
lib/weft/dsl/params.rb

Instance Method Summary collapse

Instance Method Details

#declared_keysObject

Every key this class declares, whichever door it came through — what a bag assembled for this class will hold entries for.



97
98
99
# File 'lib/weft/dsl/params.rb', line 97

def declared_keys
  params.keys | received_params.keys | derived_params.keys
end

#defines(pairs) ⇒ Object

Sugar for statically-known derivations: each pair registers derives(key) { value }. This is just derives — identical priority, overridability, and laziness; only the value is fixed at declaration. For anything computed per render (queries, clocks), use derives — an interpolated value here would freeze at class-load time. defines label: "Drivers", accent: "available"



87
88
89
90
91
92
93
# File 'lib/weft/dsl/params.rb', line 87

def defines(pairs)
  site = caller_locations(1, 1).first
  pairs.each do |name, value|
    own_derived_params[name] = { block: proc { |_p| value },
                                 source_location: [site.path, site.lineno] }
  end
end

#derived_paramsObject

All declared derivations (own + inherited), preserving declaration order. A child redeclaring a parent's key replaces the block, like a method override.



104
105
106
107
108
109
110
# File 'lib/weft/dsl/params.rb', line 104

def derived_params
  if superclass.respond_to?(:derived_params)
    superclass.derived_params.merge(own_derived_params)
  else
    own_derived_params.dup
  end
end

#derives(name, &block) ⇒ Object

Declare a lazy server-side derivation: the block runs (at most once per render) when params.name is first read, never if it isn't. derives(:order) { |params| Order.find(params.order_id) } The block is a (params) -> value pure function with a void self. Derived values are server-side: never serialized, not routable-making.



71
72
73
74
75
76
77
78
# File 'lib/weft/dsl/params.rb', line 71

def derives(name, &block)
  unless block
    raise Weft::InvalidDefinition,
          "derives #{name.inspect} requires a block — the derivation is the declaration"
  end

  own_derived_params[name] = { block: block, source_location: block.source_location }
end

#param(name, default: nil, type: nil) ⇒ Object

Declare a wire param. default: fills the key when no source supplies it; type: coerces the wire's string into the declared type (see Resolver::TYPES). The two are orthogonal — an untyped param accepts any value uncoerced — but a non-nil default must already be an instance of the declared type. param :page, type: :integer param :status, default: "active", type: :string



25
26
27
28
29
30
# File 'lib/weft/dsl/params.rb', line 25

def param(name, default: nil, type: nil)
  validate_type!(name, type, default) unless type.nil?
  meta = { default: default }
  meta[:type] = type unless type.nil?
  own_params[name] = meta
end

#paramsObject

Returns all declared params (own + inherited), preserving declaration order.



33
34
35
36
37
38
39
# File 'lib/weft/dsl/params.rb', line 33

def params
  if superclass.respond_to?(:params)
    superclass.params.merge(own_params)
  else
    own_params.dup
  end
end

#received_paramsObject

All declared hand-offs (own + inherited), preserving declaration order. Kept separate from params — the wire door and the hand-off door differ in serialization and routability, even for dual keys.



57
58
59
60
61
62
63
# File 'lib/weft/dsl/params.rb', line 57

def received_params
  if superclass.respond_to?(:received_params)
    superclass.received_params.merge(own_received_params)
  else
    own_received_params.dup
  end
end

#receives(name, **options) ⇒ Object

Declare a hand-off param: the caller provides the value as a builder kwarg at the call site; it lands in params, never in HTML chrome. receives :order # required — absence raises receives :page_num, default: 1 # optional — any declared default # (even nil) softens absence Hand-offs are server-side values: they never serialize into URLs and don't make a component routable.



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

def receives(name, **options)
  meta = {}
  meta[:default] = options[:default] if options.key?(:default)
  own_received_params[name] = meta
end