Class: PandaPal::ConsoleHelpers::CodeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/panda_pal/helpers/console_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(indent: 0) ⇒ CodeBuilder

Returns a new instance of CodeBuilder.



100
101
102
103
# File 'lib/panda_pal/helpers/console_helpers.rb', line 100

def initialize(indent: 0)
  @code = ""
  @line_prefix = ["  "] * indent
end

Instance Method Details

#<<(line) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/panda_pal/helpers/console_helpers.rb', line 105

def <<(line)
  if line.is_a?(Array)
    line.each do |l|
      self << l
    end
  else
    bits = line.split("\n", -1)

    push_bit(bits.shift)

    bits.each do |bit|
      @code << "\n"
      push_bit(bit)
    end
  end
end

#block(char = nil) ⇒ Object



127
128
129
130
131
132
# File 'lib/panda_pal/helpers/console_helpers.rb', line 127

def block(char = nil)
  indent!(char)
  yield
ensure
  dedent!
end

#dedent!Object



138
139
140
# File 'lib/panda_pal/helpers/console_helpers.rb', line 138

def dedent!
  @line_prefix.pop
end

#ensure_lineObject



122
123
124
125
# File 'lib/panda_pal/helpers/console_helpers.rb', line 122

def ensure_line
  return if @code.end_with?("\n")
  @code << "\n"
end

#indent!(char = nil) ⇒ Object



134
135
136
# File 'lib/panda_pal/helpers/console_helpers.rb', line 134

def indent!(char = nil)
  @line_prefix << (char || "  ")
end

#to_sObject



142
143
144
# File 'lib/panda_pal/helpers/console_helpers.rb', line 142

def to_s
  @code
end