Class: ViewComponent::Storybook::MethodArgs::ControlMethodArgs

Inherits:
MethodArgs
  • Object
show all
Includes:
ActiveModel::Validations::Callbacks
Defined in:
lib/view_component/storybook/method_args/control_method_args.rb

Overview

Class representing arguments passed to a method which can be validated against the args of the target method In addition the args and kwargs can contain Controls the values of which can be resolved from a params hash

Instance Attribute Summary collapse

Attributes inherited from MethodArgs

#args, #kwargs, #target_method, #target_method_params_names

Instance Method Summary collapse

Methods inherited from MethodArgs

#initialize

Constructor Details

This class inherits a constructor from ViewComponent::Storybook::MethodArgs::MethodArgs

Instance Attribute Details

#param_prefixObject (readonly)

Returns the value of attribute param_prefix.



14
15
16
# File 'lib/view_component/storybook/method_args/control_method_args.rb', line 14

def param_prefix
  @param_prefix
end

Instance Method Details

#call(params, &target_block) ⇒ Object

resolve the controls values from the params call the target method or block with those values



27
28
29
30
31
# File 'lib/view_component/storybook/method_args/control_method_args.rb', line 27

def call(params, &target_block)
  method_args = resolve_method_args(params)

  (target_block || target_method).call(*method_args.args, **method_args.kwargs)
end

#controlsObject



46
47
48
# File 'lib/view_component/storybook/method_args/control_method_args.rb', line 46

def controls
  @controls ||= (args + kwargs.values).select(&method(:control?))
end

#resolve_method_args(params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/view_component/storybook/method_args/control_method_args.rb', line 33

def resolve_method_args(params)
  assign_control_params

  args_from_params = args.map do |arg|
    value_from_params(arg, params)
  end
  kwargs_from_params = kwargs.transform_values do |arg|
    value_from_params(arg, params)
  end

  MethodArgs.new(target_method, *args_from_params, **kwargs_from_params)
end

#with_param_prefix(prefix) ⇒ Object



19
20
21
22
# File 'lib/view_component/storybook/method_args/control_method_args.rb', line 19

def with_param_prefix(prefix)
  @param_prefix = prefix
  self
end