Sha256: f9b0ec24f586d307aeb03ba0eb38da0cffe021104eae44ca84675396511ba7e5

Contents?: true

Size: 1.01 KB

Versions: 24

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

# Monkey patches for +Object+
#
class Object

  # Adds a `render` method to a class for rendering an ERB template to a string.
  #
  # @param dir [String] a directory in which to find the template to be rendered,
  #   populated with a guess from the call stack if not provided.
  def self.renderable(dir: nil)
    dir ||= begin
      this_patch_file = __FILE__
      this_patch_file_caller_index = caller_locations.find_index do |location|
        location.absolute_path == this_patch_file
      end

      location_that_called_renderable = caller_locations[(this_patch_file_caller_index || -1) + 1]
      File.dirname(location_that_called_renderable.path)
    end

    class_eval <<~METHOD, __FILE__, __LINE__ + 1
      def renderable_base_dir
        "#{dir}"
      end
    METHOD

    class_eval do
      def render(file:)
        file = File.absolute_path(file, renderable_base_dir)
        template = File.open(file, 'rb', &:read)
        ERB.new(template).result(binding)
      end
    end
  end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
openstax_kitchen-14.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-13.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-12.2.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-12.1.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-12.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-11.2.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-11.1.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-11.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-10.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-9.2.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-9.1.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-9.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-8.0.1 lib/kitchen/patches/renderable.rb
openstax_kitchen-8.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-7.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-6.1.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-6.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-5.0.0 lib/kitchen/patches/renderable.rb
openstax_kitchen-4.1.1 lib/kitchen/patches/renderable.rb
openstax_kitchen-4.1.0 lib/kitchen/patches/renderable.rb