class Gemsmith::Gem::ModuleFormatter

Formats single or multiple modules with correct, two-space indentation for templates.

Attributes

depth[R]
modules[R]
namespace[R]

Public Class Methods

indent(length = 0) click to toggle source
# File lib/gemsmith/gem/module_formatter.rb, line 11
def self.indent length = 0
  "  " * length
end
new(namespace) click to toggle source
# File lib/gemsmith/gem/module_formatter.rb, line 15
def initialize namespace
  @namespace = namespace
  @modules = namespace.split "::"
  @depth = namespace.scan("::").length
end

Public Instance Methods

render(content) click to toggle source
# File lib/gemsmith/gem/module_formatter.rb, line 21
def render content
  "#{prefix}#{body content}#{suffix.chomp}"
end

Private Instance Methods

body(content) click to toggle source
# File lib/gemsmith/gem/module_formatter.rb, line 35
def body content
  content.lstrip.split("\n").reduce "" do |body, line|
    next "#{body}\n" if line.blank?

    body + "#{self.class.indent depth + 1}#{line.gsub(/^\s{2}/, "")}\n"
  end
end
prefix() click to toggle source
# File lib/gemsmith/gem/module_formatter.rb, line 29
def prefix
  modules.each.with_index.reduce "" do |result, (module_name, index)|
    result + "#{self.class.indent index}module #{module_name}\n"
  end
end
suffix() click to toggle source
# File lib/gemsmith/gem/module_formatter.rb, line 43
def suffix
  modules.each.with_index.reduce "" do |result, (_, index)|
    result + "#{self.class.indent depth - index}end\n"
  end
end