Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options contexts to set conventions for more involved definitions #3

Merged
merged 20 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions app/models/showcase/options.rb

This file was deleted.

32 changes: 32 additions & 0 deletions lib/showcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,38 @@ def self.previews
Dir.glob("**/*.*", base: File.join(root, previews_path))
end.uniq
end

def self.options
Options
end

options.define :stimulus do
def targets(name, ...)
option(%(data-#{@controller}-target="#{name}"), ...)
end

def values(name, ...)
option("data-#{@controller}-#{name}-value", ...)
end

def classes(name, ...)
option("data-#{@controller}-#{name}-class", ...)
end

def outlet(name, ...)
option("data-#{@controller}-#{name}-outlet", ...)
end

def action(name, ...)
option(%(data-action="#{name}"), ...)
end
end

options.define :nice_partials do
def content_block(*arguments, **options, &block)
option(*arguments, **options, type: "Content Block", &block)
end
end
kaspth marked this conversation as resolved.
Show resolved Hide resolved
end

require "showcase/engine" if defined?(Rails::Engine)
89 changes: 89 additions & 0 deletions lib/showcase/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require "active_support/option_merger"

class Showcase::Options
include Enumerable

def initialize(view_context)
@view_context = view_context
@options = []
@order = [:name, :required, :type, :default, :description]
end
delegate :empty?, to: :@options

# Showcase.options.define :stimulus do
# def value(name, ...)
# option("data-#{@controller}-#{name}-value", ...)
# end
# end
singleton_class.attr_reader :contexts
@contexts = Hash.new { |h,k| h[k] = Class.new Context }

def self.define(key, &block)
contexts[key].class_eval(&block) # Lets users reopen an already defined context class.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, if people override an already defined method they may break Showcases from engines, which may rely on the default context method definition.

end

# showcase.options.stimulus controller: :welcome do |o|
# o.value :greeting, default: "Hello"
# end
def context(key, **options, &block)
context = self.class.contexts.fetch(key)
context.new(@view_context, @options, **options).tap { yield _1 if block_given? }
end

def required(*arguments, **keywords, &block)
if arguments.none?
ActiveSupport::OptionMerger.new(self, required: true)
else
option(*arguments, **keywords, required: true, &block)
end
end

def optional(*arguments, **keywords, &block)
if arguments.none?
ActiveSupport::OptionMerger.new(self, required: false)
else
option(*arguments, **keywords, required: false, &block)
end
end

DEFAULT_OMITTED = Object.new

def option(name, description = nil, required: false, type: nil, default: DEFAULT_OMITTED, **options, &block)
description ||= @view_context.capture(&block).remove(/^\s+/).html_safe if block

type ||= type_from_default(default)
default = default == DEFAULT_OMITTED ? nil : default.inspect

@options << options.with_defaults(name: name, default: default, type: type, description: description, required: required)
end

def headers
@headers ||= @order | @options.flat_map(&:keys).uniq.sort
end

def each(&block)
@options.each do |option|
yield headers.index_with { option[_1] }
end
end

private

class Context < Showcase::Options
def initialize(view_context, options, **kwargs)
super(view_context)
@options = options
kwargs.each { instance_variable_set(:"@#{_1}", _2) }
end
end

def type_from_default(default)
case default
when DEFAULT_OMITTED then String
when true, false then "Boolean"
when nil then "nil"
else
default.class
end
end
end
9 changes: 7 additions & 2 deletions test/controllers/showcase/previews_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ class Showcase::PreviewsControllerTest < Showcase::InternalIntegrationTest

within :section, "Options" do
assert_table with_rows: [
{"Name" => "data-welcome-target", "Required" => "", "Type" => "String", "Default" => "", "Description" => "If the id of the target element must be printed"},
{"Name" => "data-welcome-yell-value", "Required" => "", "Type" => "Boolean", "Default" => "false", "Description" => "Whether the hello is to be YELLED"}
{"Name" => %(data-welcome-target="greeter"), "Required" => "", "Type" => "String", "Default" => "", "Description" => "If the id of the target element must be printed"},
{"Name" => "data-welcome-yell-value", "Required" => "", "Type" => "Boolean", "Default" => "false", "Description" => "Whether the hello is to be YELLED"},
{"Name" => "data-welcome-success-class", "Required" => "", "Type" => "String", "Default" => "", "Description" => "The success class to append after greeting"},
{"Name" => "data-welcome-list-outlet", "Required" => "", "Type" => "String", "Default" => "", "Description" => "An outlet to append each yelled greeter to"},
{"Name" => %(data-action="greet"), "Required" => "", "Type" => "String", "Default" => "", "Description" => "An action to repeat the greeting, if need be"}
]

assert_element "input", count: 3
kaspth marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
<div data-controller="welcome" data-welcome-yell-value="true">
<% end %>

<% showcase.options do |o| %>
<% o.optional "data-welcome-target", "If the id of the target element must be printed" %>
<% o.optional "data-welcome-yell-value", "Whether the hello is to be YELLED", default: false %>
<% showcase.options.context :stimulus, controller: :welcome do |o| %>
<% o.optional.targets :greeter, "If the id of the target element must be printed" %>
<% o.required.values :yell, "Whether the hello is to be YELLED", default: false %>
<% o.required.classes :success, "The success class to append after greeting" %>
<% o.required.outlet :list, "An outlet to append each yelled greeter to" %>
<% o.optional.action :greet, "An action to repeat the greeting, if need be" %>
Comment on lines +17 to +22
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels pretty clean and intention revealing!

<% end %>