Module: Phlexi::Field::Common::Tokens

Included in:
Phlexi::Field::Components::Base
Defined in:
lib/phlexi/field/common/tokens.rb

Instance Method Summary collapse

Instance Method Details

#classes(*tokens, **conditional_tokens) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/phlexi/field/common/tokens.rb', line 7

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

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

#tokens(*tokens, **conditional_tokens) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/phlexi/field/common/tokens.rb', line 17

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