Class: Curlybars::Processor::Tilde

Inherits:
Object
  • Object
show all
Extended by:
TokenFactory
Defined in:
lib/curlybars/processor/tilde.rb

Class Method Summary collapse

Methods included from TokenFactory

create_token

Class Method Details

.process!(tokens, identifier) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/curlybars/processor/tilde.rb', line 7

def process!(tokens, identifier)
  tokens.each_with_index do |token, index|
    case token.type
    when :TILDE_START
      tokens[index] = create_token(:START, token.value, token.position)
      next if index == 0

      strip_token_if_text(tokens, index - 1, :rstrip)
    when :TILDE_END
      tokens[index] = create_token(:END, token.value, token.position)
      next if index == (tokens.length - 1)

      strip_token_if_text(tokens, index + 1, :lstrip)
    end
  end
end

.strip_token_if_text(tokens, index, strip_method) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/curlybars/processor/tilde.rb', line 24

def strip_token_if_text(tokens, index, strip_method)
  token = tokens[index]
  return if token.type != :TEXT

  stripped_value = token.value.public_send(strip_method)
  tokens[index] = create_token(token.type, stripped_value, token.position)
end