Sha256: 986ec5a3ec55637393595b73393ed95c2f39eff60c57f2a357ad5ba6d4c78072
Contents?: true
Size: 1.19 KB
Versions: 9
Compression:
Stored size: 1.19 KB
Contents
# frozen_string_literal: true require "refinements/strings" module Gemsmith module Gem # Formats single or multiple modules with correct, two-space indentation for templates. class ModuleFormatter using Refinements::Strings def self.indent length = 0 " " * length end def initialize namespace @namespace = namespace @modules = namespace.split "::" @depth = namespace.scan("::").length end def render content "#{prefix}#{body content}#{suffix.chomp}" end private attr_reader :namespace, :modules, :depth def prefix modules.each.with_index.reduce "" do |result, (module_name, index)| result + "#{self.class.indent index}module #{module_name}\n" end end 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 def suffix modules.each.with_index.reduce "" do |result, (_, index)| result + "#{self.class.indent depth - index}end\n" end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems