Class: Middleman::CoreExtensions::ExternalHelpers

Inherits:
Extension
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/core_extensions/external_helpers.rb

Overview

Load helpers in helpers/

Constant Summary

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary (collapse)

Methods inherited from Extension

activated_extension, #after_build, #after_extension_activated, after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, helpers, option

Methods included from Contracts

#Contract

Constructor Details

- (ExternalHelpers) initialize(app, options_hash = {}, &block)

Returns a new instance of ExternalHelpers



5
6
7
8
9
10
11
12
13
14
15
# File 'middleman-core/lib/middleman-core/core_extensions/external_helpers.rb', line 5

def initialize(app, options_hash={}, &block)
  super

  # Setup a default helpers paths
  app.config.define_setting :helpers_dir, 'helpers', 'Directory to autoload helper modules from'
  app.config.define_setting :helpers_filename_glob, '**.rb', 'Glob pattern for matching helper ruby files'
  app.config.define_setting :helpers_filename_to_module_name_proc, proc { |filename|
    basename = File.basename(filename, File.extname(filename))
    basename.camelcase
  }, 'Proc implementing the conversion from helper filename to module name'
end

Instance Method Details

- (Object) after_configuration



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'middleman-core/lib/middleman-core/core_extensions/external_helpers.rb', line 17

def after_configuration
  helpers_path = File.join(app.root, app.config[:helpers_dir])

  return unless File.exist?(helpers_path)

  Dir[File.join(helpers_path, app.config[:helpers_filename_glob])].each do |filename|
    module_name = app.config[:helpers_filename_to_module_name_proc].call(filename)
    next unless module_name

    require filename
    next unless Object.const_defined?(module_name.to_sym)

    app.template_context_class.send :include, Object.const_get(module_name.to_sym)
  end
end