Sha256: 07f4bebce1f6fd0a63b6a2af72e6c3da8fd8e84145e0ce2c5faaf9e10192ff0d

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")
	using Overrides::Symbol::Name
end

module Phlex::Helpers
	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(" ")
	end

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

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

	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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phlex-0.5.3 lib/phlex/helpers.rb
phlex-0.5.2 lib/phlex/helpers.rb
phlex-0.5.1 lib/phlex/helpers.rb
phlex-0.5.0 lib/phlex/helpers.rb