Skip to content

Commit

Permalink
generator cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Oct 30, 2010
1 parent a47b0f8 commit 0263ef9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
40 changes: 18 additions & 22 deletions lib/temple/generators.rb
Expand Up @@ -80,11 +80,6 @@ def initialize(options = {})
@compiling = false
end

def capture_generator
@capture_generator ||=
@options[:capture_generator] || Temple::Generators::StringBuffer
end

def compile(exp)
if @compiling
type, *args = exp
Expand All @@ -99,29 +94,31 @@ def compile(exp)
end
end

def buffer(str = '')
@options[:buffer] + str
def on_multi(*exp)
exp.map { |e| compile(e) }.join(' ; ')
end

def concat(str)
buffer " << (#{str})"
def on_newline
"\n"
end

# Sensible defaults
def on_capture(name, block)
capture_generator.new(:buffer => name).compile(block)
end

def preamble; '' end
def postamble; '' end
protected

def on_multi(*exp)
exp.map { |e| compile(e) }.join(" ; ")
def capture_generator
@capture_generator ||=
@options[:capture_generator] || Temple::Generators::StringBuffer
end

def on_newline
"\n"
def buffer
@options[:buffer]
end

def on_capture(name, block)
capture_generator.new(:buffer => name).compile(block)
def concat(str)
"#{buffer} << (#{str})"
end
end

Expand All @@ -135,8 +132,8 @@ def on_capture(name, block)
# end
# _buf.join
class ArrayBuffer < Generator
def preamble; buffer " = []" end
def postamble; "#{buffer} = #{buffer}.join" end
def preamble; "#{buffer} = []" end
def postamble; "#{buffer} = #{buffer}.join" end

def on_static(text)
concat(text.inspect)
Expand Down Expand Up @@ -166,12 +163,11 @@ def postamble; buffer; end
# end
# _buf
class StringBuffer < Array
def preamble; buffer " = ''" end
def preamble; "#{buffer} = ''" end

def on_dynamic(code)
concat(code) + '.to_s'
end
end
end
end

2 changes: 1 addition & 1 deletion test/test_generator.rb
Expand Up @@ -3,7 +3,7 @@
class TestTempleGenerator < Test::Unit::TestCase
class Simple < Temple::Generators::Generator
def preamble
buffer " = BUFFER"
"#{buffer} = BUFFER"
end

def postamble
Expand Down

0 comments on commit 0263ef9

Please sign in to comment.