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 then/else conditional token option #392

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 3 additions & 28 deletions lib/phlex/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,12 @@
end

module Phlex::Helpers
private

def tokens(*tokens, **conditional_tokens)
conditional_tokens.each do |condition, token|
case condition
when Symbol then next unless send(condition)
when Proc then next unless condition.call
else raise ArgumentError,
"The class condition must be a Symbol or a Proc."
end

case token
when Symbol then tokens << token.name
when String then tokens << token
when Array then tokens.concat(token)
else raise ArgumentError,
"Conditional classes must be Symbols, Strings, or Arrays of Symbols or Strings."
end
end

tokens.compact.join(" ")
def tokens(...)
Phlex::Helpers::TokenGenerator.new(self, ...).call
end

def classes(*tokens, **conditional_tokens)
tokens = self.tokens(*tokens, **conditional_tokens)

if tokens.empty?
{}
else
{ class: tokens }
end
Phlex::Helpers::TokenGenerator.new(self, *tokens, namespace: :class, **conditional_tokens).call
end

def mix(*args)
Expand Down
71 changes: 71 additions & 0 deletions lib/phlex/helpers/token_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

module Phlex::Helpers
class TokenGenerator
def initialize(context, *tokens, namespace: nil, **conditional_tokens)
raise ArgumentError, "Tokens should not be passed as an array" if tokens[0].is_a?(Array)

@tokens = tokens
@context = context
@conditional_tokens = conditional_tokens
@namespace = namespace
end

# Converts non-hash token values into a :then/:else hash structure
def self.normalize_conditional_tokens(tokens)
{}.tap do |validated_tokens|
tokens.each do |condition, value|
validated_tokens[condition] = begin
case value
when Hash then validate_hash!(value)
else { then: value }
end
end
end
end
end

# Ensures a hash token value structure has at minimum a :then entry
def self.validate_hash!(hash)
raise ArgumentError, "Token hash mush have a :then key" unless hash.key?(:then)

hash
end

def call
return {} if @namespace && all_tokens.empty?
return { @namespace.to_sym => all_tokens_value } if @namespace

all_tokens_value
end

private

# Joins all_tokens array with <space> separator
def all_tokens_value
all_tokens.join(" ")
end

# Returns array of tokens and conditional tokens that passed checks
def all_tokens
@all_tokens ||= [
*@tokens,
*normalized_conditional_tokens.map do |symbol_or_proc, token_hash|
result = case symbol_or_proc
when Symbol then @context.send(symbol_or_proc)
when Proc then symbol_or_proc.call
else raise ArgumentError,
"The class condition must be a Symbol or a Proc."
end

result ? token_hash[:then] : token_hash[:else]
end
].compact.uniq
end

# Memoized hash of normalized conditional tokens
def normalized_conditional_tokens
@normalized_conditional_tokens ||= TokenGenerator.normalize_conditional_tokens(@conditional_tokens)
end
end
end
46 changes: 46 additions & 0 deletions test/phlex/view/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,51 @@ def template
%(<a href="/" class="a b c true">Home</a>)
end
end

with "negative conditionals" do
view do
def template
a href: "/", **classes("a", "b", "c",
active?: {
then: "active",
else: "inactive"
}) do
"Home"
end
end

def active?
false
end
end

it "works" do
expect(output).to be ==
%(<a href="/" class="a b c inactive">Home</a>)
end
end

with "no truthy conditionals" do
view do
def template
a href: "/", **classes(
active?: {
then: "active"
}
) do
"Home"
end
end

def active?
false
end
end

it "works" do
expect(output).to be ==
%(<a href="/">Home</a>)
end
end
end
end