Class: Middleman::ExtensionManager

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/middleman-core/extension_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ExtensionManager

Returns a new instance of ExtensionManager.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/middleman-core/extension_manager.rb', line 8

def initialize(app)
  @app = app
  @activated = {}

  ::Middleman::Extensions.load_settings(@app)

  manager = self

  {
    before_sitemap: :before_sitemap,
    initialized: :before_configuration
  }.each do |key, value|
    cb = proc { manager.auto_activate(value) }
    @app.send(key, &cb)
  end

  @app.after_configuration_eval(&method(:activate_all))
end

Instance Method Details

#activate(ext_name, options = {}) {|Middleman::Configuration::ConfigurationManager| ... }

This method returns an undefined value.

Activate an extension, optionally passing in options. This method is typically used from a project's config.rb.

Examples:

Activate an extension with no options

activate :lorem

Activate an extension, with options

activate :minify_javascript, inline: true

Use a block to configure extension options

activate :minify_javascript do |opts|
  opts.ignore += ['*-test.js']
end

Parameters:

  • ext_name (Symbol)

    The name of thed extension to activate

  • options (Hash) (defaults to: {})

    Options to pass to the extension

Yields:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/middleman-core/extension_manager.rb', line 49

def activate(ext_name, options={}, &block)
  begin
    extension = ::Middleman::Extensions.load(ext_name)
  rescue LoadError => e
    logger.debug "== Failed Activation `#{ext_name}` : #{e.message}"
    return
  end

  logger.debug "== Activating: #{ext_name}"

  if extension.supports_multiple_instances?
    @activated[ext_name] ||= {}
    key = "instance_#{@activated[ext_name].keys.length}"
    @activated[ext_name][key] = extension.new(@app, options, &block)
  elsif @activated.key?(ext_name)
    raise "#{ext_name} has already been activated and cannot be re-activated."
  else
    @activated[ext_name] = extension.new(@app, options, &block)
  end
end

#activate_allObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/middleman-core/extension_manager.rb', line 70

def activate_all
  logger.debug 'Loaded extensions:'
  @instances = @activated.each_with_object([]) do |(ext_name, ext), sum|
    if ext.is_a?(Hash)
      ext.each do |instance_key, instance|
        logger.debug "== Extension: #{ext_name} #{instance_key}"
        sum << instance
      end
    else
      logger.debug "== Extension: #{ext_name}"
      sum << ext
    end
  end

  @instances.each do |ext|
    ::Middleman::Extension.activated_extension(ext)
  end
end

#add_exposed_to_context(context) ⇒ Object



89
90
91
92
93
# File 'lib/middleman-core/extension_manager.rb', line 89

def add_exposed_to_context(context)
  @instances.each do |ext|
    ext.add_exposed_to_context(context)
  end
end

#auto_activate(key) ⇒ Object



27
28
29
# File 'lib/middleman-core/extension_manager.rb', line 27

def auto_activate(key)
  ::Middleman::Extensions.auto_activate(key, @app)
end