Class: XDry::Emitter
- Inherits:
-
Object
- Object
- XDry::Emitter
- Defined in:
- lib/xdry/patching/emitter.rb
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(line) ⇒ Object
- #block(prefix = '') ⇒ Object
- #empty? ⇒ Boolean
- #if(condition, &block) ⇒ Object
- #indent ⇒ Object
-
#initialize ⇒ Emitter
constructor
A new instance of Emitter.
- #method(decl, &block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Emitter
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
#lines ⇒ Object (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
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
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 |
#indent ⇒ Object
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_s ⇒ Object
40 41 42 |
# File 'lib/xdry/patching/emitter.rb', line 40 def to_s @lines.collect { |line| line + "\n" }.join("") end |