Class: Rattler::BackEnd::RubyGenerator
- Inherits:
-
Object
- Object
- Rattler::BackEnd::RubyGenerator
- Defined in:
- lib/rattler/back_end/ruby_generator.rb
Overview
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.
Class Method Summary collapse
-
.code(options = {}) {|RubyGenerator| ... } ⇒ String
Create a new
RubyGeneratorwith the given options, yield it to the block, and return the code generated by the block.
Instance Method Summary collapse
-
#<<(s) ⇒ self
Add arbirtrary code.
-
#block(before, after = 'end') ⇒ self
Generate a multiline indented block with the code generated in the given block, opening the block with
beforeand closing it withafter. -
#code ⇒ String
Return the generated code.
-
#indent ⇒ self
Increase the indent level and start a new line for the given block.
-
#initialize(options = {}) ⇒ RubyGenerator
constructor
Create a new
RubyGeneratorwith the given options. -
#intersperse(enum, opts = {}) {|element| ... } ⇒ self
Add a separator or newlines or both in between code generated in the given block for each element in
enum. -
#intersperse_nl(enum, sep) {|element| ... } ⇒ self
Add
sepfollowed by a newline in between code generated in the given block for each element inenum. -
#newline ⇒ self
Add a line break followed by the appropriate amount of space to indent the start of a line.
-
#start_line ⇒ self
Add the appropriate amount of space to indent the start of a line.
-
#suffix(s) ⇒ self
Append the given string after code generated in the given block.
-
#surround(before, after) ⇒ self
Append
before, followed by the code generated in the given block, followed byafter.
Constructor Details
#initialize(options = {}) ⇒ RubyGenerator
Create a new RubyGenerator with the given options.
40 41 42 43 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 40 def initialize( = {}) @indent_level = [:indent_level] || 0 @io = [:io] || StringIO.new end |
Class Method Details
.code(options = {}) {|RubyGenerator| ... } ⇒ String
Create a new RubyGenerator with the given options, yield it to the block, and return the code generated by the block.
28 29 30 31 32 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 28 def self.code( = {}) generator = self.new() yield generator generator.code end |
Instance Method Details
#<<(s) ⇒ self
Add arbirtrary code.
50 51 52 53 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 50 def <<(s) @io << s self end |
#block(before, after = 'end') ⇒ self
Generate a multiline indented block with the code generated in the given block, opening the block with before and closing it with after.
111 112 113 114 115 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 111 def block(before, after='end') self << before indent { yield } newline << after end |
#code ⇒ String
Return the generated code.
160 161 162 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 160 def code @io.string end |
#indent ⇒ self
Increase the indent level and start a new line for the given block.
75 76 77 78 79 80 81 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 75 def indent @indent_level += 1 newline yield @indent_level -= 1 self end |
#intersperse(enum, opts = {}) {|element| ... } ⇒ self
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.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 133 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) {|element| ... } ⇒ self
Add sep followed by a newline in between code generated in the given block for each element in enum.
153 154 155 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 153 def intersperse_nl(enum, sep) intersperse(enum, :sep => sep, :newline => true) {|_| yield _ } end |
#newline ⇒ self
Add a line break followed by the appropriate amount of space to indent the start of a line.
67 68 69 70 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 67 def newline @io.puts start_line end |
#start_line ⇒ self
Add the appropriate amount of space to indent the start of a line.
58 59 60 61 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 58 def start_line @io << (' ' * @indent_level) self end |
#suffix(s) ⇒ self
Append the given string after code generated in the given block.
86 87 88 89 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 86 def suffix(s) yield self << s end |
#surround(before, after) ⇒ self
Append before, followed by the code generated in the given block, followed by after.
98 99 100 101 |
# File 'lib/rattler/back_end/ruby_generator.rb', line 98 def surround(before, after) self << before suffix(after) { yield } end |