module Protobuf::Generators::Printable

Constants

PARENT_CLASS_ENUM
PARENT_CLASS_MESSAGE
PARENT_CLASS_SERVICE

Attributes

current_indent[RW]

Public Instance Methods

init_printer(indent_level) click to toggle source

Initialize the printer. Must be called by any class/module that includes the Printable module.

# File lib/protobuf/generators/printable.rb, line 12
def init_printer(indent_level)
  @io = ::StringIO.new
  self.current_indent = indent_level.to_i
end

Private Instance Methods

comment(message) click to toggle source

Print a one-line comment.

# File lib/protobuf/generators/printable.rb, line 25
def comment(message)
  puts "# #{message}"
end
header(message) click to toggle source

Print a “header” comment.

header("Lorem ipsum dolor")
##
# Lorem ipsum dolor
#
# File lib/protobuf/generators/printable.rb, line 35
def header(message)
  puts
  puts "##"
  puts "# #{message}"
  puts "#"
end
indent() { || ... } click to toggle source

Increase the indent level. An outdent will only occur if given a block (after the block is finished).

# File lib/protobuf/generators/printable.rb, line 45
def indent
  self.current_indent += 1
  yield
  outdent
end
modulize(name) click to toggle source

Take a string and upcase the first character of each namespace. Due to the nature of varying standards about how class/modules are named (e.g. CamelCase, Underscore_Case, SCREAMING_SNAKE_CASE), we only want to capitalize the first character to ensure ruby will treat the value as a constant. Otherwise we do not attempt to change the token's definition.

modulize("foo.bar.Baz") -> "::Foo::Bar::Baz"
modulize("foo.bar.baz") -> "::Foo::Bar::Baz"
modulize("foo.bar.BAZ") -> "::Foo::Bar::BAZ"
# File lib/protobuf/generators/printable.rb, line 62
def modulize(name)
  name = name.gsub(/\./, '::')
  name = name.gsub(/(^(?:::)?[a-z]|::[a-z])/, &:upcase)
  name
end
outdent() click to toggle source

Decrease the indent level. Cannot be negative.

# File lib/protobuf/generators/printable.rb, line 70
def outdent
  self.current_indent -= 1 unless current_indent.zero?
end
parent_class(type) click to toggle source

Return the parent class for a given type. Valid types are :message, :enum, and :service, otherwise an error will be thrown.

# File lib/protobuf/generators/printable.rb, line 78
def parent_class(type)
  case type
  when :message then
    PARENT_CLASS_MESSAGE
  when :enum then
    PARENT_CLASS_ENUM
  when :service then
    PARENT_CLASS_SERVICE
  else
    fail "Unknown parent class type #{type}: #{caller[0..5].join("\n")}"
  end
end
print(contents) click to toggle source

Print the given message raw, no indent.

print_block(name, parent_klass, type) { || ... } click to toggle source

Print a class or module block, indicated by type. If a class, can be given a parent class to inherit from. If a block is given, call the block from within an indent block. Otherwise, end the block on the same line.

print_class(name, parent_klass, &block) click to toggle source

Use print_block to print a class, with optional parent class to inherit from. Accepts a block for use with print_block.

print_contents() click to toggle source

Returns the contents of the underlying StringIO object.

print_module(name, &block) click to toggle source

Use print_block to print a module. Accepts a block for use with print_block.

print_require(file) click to toggle source

Print a file require.

print_require('foo/bar/baz') -> "require 'foo/bar/baz'"
puts(message = nil) click to toggle source

Puts the given message prefixed by the indent level. If no message is given print a newline.

# File lib/protobuf/generators/printable.rb, line 137
def puts(message = nil)
  if message
    @io.puts(("  " * current_indent) + message)
  else
    @io.puts
  end
end