Class: ORB::Temple::Generators::Generator

Inherits:
Object
  • Object
show all
Includes:
Mixins::CompiledDispatcher, Mixins::Options, Utils
Defined in:
lib/orb/temple/generators.rb

Instance Method Summary collapse

Instance Method Details

#call(exp) ⇒ Object



22
23
24
# File 'lib/orb/temple/generators.rb', line 22

def call(exp)
  [preamble, compile(exp), postamble].flatten.compact.join('; ')
end

#create_bufferObject



42
43
44
45
46
47
48
# File 'lib/orb/temple/generators.rb', line 42

def create_buffer
  if buffer == '@output_buffer'
    "#{buffer} = output_buffer || #{options[:buffer_class]}.new"
  else
    "#{buffer} = #{options[:buffer_class]}.new"
  end
end

#on(*exp) ⇒ Object

Raises:

  • (InvalidExpression)


54
55
56
# File 'lib/orb/temple/generators.rb', line 54

def on(*exp)
  raise InvalidExpression, "Generator supports only core expressions - found #{exp.inspect}"
end

#on_capture(name, exp) ⇒ Object



66
67
68
69
70
# File 'lib/orb/temple/generators.rb', line 66

def on_capture(name, exp)
  capture_generator.new(capture_generator: options[:capture_generator],
    freeze_static: options[:freeze_static],
    buffer: name).call(exp)
end

#on_code(code) ⇒ Object



80
81
82
# File 'lib/orb/temple/generators.rb', line 80

def on_code(code)
  code
end

#on_dynamic(code) ⇒ Object



76
77
78
# File 'lib/orb/temple/generators.rb', line 76

def on_dynamic(code)
  concat(code)
end

#on_multi(*exp) ⇒ Object



58
59
60
# File 'lib/orb/temple/generators.rb', line 58

def on_multi(*exp)
  exp.map { |e| compile(e) }.join('; ')
end

#on_newlineObject



62
63
64
# File 'lib/orb/temple/generators.rb', line 62

def on_newline
  "\n"
end

#on_static(text) ⇒ Object



72
73
74
# File 'lib/orb/temple/generators.rb', line 72

def on_static(text)
  concat(options[:freeze_static] ? "#{text.inspect}.freeze" : text.inspect)
end

#postambleObject



30
31
32
# File 'lib/orb/temple/generators.rb', line 30

def postamble
  [return_buffer, restore_buffer]
end

#preambleObject



26
27
28
# File 'lib/orb/temple/generators.rb', line 26

def preamble
  [save_buffer, create_buffer]
end

#restore_bufferObject



38
39
40
# File 'lib/orb/temple/generators.rb', line 38

def restore_buffer
  "ensure; #{buffer} = #{@original_buffer}; end" if options[:save_buffer]
end

#return_bufferObject



50
51
52
# File 'lib/orb/temple/generators.rb', line 50

def return_buffer
  'nil'
end

#save_bufferObject



34
35
36
# File 'lib/orb/temple/generators.rb', line 34

def save_buffer
  "begin; #{@original_buffer = unique_name} = #{buffer} if defined?(#{buffer})" if options[:save_buffer]
end