Class: SXP::Generator

Inherits:
Object show all
Defined in:
lib/sxp/generator.rb

Overview

An S-expression generator.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Generator) initialize(buffer)

A new instance of Generator

Parameters:



34
35
36
37
# File 'lib/sxp/generator.rb', line 34

def initialize(buffer)
  @output = [@buffer = buffer]
  @indent = 0
end

Class Method Details

Parameters:

Returns:



16
17
18
# File 'lib/sxp/generator.rb', line 16

def self.print(*sxps)
  write($stdout, *sxps)
end

+ (Object) string(*sxps)

Parameters:

Returns:



8
9
10
11
# File 'lib/sxp/generator.rb', line 8

def self.string(*sxps)
  require 'stringio' unless defined?(StringIO)
  write(StringIO.new, *sxps).instance_variable_get('@buffer').string
end

+ (Object) write(out, *sxps)

Parameters:

Returns:



24
25
26
27
28
29
30
# File 'lib/sxp/generator.rb', line 24

def self.write(out, *sxps)
  generator = self.new(out)
  sxps.each do |sxp|
    generator.send((op = sxp.shift).to_sym, *sxp)
  end
  generator
end

Instance Method Details

- (String) captured { ... } (protected)

Yields:

Returns:



55
56
57
58
59
60
61
62
63
64
# File 'lib/sxp/generator.rb', line 55

def captured(&block)
  require 'stringio' unless defined?(StringIO)
  begin
    @output.push(buffer = StringIO.new)
    block.call
  ensure
    @output.pop
  end
  buffer.string
end

- decrease_indent! (protected)

This method returns an undefined value.



86
87
88
# File 'lib/sxp/generator.rb', line 86

def decrease_indent!
  @indent -= 1
end

- emit(text, options = {}) (protected)

This method returns an undefined value.

Parameters:



45
46
47
48
49
50
# File 'lib/sxp/generator.rb', line 45

def emit(text, options = {})
  if out = @output.last
    out.print(' ' * (indent * 2)) if options[:indent]
    out.print(text)
  end
end

- increase_indent! (protected)

This method returns an undefined value.



80
81
82
# File 'lib/sxp/generator.rb', line 80

def increase_indent!
  @indent += 1
end

- (Object) indented { ... } (protected)

Yields:

Returns:



69
70
71
72
73
74
75
76
# File 'lib/sxp/generator.rb', line 69

def indented(&block)
  begin
    increase_indent!
    block.call
  ensure
    decrease_indent!
  end
end