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 7 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions app/models/showcase/options.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Showcase::Options
include Enumerable
include Enumerable, Contexts

def initialize(view_context)
@view_context = view_context
Expand All @@ -9,11 +9,19 @@ def initialize(view_context)
delegate :empty?, to: :@options

def required(*arguments, **keywords, &block)
option(*arguments, **keywords, required: true, &block)
if arguments.none?
with_options required: true
else
option(*arguments, **keywords, required: true, &block)
end
end

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

DEFAULT_OMITTED = Object.new
Expand Down
65 changes: 65 additions & 0 deletions app/models/showcase/options/contexts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Showcase::Options::Contexts
class Context < Showcase::Options
def initialize(view_context, options, **kwargs)
super(view_context)
@options = options
kwargs.each { instance_variable_set(:"@#{_1}", _2) }
end
end

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

module ClassMethods
# Showcase.options.define :stimulus do
# def value(name, ...)
# option("data-#{@controller}-#{name}-value", ...)
# end
# end
#
# showcase.options.stimulus controller: :welcome do |o|
# o.value :greeting, default: "Hello"
# end
attr_reader :contexts

def define(key, &block)
contexts[key].class_eval(&block) # Lets users reopen an already defined context class.
end
end

def self.included(klass)
klass.extend ClassMethods
klass.instance_variable_set :@contexts, Hash.new { |h,k| h[k] = Class.new Context }

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

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

def klass(name, ...)
kaspth marked this conversation as resolved.
Show resolved Hide resolved
option("data-#{@controller}-#{name}-class", ...)
end

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

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

klass.define :nice_partials do
def content_block(*arguments, **options, &block)
option(*arguments, **options, type: "Content Block", &block)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/showcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def self.previews
Dir.glob("**/*.*", base: File.join(root, previews_path))
end.uniq
end

def self.options
Options
end
end

require "showcase/engine" if defined?(Rails::Engine)
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.target :greeter, "If the id of the target element must be printed" %>
<% o.required.value :yell, "Whether the hello is to be YELLED", default: false %>
<% o.required.klass :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" %>
<% end %>