Class: FlameChannelParser::Builder

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

Overview

A Builder-like class for exporting Flame setups

Constant Summary collapse

INDENT =

< BasicObject

"\t"

Instance Method Summary collapse

Constructor Details

#initialize(io, indent = 0) ⇒ Builder

Returns a new instance of Builder.



5
6
7
# File 'lib/builder.rb', line 5

def initialize(io, indent = 0)
  @io, @indent = io, indent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, arg = nil) ⇒ Object (private)



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/builder.rb', line 57

def method_missing(meth, arg = nil)
  if block_given?
    write_block!(meth, arg) {|c| yield(c) }
  else
    if arg.nil?
      write_loose!(meth)
    else
      write_tuple!(meth, arg)
    end
  end
end

Instance Method Details

#<<(some_verbatim_string) ⇒ Object

Append the text passed to the setup. The appended lines will be prepended by the indent of the current builder



49
50
51
52
53
# File 'lib/builder.rb', line 49

def <<(some_verbatim_string)
  some_verbatim_string.split("\n").each do | line |
    @io.puts(["\t" * @indent, line].join)
  end
end

#color_hash!(name, red, green, blue) ⇒ Object

Write a color hash with the right order of values



39
40
41
42
43
44
45
# File 'lib/builder.rb', line 39

def color_hash!(name, red, green, blue)
  write_unterminated_block!(name) do | b |
    b.red(red)
    b.green(green)
    b.blue(blue)
  end
end

#linebreak!(how_many = 1) ⇒ Object

Write a number of linebreaks



34
35
36
# File 'lib/builder.rb', line 34

def linebreak!(how_many = 1)
  @io.write("\n" * how_many)
end

#write_block!(name, value = nil) {|self.class.new(@io, @indent + 1)| ... } ⇒ Object

Writes a block of values delimited by “End” terminators. Will yield a nested Builder objectg which

Yields:

  • (self.class.new(@io, @indent + 1))


11
12
13
14
15
# File 'lib/builder.rb', line 11

def write_block!(name, value = nil, &blk)
  value.nil? ? write_loose!(name) : write_tuple!(name, value)
  yield(self.class.new(@io, @indent + 1))
  @io.puts(INDENT * (@indent + 1) + "End")
end

#write_loose!(value) ⇒ Object

Write a number of linebreaks



29
30
31
# File 'lib/builder.rb', line 29

def write_loose!(value)
  @io.puts("%s%s" % [INDENT * @indent, __camelize(value)])
end

#write_tuple!(key, value) ⇒ Object

Write a tuple of “Parameter Value”, like “Frame 13”



24
25
26
# File 'lib/builder.rb', line 24

def write_tuple!(key, value)
  @io.puts("%s%s %s" % [INDENT * @indent, __camelize(key), __flameize(value)])
end

#write_unterminated_block!(name, value = nil) {|self.class.new(@io, @indent + 1)| ... } ⇒ Object

Write an unterminated block of values

Yields:

  • (self.class.new(@io, @indent + 1))


18
19
20
21
# File 'lib/builder.rb', line 18

def write_unterminated_block!(name, value = nil, &blk)
  value.nil? ? write_loose!(name) : write_tuple!(name, value)
  yield(self.class.new(@io, @indent + 1))
end