module Protobuf::Generators::Printable
Constants
- PARENT_CLASS_ENUM
- PARENT_CLASS_MESSAGE
- PARENT_CLASS_SERVICE
Attributes
Public Instance Methods
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
Print a one-line comment.
# File lib/protobuf/generators/printable.rb, line 25 def comment(message) puts "# #{message}" end
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
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
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
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
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 the given message raw, no indent.
# File lib/protobuf/generators/printable.rb, line 147 def print(contents) @io.print(contents) end
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.
# File lib/protobuf/generators/printable.rb, line 96 def print_block(name, parent_klass, type) name = modulize(name) block_def = "#{type} #{name}" block_def += " < #{parent_class(parent_klass)}" if parent_klass if block_given? puts block_def indent { yield } puts "end" puts else block_def += "; end" puts block_def end end
Use #print_block to print a class, with optional parent class to inherit from. Accepts a block for use with print_block.
# File lib/protobuf/generators/printable.rb, line 115 def print_class(name, parent_klass, &block) print_block(name, parent_klass, :class, &block) end
Returns the contents of the underlying StringIO object.
# File lib/protobuf/generators/printable.rb, line 153 def print_contents @io.rewind @io.read end
Use #print_block to print a module. Accepts a block for use with print_block.
# File lib/protobuf/generators/printable.rb, line 122 def print_module(name, &block) print_block(name, nil, :module, &block) end
Print a file require.
print_require('foo/bar/baz') -> "require 'foo/bar/baz'"
# File lib/protobuf/generators/printable.rb, line 130 def print_require(file, relative = false) puts "require#{'_relative' if relative} '#{file}'" end
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