class Rubysmith::Renderers::Namespace

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

Public Class Methods

new(namespace) click to toggle source
# File lib/rubysmith/renderers/namespace.rb, line 11
def initialize namespace
  @namespace = namespace
  @modules = namespace.split "::"
  @depth = namespace.scan("::").length
end

Public Instance Methods

body(content) click to toggle source

:reek: FeatureEnvy

# File lib/rubysmith/renderers/namespace.rb, line 30
def body content
  String(content).lstrip.split("\n").reduce "" do |snippet, line|
    next "#{snippet}\n" if line.blank?

    "#{snippet}#{line.gsub(/^\s{2}/, "").indent depth + 1}\n"
  end
end
call(content = nil) click to toggle source
# File lib/rubysmith/renderers/namespace.rb, line 17
  def call(content = nil) = "#{prefix}#{body content}#{suffix}"

  private

  attr_reader :namespace, :modules, :depth

  def prefix
    modules.each.with_index.reduce "" do |snippet, (module_name, index)|
      %(#{snippet}#{"module".indent index} #{module_name}\n)
    end
  end

  # :reek:FeatureEnvy
  def body content
    String(content).lstrip.split("\n").reduce "" do |snippet, line|
      next "#{snippet}\n" if line.blank?

      "#{snippet}#{line.gsub(/^\s{2}/, "").indent depth + 1}\n"
    end
  end

  def suffix
    modules.each.with_index.reduce "" do |snippet, (_, index)|
      %(#{snippet}#{"end".indent depth - index}\n)
    end
  end
end
prefix() click to toggle source
# File lib/rubysmith/renderers/namespace.rb, line 23
def prefix
  modules.each.with_index.reduce "" do |snippet, (module_name, index)|
    %(#{snippet}#{"module".indent index} #{module_name}\n)
  end
end
suffix() click to toggle source
# File lib/rubysmith/renderers/namespace.rb, line 38
def suffix
  modules.each.with_index.reduce "" do |snippet, (_, index)|
    %(#{snippet}#{"end".indent depth - index}\n)
  end
end