Skip to content

Commit

Permalink
Move all the collections
Browse files Browse the repository at this point in the history
  • Loading branch information
jonspalmer committed Jan 7, 2023
1 parent f627987 commit 9417744
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 195 deletions.
6 changes: 2 additions & 4 deletions lib/view_component/storybook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ module Storybook
extend ActiveSupport::Autoload

autoload :Controls
autoload :Collections
autoload :Stories
autoload :StoriesParser
autoload :StoriesCollection
autoload :ControlsCollection
autoload :ParametersCollection
autoload :LayoutCollection

autoload :Story
autoload :Slots

Expand Down
17 changes: 17 additions & 0 deletions lib/view_component/storybook/collections.rb
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 lib/view_component/storybook/collections/controls_collection.rb
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 lib/view_component/storybook/collections/layout_collection.rb
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 lib/view_component/storybook/collections/parameters_collection.rb
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 lib/view_component/storybook/collections/stories_collection.rb
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
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
80 changes: 0 additions & 80 deletions lib/view_component/storybook/controls_collection.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/view_component/storybook/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def parser

class << self
def stories
@stories ||= StoriesCollection.new
@stories ||= Collections::StoriesCollection.new
end
end
end
Expand Down
37 changes: 0 additions & 37 deletions lib/view_component/storybook/layout_collection.rb

This file was deleted.

Loading

0 comments on commit 9417744

Please sign in to comment.