class Tipi::Configuration::Assembler

Public Class Methods

from_source(code) click to toggle source
# File lib/tipi/config_dsl.rb, line 30
def self.from_source(code)
  new.from_source code
end

Public Instance Methods

add_app_proc(proc) click to toggle source
# File lib/tipi/config_dsl.rb, line 91
def add_app_proc(proc)
  id = :"proc#{@app_procs.size}"
  @app_procs[id] = proc
  id
end
add_frame() { || ... } click to toggle source
# File lib/tipi/config_dsl.rb, line 55
def add_frame(&block)
  @stack.push new_frame
  yield
ensure
  frame = @stack.pop
  emit assemble(frame)
end
assemble_app_proc(frame) click to toggle source
# File lib/tipi/config_dsl.rb, line 115
def assemble_app_proc(frame)
  indent = 0
  lines = []
  emit_code lines, frame[:prelude], indent
  emit_code lines, 'proc do |req|', indent
  emit_code lines, frame[:body], indent + 1
  emit_code lines, 'end', indent

  lines
end
assemble_frame(frame) click to toggle source
# File lib/tipi/config_dsl.rb, line 97
def assemble_frame(frame)
  indent = 0
  lines = []
  emit_code lines, frame[:prelude], indent
  if frame[:rescue_proc_id]
    emit_code lines, 'begin', indent
    indent += 1
  end
  emit_code lines, frame[:body], indent
  if frame[:rescue_proc_id]
    emit_code lines, 'rescue => e', indent
    emit_code lines, "  app_procs[#{frame[:rescue_proc_id].inspect}].call(req, e)", indent
    emit_code lines, 'end', indent
    indent -= 1
  end
  lines
end
emit(code) click to toggle source
# File lib/tipi/config_dsl.rb, line 71
def emit(code)
  @stack.last[:body] << code
end
emit_block(conditional, &block) click to toggle source
# File lib/tipi/config_dsl.rb, line 84
def emit_block(conditional, &block)
  proc_id = add_app_proc block
  @stack.last[:branched] = true
  emit conditional
  add_frame &block
end
emit_code(lines, code, indent) click to toggle source
# File lib/tipi/config_dsl.rb, line 126
def emit_code(lines, code, indent)
  if code.is_a? Array
    code.each { |l| emit_code lines, l, indent + 1 }
  else
    lines << (indent_line code, indent)
  end
end
emit_exception_handler(&block) click to toggle source
# File lib/tipi/config_dsl.rb, line 79
def emit_exception_handler(&block)
  proc_id = add_app_proc block
  @stack.last[:rescue_proc_id] = proc_id
end
emit_prelude(code) click to toggle source
# File lib/tipi/config_dsl.rb, line 75
def emit_prelude(code)
  @stack.last[:prelude] << code
end
from_source(code) click to toggle source
# File lib/tipi/config_dsl.rb, line 34
def from_source(code)
  @stack = [new_frame]
  @app_procs = {}
  @interpreter = Interpreter.new self
  @interpreter.instance_eval code
  
  loop do
    frame = @stack.pop
    return assemble_app_proc(frame).join("\n") if @stack.empty?

    @stack.last[:body] << assemble_frame(frame)
  end
end
indent_line(code, indent) click to toggle source
# File lib/tipi/config_dsl.rb, line 136
def indent_line(code, indent)
  indent == 0 ? code : "#{ @@indents[indent] }#{code}"
end
new_frame() click to toggle source
# File lib/tipi/config_dsl.rb, line 48
def new_frame
  {
    prelude: [],
    body: []
  }
end
wrap_current_frame(head) click to toggle source
# File lib/tipi/config_dsl.rb, line 63
def wrap_current_frame(head)
  frame = @stack.pop
  wrapper = new_frame
  wrapper[:body] << head
  @stack.push wrapper
  @stack.push frame
end