Module: Middleman::CoreExtensions::Extensions::InstanceMethods

Defined in:
lib/middleman-core/core_extensions/extensions.rb

Overview

Instance methods

Instance Method Summary collapse

Instance Method Details

#activate(ext, options = {}, &block) ⇒ void

This method returns an undefined value.

This method is available in the project’s ‘config.rb`. It takes a underscore-separated symbol, finds the appropriate feature module and includes it.

activate :lorem

Parameters:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/middleman-core/core_extensions/extensions.rb', line 99

def activate(ext, options={}, &block)
  ext_module = if ext.is_a?(Module)
    ext
  else
    ::Middleman::Extensions.load(ext)
  end

  if ext_module.nil?
    logger.error "== Unknown Extension: #{ext}"
  else
    logger.debug "== Activating: #{ext}"

    if ext_module.instance_of? Module
      extensions[ext] = self.class.register(ext_module, options, &block)
    elsif ext_module.instance_of?(Class) && ext_module.ancestors.include?(::Middleman::Extension)
      if ext_module.supports_multiple_instances?
        extensions[ext] ||= {}
        key = "instance_#{extensions[ext].keys.length}"
        extensions[ext][key] = ext_module.new(self.class, options, &block)
      else
        if extensions[ext]
          logger.error "== #{ext} already activated."
        else
          extensions[ext] = ext_module.new(self.class, options, &block)
        end
      end
    end
  end
end

#extensionsHash<Symbol,Middleman::Extension|Module>

Access activated extensions



132
133
134
# File 'lib/middleman-core/core_extensions/extensions.rb', line 132

def extensions
  @extensions ||= {}
end

#initializeObject

Load features before starting server



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/middleman-core/core_extensions/extensions.rb', line 137

def initialize
  super

  self.class.inst = self

  # Search the root of the project for required files
  $LOAD_PATH.unshift(root)

  ::Middleman::Extension.clear_after_extension_callbacks
  run_hook :initialized

  if config[:autoload_sprockets]
    begin
      require "middleman-sprockets"
      activate(:sprockets)
    rescue LoadError
    end
  end

  run_hook :before_configuration

  # Check for and evaluate local configuration
  local_config = File.join(root, "config.rb")
  if File.exists? local_config
    logger.debug "== Reading:  Local config"
    instance_eval File.read(local_config), local_config, 1
  end

  run_hook :build_config if build?
  run_hook :development_config if development?

  run_hook :instance_available

  # This is for making the tests work - since the tests
  # don't completely reload middleman, I18n.load_path can get
  # polluted with paths from other test app directories that don't
  # exist anymore.
  if ENV["TEST"]
    ::I18n.load_path.delete_if {|path| path =~ %r{tmp/aruba}}
    ::I18n.reload!
  end

  run_hook :after_configuration

  logger.debug "Loaded extensions:"
  self.extensions.each do |ext, klass|
    if ext.is_a?(Hash)
      ext.each do |k,_|
        logger.debug "== Extension: #{k}"
      end
    else
      logger.debug "== Extension: #{ext}"
    end

    if klass.is_a?(::Middleman::Extension)
      ::Middleman::Extension.activated_extension(klass)
    end
  end
end