Class: Autumn::Coder

Inherits:
Object
  • Object
show all
Defined in:
lib/autumn/coder.rb

Overview

Helper class that generates shell Ruby code. This class knows how to generate Ruby code for template classes and methods.

Direct Known Subclasses

TemplateCoder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoder

Creates a new instance with an indent level of 0 and an empty output string.



16
17
18
19
# File 'lib/autumn/coder.rb', line 16

def initialize
  @indent = 0
  @output = String.new
end

Instance Attribute Details

#outputObject (readonly)

The generated code string.



11
12
13
# File 'lib/autumn/coder.rb', line 11

def output
  @output
end

Instance Method Details

#<<(str) ⇒ Object

Adds a line of code to this Generator, sequentially.



105
106
107
108
109
# File 'lib/autumn/coder.rb', line 105

def <<(str)
  str.split(/\n/).each do |line|
    @output << "#{tab}#{line}\n"
  end
end

#doc=(str) ⇒ Object



111
112
113
114
115
# File 'lib/autumn/coder.rb', line 111

def doc=(str)
  doc_lines = str.line_wrap(80 - tab.size - 2).split("\n")
  doc_lines.map! { |str| "#{tab}# #{str}\n" }
  @output = doc_lines.join + "\n" + @output
end

#indent!Object

Increases the indent level for all future lines of code appended to this Generator.



92
93
94
# File 'lib/autumn/coder.rb', line 92

def indent!
  @indent = @indent + 1
end

#klass(name, superclass = nil) ⇒ Object

Creates a new class empty class. This method yields another Generator, which you can populate with the contents of the class, if you wish. Example:

gen.klass("Foo") { |foo| foo.method("bar") }

produces:

class Foo
  def bar
  end
end


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/autumn/coder.rb', line 34

def klass(name, superclass=nil)
  if superclass then
    self << "class #{name} < #{superclass}"
  else
    self << "class #{name}"
  end
  
  if block_given? then
    generator = self.class.new
    yield generator
    indent!
    self << generator.output
    unindent!
  end
  
  self << "end"
  
  return self
end

#method(name, *params) ⇒ Object

Creates a new empty method. Any additional parameters are considered to be the generated method’s parameters. They can be symbols/strings (taken to be the parameter’s name), or hashes associating the parameter’s name to its default value.

This method yields another Generator, which you can populate with the contents of the method, if you wish. Example:

gen.method("test", :required, { :optional => 'default' })

produces:

def test(required, optional="default")
end


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/autumn/coder.rb', line 69

def method(name, *params)
  if params.empty? then
    self << "def #{name}"
  else
    self << "def #{name}(#{parameterize params})"
  end
  
  if block_given? then
    generator = self.class.new
    yield generator
    indent!
    self << generator.output
    unindent!
  end
  
  self << "end"
  
  return self
end

#newline!Object

Appends a blank line to the output.



119
120
121
# File 'lib/autumn/coder.rb', line 119

def newline!
  @output << "\n"
end

#unindent!Object

Decreases the indent level for all future lines of code appended to this Generator.



99
100
101
# File 'lib/autumn/coder.rb', line 99

def unindent!
  @indent = @indent - 1 unless @indent == 0
end