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