Sha256: 3b43a10d533cb31dcc4b4450f4380c231418162aa01b23467df5d4084bb00a32

Contents?: true

Size: 1.15 KB

Versions: 37

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require "refinements/strings"

module Rubysmith
  module Renderers
    # Renders single or multiple modules with correct, two-space indentation for templates.
    class Namespace
      using Refinements::Strings

      def initialize namespace, delimiter: "::"
        @namespace = namespace
        @modules = namespace.split delimiter
        @depth = namespace.scan(delimiter).length
      end

      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
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
rubysmith-5.9.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.8.1 lib/rubysmith/renderers/namespace.rb
rubysmith-5.8.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.7.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.6.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.5.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.4.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.3.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.2.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.1.0 lib/rubysmith/renderers/namespace.rb
rubysmith-5.0.1 lib/rubysmith/renderers/namespace.rb
rubysmith-4.9.0 lib/rubysmith/renderers/namespace.rb
rubysmith-4.8.0 lib/rubysmith/renderers/namespace.rb
rubysmith-4.7.0 lib/rubysmith/renderers/namespace.rb
rubysmith-4.6.1 lib/rubysmith/renderers/namespace.rb
rubysmith-4.6.0 lib/rubysmith/renderers/namespace.rb
rubysmith-4.5.0 lib/rubysmith/renderers/namespace.rb
rubysmith-4.4.0 lib/rubysmith/renderers/namespace.rb
rubysmith-4.3.0 lib/rubysmith/renderers/namespace.rb
rubysmith-4.2.0 lib/rubysmith/renderers/namespace.rb