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.



111
112
113
114
# File 'lib/panda_pal/helpers/console_helpers.rb', line 111

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

Instance Method Details

#<<(line) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/panda_pal/helpers/console_helpers.rb', line 116

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



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

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

#dedent!Object



149
150
151
# File 'lib/panda_pal/helpers/console_helpers.rb', line 149

def dedent!
  @line_prefix.pop
end

#ensure_lineObject



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

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

#indent!(char = nil) ⇒ Object



145
146
147
# File 'lib/panda_pal/helpers/console_helpers.rb', line 145

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

#to_sObject



153
154
155
# File 'lib/panda_pal/helpers/console_helpers.rb', line 153

def to_s
  @code
end