class RgGen::Core::Utility::CodeUtility::CodeBlock

Attributes

indent[R]
lines[R]

Public Class Methods

new(indent = 0) { |self| ... } click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 8
def initialize(indent = 0)
  @indent = indent
  @lines = []
  add_line
  block_given? && yield(self)
end

Public Instance Methods

<<(rhs) click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 17
def <<(rhs)
  case rhs
  when String then push_string(rhs)
  when CodeBlock then push_code_block(rhs)
  when Array then rhs.inject(self, :<<)
  when code? then self << rhs.to_code
  else push_word(rhs)
  end
end
eval_block() { |: yield(self)| ... } click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 40
def eval_block(&block)
  return unless block_given?
  block.arity.zero? ? self << yield : yield(self)
end
indent=(indent) click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 27
def indent=(indent)
  @indent = indent
  last_line.indent = indent
end
last_line_empty?() click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 32
def last_line_empty?
  last_line.empty?
end
to_s() click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 36
def to_s
  @lines.map(&:to_s).each(&:rstrip!).join(newline)
end

Private Instance Methods

add_line() click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 47
def add_line
  line = Line.new(@indent)
  @lines << line
end
code?() click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 79
def code?
  ->(rhs) { rhs.respond_to?(:to_code) }
end
last_line() click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 70
def last_line
  @lines.last
end
newline() click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 83
def newline
  "\n"
end
push_code_block(rhs) click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 61
def push_code_block(rhs)
  rhs.lines.each_with_index do |line, i|
    i.positive? && add_line
    line.empty? || (last_line.indent += line.indent)
    last_line.concat(line)
  end
  self
end
push_string(rhs) click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 52
def push_string(rhs)
  rhs += newline if rhs.end_with?(newline)
  rhs.each_line.with_index do |line, i|
    i.positive? && add_line
    push_word(line.chomp)
  end
  self
end
push_word(word) click to toggle source
# File lib/rggen/core/utility/code_utility/code_block.rb, line 74
def push_word(word)
  last_line << word
  self
end