Class: Middleman::CoreExtensions::ExternalHelpers

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

Overview

Load helpers in helpers/

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

activated_extension, #add_exposed_to_context, #after_build, after_extension_activated, #after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, expose_to_application, expose_to_config, expose_to_template, helpers, #manipulate_resource_list, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

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

Returns a new instance of ExternalHelpers.



5
6
7
8
9
10
11
12
13
14
15
# File '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

#after_configurationObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File '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)

  glob = File.join(helpers_path, app.config[:helpers_filename_glob])
  ::Middleman::Util.glob_directory(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