Class: XDry::Emitter

Inherits:
Object
  • Object
show all
Defined in:
lib/xdry/patching/emitter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmitter

Returns a new instance of Emitter.



6
7
8
9
10
# File 'lib/xdry/patching/emitter.rb', line 6

def initialize
  @lines = []
  @indent = "\t"
  @current_indent = ""
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



4
5
6
# File 'lib/xdry/patching/emitter.rb', line 4

def lines
  @lines
end

Class Method Details

.capture {|emitter| ... } ⇒ Object

Yields:

  • (emitter)


12
13
14
15
16
# File 'lib/xdry/patching/emitter.rb', line 12

def self.capture
  emitter = Emitter.new
  yield emitter
  return emitter.lines
end

Instance Method Details

#<<(line) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/xdry/patching/emitter.rb', line 18

def << line
  case line
  when Emitter
    @lines.push *line.lines.collect { |l| @current_indent + l }
  when Array
    @lines.push *line.collect { |l| @current_indent + l }
  else
    @lines << @current_indent + line
  end
end

#block(prefix = '') ⇒ Object



44
45
46
47
48
# File 'lib/xdry/patching/emitter.rb', line 44

def block prefix = ''
  self << "#{prefix} {"
  self.indent { yield }
  self << "}"
end

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/xdry/patching/emitter.rb', line 36

def empty?
  @lines.empty?
end

#if(condition, &block) ⇒ Object



55
56
57
# File 'lib/xdry/patching/emitter.rb', line 55

def if condition, &block
  self.block "if (#{condition})", &block
end

#indentObject



29
30
31
32
33
34
# File 'lib/xdry/patching/emitter.rb', line 29

def indent
  prev_indent = @current_indent
  @current_indent = @current_indent + @indent
  yield
  @current_indent = prev_indent
end

#method(decl, &block) ⇒ Object



50
51
52
53
# File 'lib/xdry/patching/emitter.rb', line 50

def method decl, &block
  self << ""
  self.block "- #{decl}", &block
end

#to_sObject



40
41
42
# File 'lib/xdry/patching/emitter.rb', line 40

def to_s
  @lines.collect { |line| line + "\n" }.join("")
end