Sha256: e9a76b9d4525cab16f8b1627b4e1f204a10f18cb2e1df117dee911ac54cbc445

Contents?: true

Size: 955 Bytes

Versions: 5

Compression:

Stored size: 955 Bytes

Contents

module FoodTruck

  # Used to generate a [module](https://ruby-doc.org/core-2.6.5/doc/syntax/modules_and_classes_rdoc.html).
  #
  # More accurately, wrap the `body` (first argument) with any following module definitions (additional arguments).
  #
  # @example
  #   FoodTruck.mod("puts('Hello World')", "Level1", "Level2")
  #   #=> module Level1
  #         module Level2
  #           puts('Hello World')
  #         end
  #       end
  #
  # @param body [String] Name of module namespaces.
  # @param names [String,Array<String>] Name of module namespaces.
  # @return [String]
  def self.mod(body, *names)
    names.flatten!
    count = names.length
    return body unless count.positive?()
    level = 0
    head = []
    tail = []
    names.each do |name|
      head.push("module #{name}".indent(level))
      tail.unshift("end".indent(level))
      level += 2
    end
    return (head + [body&.indent(level)] + tail).compact.join("\n")
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
food_truck-0.5.3 lib/food_truck/mod.rb
food_truck-0.5.2 lib/food_truck/mod.rb
food_truck-0.5.1 lib/food_truck/mod.rb
food_truck-0.5.0 lib/food_truck/mod.rb
food_truck-0.4.0 lib/food_truck/mod.rb