Module: Phlex::Helpers

Included in:
HTML
Defined in:
lib/phlex/helpers.rb

Instance Method Summary collapse

Instance Method Details

#classes(*tokens, **conditional_tokens) ⇒ Hash (private)

Like #tokens but returns a Hash where the tokens are the value for :class.

Returns:

  • (Hash)


65
66
67
68
69
70
71
72
73
# File 'lib/phlex/helpers.rb', line 65

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

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

#mix(*args) ⇒ Hash (private)

Returns:

  • (Hash)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/phlex/helpers.rb', line 76

def mix(*args)
	args.each_with_object({}) do |object, result|
		result.merge!(object) do |_key, old, new|
			case new
			when Hash
				old.is_a?(Hash) ? mix(old, new) : new
			when Array
				old.is_a?(Array) ? (old + new) : new
			when String
				old.is_a?(String) ? "#{old} #{new}" : new
			else
				new
			end
		end

		result.transform_keys! do |key|
			key.end_with?("!") ? key.name.chop.to_sym : key
		end
	end
end

#tokens(*tokens, **conditional_tokens) ⇒ String (private)

Tokens

Examples:

With Proc conditions

tokens(
	-> { true } => "a",
	-> { false } => "b"
)

With method conditions

tokens(
	active?: "active"
)

With else condition

tokens(
	active?: { then: "active", else: "inactive" }
)

Returns:

  • (String)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/phlex/helpers.rb', line 25

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, "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