class Object

Monkey patches for Object

Public Class Methods

renderable(dir: nil) click to toggle source

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.
# File lib/kitchen/patches/renderable.rb, line 11
  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

Public Instance Methods

file_glob(relative_folder_and_extension) click to toggle source
# File lib/openstax_kitchen.rb, line 13
def file_glob(relative_folder_and_extension)
  Dir[File.expand_path("#{__dir__}/#{relative_folder_and_extension}")]
end
render(file:) click to toggle source
# File lib/kitchen/patches/renderable.rb, line 29
def render(file:)
  file = File.absolute_path(file, renderable_base_dir)
  template = File.open(file, 'rb', &:read)
  ERB.new(template).result(binding)
end
require_all(relative_folder, file_matcher='**/*.rb') click to toggle source
# File lib/openstax_kitchen.rb, line 17
def require_all(relative_folder, file_matcher='**/*.rb')
  file_glob(relative_folder + "/#{file_matcher}").each { |f| require f }
end