Class: Trailblazer::Macro::Contract::Validate::Extract

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/macro/contract/validate.rb

Overview

Task: extract the contract’s input from params by reading ‘:key`.

Instance Method Summary collapse

Constructor Details

#initialize(key_path: nil, params_path: nil) ⇒ Extract

Returns a new instance of Extract.



52
53
54
# File 'lib/trailblazer/macro/contract/validate.rb', line 52

def initialize(key_path: nil, params_path: nil)
  @key_path, @params_path = key_path, params_path
end

Instance Method Details

#call(ctx, params: {}) ⇒ Object



56
57
58
59
# File 'lib/trailblazer/macro/contract/validate.rb', line 56

def call(ctx, params: {}, **)
  key = ctx[@key_path] # e.g. {:song}.
  ctx[@params_path] = key ? params[key] : params
end