class Rattler::Compiler::RubyGenerator

A RubyGenerator is used to generate well-formatted ruby code. It keeps track of the indent level and has various methods for adding code, all of which return self to allow method chaining.

Public Class Methods

code(options = {}) { |generator| ... } click to toggle source

Create a new RubyGenerator with the given options, yield it to the block, and return the code generated by the block.

@param (see initialize) @option (see initialize) @yield [RubyGenerator] @return [String] the code generated by the block

# File lib/rattler/compiler/ruby_generator.rb, line 19
def self.code(options = {})
  generator = self.new(options)
  yield generator
  generator.code
end
new(options = {}) click to toggle source

Create a new RubyGenerator with the given options.

@option options [Integer] :indent_level (0) the initial indent level @option options [StringIO] :io (StringIO.new) the StringIO object that

the generator will write the generated code to
# File lib/rattler/compiler/ruby_generator.rb, line 31
def initialize(options = {})
  @indent_level = options[:indent_level] || 0
  @io = options[:io] || StringIO.new
end

Public Instance Methods

<<(s) click to toggle source

Add arbirtrary code.

@param [String] s the code to add

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 41
def <<(s)
  @io << s
  self
end
block(before, after='end') { || ... } click to toggle source

Generate a multiline indented block with the code generated in the given block, opening the block with before and closing it with after.

@param [String] before the code to open the block @param [String] after the code to close the block

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 102
def block(before, after='end')
  self << before
  indent { yield }
  newline << after
end
code() click to toggle source

Return the generated code.

@return [String] the generated code

# File lib/rattler/compiler/ruby_generator.rb, line 151
def code
  @io.string
end
indent() { || ... } click to toggle source

Increase the indent level and start a new line for the given block.

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 66
def indent
  @indent_level += 1
  newline
  yield
  @indent_level -= 1
  self
end
intersperse(enum, opts={}) { |_| ... } click to toggle source

Add a separator or newlines or both in between code generated in the given block for each element in enum. Newlines, are always added after the separator.

@param [Enumerable] enum an enumerable sequence of objects

@option opts [String] :sep (nil) optional separator to use between

elements

@option opts [true, false] :newline (false) separate with a single

newline if +true+ (and if :newlines is not specified)

@option opts [Integer] :newlines (nil) optional number of newlines to

use between elements (overrides :newline)

@yield [element] each element in enum

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 124
def intersperse(enum, opts={})
  sep = opts[:sep]
  newlines = opts[:newlines] || (opts[:newline] ? 1 : 0)
  enum.each_with_index do |_, i|
    if i > 0
      self << sep if sep
      newlines.times { newline }
    end
    yield _
  end
  self
end
intersperse_nl(enum, sep) { |_| ... } click to toggle source

Add sep followed by a newline in between code generated in the given block for each element in enum.

@param [Enumerable] enum an enumerable sequence of objects @yield [element] each element in enum

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 144
def intersperse_nl(enum, sep)
  intersperse(enum, :sep => sep, :newline => true) {|_| yield _ }
end
newline() click to toggle source

Add a line break followed by the appropriate amount of space to indent the start of a line.

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 58
def newline
  @io.puts
  start_line
end
start_line() click to toggle source

Add the appropriate amount of space to indent the start of a line.

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 49
def start_line
  @io << ('  ' * @indent_level)
  self
end
suffix(s) { || ... } click to toggle source

Append the given string after code generated in the given block.

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 77
def suffix(s)
  yield
  self << s
end
surround(before, after) { || ... } click to toggle source

Append before, followed by the code generated in the given block, followed by after.

@param [String] before the code to append before the block @param [String] after the code to append after the block

@return [self]

# File lib/rattler/compiler/ruby_generator.rb, line 89
def surround(before, after)
  self << before
  suffix(after) { yield }
end