Class: ViewComponent::Storybook::StoryConfig

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, ContentConcern, Controls::ControlsHelpers
Defined in:
lib/view_component/storybook/story_config.rb

Defined Under Namespace

Classes: ValidationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Controls::ControlsHelpers

#array, #boolean, #check, #color, #custom, #date, #inline_check, #inline_radio, #klazz, #multi_select, #number, #object, #radio, #range, #select, #text

Methods included from ContentConcern

#content, #resolve_content_block

Constructor Details

#initialize(id, name, component_class, layout) ⇒ StoryConfig

Returns a new instance of StoryConfig.



14
15
16
17
18
19
20
# File 'lib/view_component/storybook/story_config.rb', line 14

def initialize(id, name, component_class, layout)
  @id = id
  @name = name
  @component_class = component_class
  @layout = layout
  @slots ||= {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



60
61
62
63
64
65
66
# File 'lib/view_component/storybook/story_config.rb', line 60

def method_missing(method_name, *args, **kwargs, &block)
  if component_class.slot_type(method_name)
    slot(method_name, *args, **kwargs, &block)
  else
    super
  end
end

Instance Attribute Details

#component_classObject (readonly)

Returns the value of attribute component_class.



10
11
12
# File 'lib/view_component/storybook/story_config.rb', line 10

def component_class
  @component_class
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/view_component/storybook/story_config.rb', line 10

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/view_component/storybook/story_config.rb', line 10

def name
  @name
end

Instance Method Details

#constructor(*args, **kwargs, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/view_component/storybook/story_config.rb', line 22

def constructor(*args, **kwargs, &block)
  @constructor_args = MethodArgs::ComponentConstructorArgs.from_component_class(
    component_class,
    *args,
    **kwargs
  )
  content(nil, &block)

  self
end

#controls(&block) ⇒ Object

Once deprecated block version is removed make this a private getter



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/view_component/storybook/story_config.rb', line 34

def controls(&block)
  if block_given?
    ActiveSupport::Deprecation.warn("`controls` will be removed in v1.0.0. Use `#constructor` instead.")
    controls_dsl = Dsl::LegacyControlsDsl.new
    controls_dsl.instance_eval(&block)

    controls_hash = controls_dsl.controls.index_by(&:param)
    constructor(**controls_hash)
  else
    list = constructor_args.controls.dup
    list << content_control if content_control
    list += slots.flat_map(&:controls) if slots
    list
  end
end

#layout(layout = nil) ⇒ Object



50
51
52
53
# File 'lib/view_component/storybook/story_config.rb', line 50

def layout(layout = nil)
  @layout = layout unless layout.nil?
  @layout
end

#parameters(parameters = nil) ⇒ Object



55
56
57
58
# File 'lib/view_component/storybook/story_config.rb', line 55

def parameters(parameters = nil)
  @parameters = parameters unless parameters.nil?
  @parameters
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/view_component/storybook/story_config.rb', line 68

def respond_to_missing?(method_name, _include_private = false)
  component_class.slot_type(method_name).present?
end

#story(params) ⇒ Object

Build a Story from this config

  • Resolves the values of the constructor args from the params

  • constructs the component

  • resolve the content_control and content_block to a single block

  • builds a list of Slots by resolving their args from the params



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/view_component/storybook/story_config.rb', line 92

def story(params)
  # constructor_args.target_method is UnboundMethod so can't call it directly
  component = constructor_args.call(params) do |*args, **kwargs|
    component_class.new(*args, **kwargs)
  end

  story_content_block = resolve_content_block(params)

  story_slots = slots.map do |slot_config|
    slot_config.slot(component, params)
  end

  Storybook::Story.new(component, story_content_block, story_slots, layout)
end

#to_csf_paramsObject



72
73
74
75
76
77
78
79
80
# File 'lib/view_component/storybook/story_config.rb', line 72

def to_csf_params
  validate!
  csf_params = { name: name, parameters: { server: { id: id } } }
  csf_params.deep_merge!(parameters: parameters) if parameters.present?
  controls.each do |control|
    csf_params.deep_merge!(control.to_csf_params)
  end
  csf_params
end

#validate!Object



82
83
84
# File 'lib/view_component/storybook/story_config.rb', line 82

def validate!
  valid? || raise(ValidationError, self)
end