Skip to content

Commit

Permalink
Remove tokens helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Jun 24, 2024
1 parent 6635d21 commit 8f6cce1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 170 deletions.
64 changes: 0 additions & 64 deletions lib/phlex/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,6 @@
module Phlex::Helpers
private

# Tokens
# @return [String]
# @example With Proc conditions
# tokens(
# -> { true } => "a",
# -> { false } => "b"
# )
# @example With method conditions
# tokens(
# active?: "active"
# )
# @example With else condition
# tokens(
# active?: { then: "active", else: "inactive" }
# )
def tokens(*tokens, **conditional_tokens)
conditional_tokens.each do |condition, token|
truthy = case condition
when Symbol then __send__(condition)
when Proc then condition.call
else raise ArgumentError.new("The class condition must be a Symbol or a Proc.")
end

if truthy
case token
when Hash then __append_token__(tokens, token[:then])
else __append_token__(tokens, token)
end
else
case token
when Hash then __append_token__(tokens, token[:else])
end
end
end

tokens = tokens.select(&:itself).join(" ")
tokens.strip!
tokens.gsub!(/\s+/, " ")
tokens
end

# @api private
def __append_token__(tokens, token)
case token
when nil then nil
when String then tokens << token
when Symbol then tokens << token.name
when Array then tokens.concat(token)
else raise ArgumentError.new("Conditional classes must be Symbols, Strings, or Arrays of Symbols or Strings.")
end
end

# Like {#tokens} but returns a {Hash} where the tokens are the value for `:class`.
# @return [Hash]
def classes(*tokens, **conditional_tokens)
tokens = self.tokens(*tokens, **conditional_tokens)

if tokens.empty?
{}
else
{ class: tokens }
end
end

# @return [Hash]
def mix(*args)
args.each_with_object({}) do |object, result|
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def view_template
with "context" do
view do
def view_template
div(class: tokens(-> { context[:theme] == :dark } => { then: "dark", else: "light" }))
div(class: [((context[:theme] == :dark) ? "dark" : "light")])
end
end

Expand Down
105 changes: 0 additions & 105 deletions test/phlex/view/tokens.rb

This file was deleted.

0 comments on commit 8f6cce1

Please sign in to comment.