Class: Sitepress::HelperLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/sitepress/helper_loader.rb

Overview

Loads modules into an isolated namespace that will be used for the rendering context. This loader is designed to be immutable so that it throws away the constants and modules on each load.

TODO: Rename this to a RenderingContext, or something.

Instance Method Summary collapse

Constructor Details

#initialize(paths:) ⇒ HelperLoader

Returns a new instance of HelperLoader.



9
10
11
# File 'lib/sitepress/helper_loader.rb', line 9

def initialize(paths:)
  @paths = Array(paths)
end

Instance Method Details

#context(locals: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sitepress/helper_loader.rb', line 13

def context(locals: {})
  modules = helpers
  Object.new.tap do |object|
    # Locals of rendering context that are accessible from the
    # helper modules.
    locals.each do |name, value|
      object.define_singleton_method(name) { value }
    end
    # Include the helper modules of the rendering context.
    modules.constants.each do |module_name|
      object.send(:extend, modules.const_get(module_name))
    end
  end
end