-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f627987
commit 9417744
Showing
14 changed files
with
227 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/dependencies/autoload" | ||
|
||
module ViewComponent | ||
module Storybook | ||
module Collections | ||
extend ActiveSupport::Autoload | ||
|
||
autoload :ValidForStoryConcern | ||
autoload :StoriesCollection | ||
autoload :ControlsCollection | ||
autoload :ParametersCollection | ||
autoload :LayoutCollection | ||
end | ||
end | ||
end |
80 changes: 80 additions & 0 deletions
80
lib/view_component/storybook/collections/controls_collection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
module Storybook | ||
module Collections | ||
class ControlsCollection | ||
include Collections::ValidForStoryConcern | ||
|
||
attr_reader :controls | ||
|
||
attr_accessor :code_object | ||
|
||
def initialize | ||
@controls = [] | ||
end | ||
|
||
def add(param, as:, only: nil, except: nil, **opts) | ||
controls << { param: param, as: as, only: only, except: except, **opts } | ||
end | ||
|
||
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 | ||
|
||
private | ||
|
||
def parse_default(story_name, param) | ||
code_method = code_object.meths.find { |m| m.name == story_name } | ||
default_value_parts = code_method.parameters.find { |parts| parts[0].chomp(":") == param.to_s } | ||
return unless default_value_parts | ||
|
||
code_method.instance_eval(default_value_parts[1]) | ||
end | ||
|
||
def build_control(param, as:, **opts) | ||
case as | ||
when :text | ||
Controls::Text.new(param, **opts) | ||
when :boolean | ||
Controls::Boolean.new(param, **opts) | ||
when :number | ||
Controls::Number.new(param, type: :number, **opts) | ||
when :range | ||
Controls::Number.new(param, type: :range, **opts) | ||
when :color | ||
Controls::Color.new(param, **opts) | ||
when :object, :array | ||
Controls::Object.new(param, **opts) | ||
when :select | ||
Controls::Options.new(param, type: :select, **opts) | ||
when :multi_select | ||
Controls::MultiOptions.new(param, type: :'multi-select', **opts) | ||
when :radio | ||
Controls::Options.new(param, type: :radio, **opts) | ||
when :inline_radio | ||
Controls::Options.new(param, type: :'inline-radio', **opts) | ||
when :check | ||
Controls::MultiOptions.new(param, type: :check, **opts) | ||
when :inline_check | ||
Controls::MultiOptions.new(param, type: :'inline-check', **opts) | ||
when :date | ||
Controls::Date.new(param, **opts) | ||
else | ||
raise "Unknonwn control type '#{as}'" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
lib/view_component/storybook/collections/layout_collection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
module Storybook | ||
module Collections | ||
class LayoutCollection | ||
include Collections::ValidForStoryConcern | ||
|
||
def initialize | ||
@default = nil | ||
@layouts = [] | ||
end | ||
|
||
def add(layout, only: nil, except: nil) | ||
if only.nil? && except.nil? | ||
@default = layout | ||
else | ||
layouts << { layout: layout, only: only, except: except } | ||
end | ||
end | ||
|
||
# Parameters set for the story method | ||
def for_story(story_name) | ||
story_layout = default | ||
layouts.each do |opts| | ||
story_layout = opts[:layout] if valid_for_story?(story_name, **opts.slice(:only, :except)) | ||
end | ||
story_layout | ||
end | ||
|
||
private | ||
|
||
attr_reader :default, :layouts | ||
end | ||
end | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
lib/view_component/storybook/collections/parameters_collection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
module Storybook | ||
module Collections | ||
class ParametersCollection | ||
include Collections::ValidForStoryConcern | ||
|
||
def initialize | ||
@all_paramsters = {} | ||
@parameters = [] | ||
end | ||
|
||
def add(params, only: nil, except: nil) | ||
if only.nil? && except.nil? | ||
all_paramsters.merge!(params) | ||
else | ||
parameters << { params: params, only: only, except: except } | ||
end | ||
end | ||
|
||
# Parameters set for all stories | ||
def for_all | ||
all_paramsters | ||
end | ||
|
||
# Parameters set for the story method | ||
def for_story(story_name) | ||
parameters.each_with_object({}) do |opts, accum| | ||
accum.merge!(opts[:params]) if valid_for_story?(story_name, **opts.slice(:only, :except)) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :all_paramsters, :parameters | ||
end | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
lib/view_component/storybook/collections/stories_collection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
module Storybook | ||
module Collections | ||
class StoriesCollection | ||
include Enumerable | ||
|
||
delegate_missing_to :stories | ||
|
||
attr_reader :stories | ||
|
||
def load(code_objects) | ||
@stories = Array(code_objects).map { |obj| StoriesCollection.stories_from_code_object(obj) }.compact | ||
end | ||
|
||
def self.stories_from_code_object(code_object) | ||
klass = code_object.path.constantize | ||
klass.code_object = code_object | ||
klass | ||
end | ||
|
||
def self.stories_class?(klass) | ||
return unless klass.ancestors.include?(ViewComponent::Storybook::Stories) | ||
|
||
!klass.respond_to?(:abstract_class) || klass.abstract_class != true | ||
end | ||
end | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
lib/view_component/storybook/collections/valid_for_story_concern.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
module Storybook | ||
module Collections | ||
module ValidForStoryConcern | ||
extend ActiveSupport::Concern | ||
|
||
def valid_for_story?(story_name, only:, except:) | ||
(only.nil? || Array.wrap(only).include?(story_name)) && Array.wrap(except).exclude?(story_name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.