Class: Yalphabetize::ErbCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/yalphabetize/erb_compiler.rb

Defined Under Namespace

Classes: Scanner

Constant Summary collapse

DEFAULT_STAGS =
['<%%', '<%=', '<%#', '<%', '%{', '{{'].freeze
DEFAULT_ETAGS =
['%%>', '%>', '}}', '}'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErbCompiler

Returns a new instance of ErbCompiler.



38
39
40
41
# File 'lib/yalphabetize/erb_compiler.rb', line 38

def initialize
  @buffer = []
  @content = []
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



76
77
78
# File 'lib/yalphabetize/erb_compiler.rb', line 76

def content
  @content
end

Instance Method Details

#compile(string) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yalphabetize/erb_compiler.rb', line 43

def compile(string)
  scanner = Scanner.new(string)
  scanner.scan do |token|
    next if token.nil?
    next if token == ''

    if scanner.stag.nil?
      compile_stag(token, scanner)
    else
      compile_etag(token, scanner)
    end
  end
end

#compile_etag(etag, scanner) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/yalphabetize/erb_compiler.rb', line 65

def compile_etag(etag, scanner)
  buffer << etag

  case etag
  when *DEFAULT_ETAGS
    content << buffer.join
    self.buffer = []
    scanner.stag = nil
  end
end

#compile_stag(stag, scanner) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/yalphabetize/erb_compiler.rb', line 57

def compile_stag(stag, scanner)
  case stag
  when *DEFAULT_STAGS
    scanner.stag = stag
    buffer << stag
  end
end