Class: ViewComponent::Storybook::Collections::ControlsCollection

Inherits:
Object
  • Object
show all
Includes:
ValidForStoryConcern
Defined in:
lib/view_component/storybook/collections/controls_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValidForStoryConcern

#valid_for_story?

Constructor Details

#initializeControlsCollection

Returns a new instance of ControlsCollection.



13
14
15
# File 'lib/view_component/storybook/collections/controls_collection.rb', line 13

def initialize
  @controls = []
end

Instance Attribute Details

#code_objectObject

Returns the value of attribute code_object.



11
12
13
# File 'lib/view_component/storybook/collections/controls_collection.rb', line 11

def code_object
  @code_object
end

#controlsObject (readonly)

Returns the value of attribute controls.



9
10
11
# File 'lib/view_component/storybook/collections/controls_collection.rb', line 9

def controls
  @controls
end

Instance Method Details

#add(param, as:, only: nil, except: nil, **opts) ⇒ Object



17
18
19
# File 'lib/view_component/storybook/collections/controls_collection.rb', line 17

def add(param, as:, only: nil, except: nil, **opts)
  controls << { param: param, as: as, only: only, except: except, **opts }
end

#for_story(story_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/view_component/storybook/collections/controls_collection.rb', line 21

def for_story(story_name)
  # build the controls for the story_name
  # pass through a hash to get the last valid control declared for each param
  controls.map do |opts|
    next unless valid_for_story?(story_name, **opts.slice(:only, :except))

    param = opts[:param]
    unless opts.key?(:default)
      opts = opts.merge(default: parse_default(story_name, param))
    end
    [param, build_control(param, **opts.except(:param, :only, :except))]
  end.compact.to_h.values
end