Class: ActionView::Storybook::StoryConfig

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/action_view/storybook/story_config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of StoryConfig.



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

def initialize(id, name, layout, template)
  @id = id
  @name = name
  @layout = layout
  @template = template
  @controls = []
end

Instance Attribute Details

#content_blockObject

Returns the value of attribute content_block.



9
10
11
# File 'lib/action_view/storybook/story_config.rb', line 9

def content_block
  @content_block
end

#controlsObject

Returns the value of attribute controls.



9
10
11
# File 'lib/action_view/storybook/story_config.rb', line 9

def controls
  @controls
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/action_view/storybook/story_config.rb', line 8

def id
  @id
end

#layoutObject

Returns the value of attribute layout.



9
10
11
# File 'lib/action_view/storybook/story_config.rb', line 9

def layout
  @layout
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/action_view/storybook/story_config.rb', line 8

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



9
10
11
# File 'lib/action_view/storybook/story_config.rb', line 9

def parameters
  @parameters
end

#templateObject (readonly)

Returns the value of attribute template.



8
9
10
# File 'lib/action_view/storybook/story_config.rb', line 8

def template
  @template
end

Class Method Details

.configure(id, name, layout, template, &configuration) ⇒ Object



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

def self.configure(id, name, layout, template, &configuration)
  config = new(id, name, layout, template)
  ActionView::Storybook::Dsl::StoryDsl.evaluate!(config, &configuration)
  config
end

Instance Method Details

#pretty_name(name) ⇒ Object



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

def pretty_name(name)
  name.to_s.humanize.titlecase
end

#to_csf_paramsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/action_view/storybook/story_config.rb', line 21

def to_csf_params
  validate!
  csf_params = {
    name: pretty_name(name),
    parameters: {
      server: {
        id: id,
        params: {
          story_name: name
        }
      }
    }
  }
  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

#values_from_params(params) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/action_view/storybook/story_config.rb', line 41

def values_from_params(params)
  controls.map do |control|
    value = control.value_from_param(params[control.param])
    value = control.value if value.nil? # nil only not falsey
    [control.param, value]
  end.to_h
end