class Middleman::Extension
@see middlemanapp.com/advanced/custom/ Middleman Custom Extensions Documentation
def activated_extension(instance)
-
(void)
-
Parameters:
-
instance
(Middleman::Extension
) -- Activated extension instance
Other tags:
- Api: - private
def activated_extension(instance) name = instance.class.ext_name return unless @_extension_activation_callbacks && @_extension_activation_callbacks.key?(name) @_extension_activation_callbacks[name].each do |block| block.arity == 1 ? block.call(instance) : block.call end end
def add_exposed_to_context(context)
def add_exposed_to_context(context) (self.class.exposed_to_template || {}).each do |k, v| context.define_singleton_method(k, &method(v)) end end
def after_extension_activated(name, &block)
-
(void)
-
Parameters:
-
block
(Proc
) -- A callback to run when the named extension is activated -
name
(Symbol
) -- The name the extension was registered under
def after_extension_activated(name, &block) @_extension_activation_callbacks ||= {} @_extension_activation_callbacks[name] ||= [] @_extension_activation_callbacks[name] << block if block_given? end
def bind_after_build
def bind_after_build ext = self return unless ext.respond_to?(:after_build) @app.after_build do |builder| if ext.method(:after_build).arity == 1 ext.after_build(builder) elsif ext.method(:after_build).arity == 2 ext.after_build(builder, builder.thor) else ext.after_build end end end
def bind_after_configuration
def bind_after_configuration ext = self @app.after_configuration do ext.after_configuration if ext.respond_to?(:after_configuration) if ext.respond_to?(:manipulate_resource_list) ext.app.sitemap.register_resource_list_manipulators(ext.class.ext_name, ext, ext.class.resource_list_manipulator_priority) end if ext.class.resources_generators && !ext.class.resources_generators.empty? ext.app.sitemap.register_resource_list_manipulators( :"#{ext.class.ext_name}_generator", ext, ext.class.resource_list_manipulator_priority, :generate_resources ) end end end
def bind_before_build
def bind_before_build ext = self return unless ext.respond_to?(:before_build) @app.before_build do |builder| if ext.method(:before_build).arity == 1 ext.before_build(builder) else ext.before_build end end end
def bind_before_configuration
def bind_before_configuration @app.before_configuration(&method(:before_configuration)) if respond_to?(:before_configuration) end
def bind_ready
def bind_ready @app.ready(&method(:ready)) if respond_to?(:ready) end
def clear_after_extension_callbacks
-
(void)
-
Other tags:
- Api: - private
def clear_after_extension_callbacks @_extension_activation_callbacks = {} end
def config
-
(Middleman::Configuration::ConfigurationManager)
- The defined options for this extension.
Other tags:
- Api: - private
def config @_config ||= ::Middleman::Configuration::ConfigurationManager.new end
def define_setting(key, default=nil, description=nil, options={})
-
description
(String
) -- A human-readable description of what the option does -
default
(Object
) -- The default value for the option -
key
(Symbol
) -- The name of the option
Other tags:
- See: Middleman::Configuration::ConfigurationManager#define_setting -
def define_setting(key, default=nil, description=nil, options={}) global_config.define_setting(key, default, description, options) end
def expose_methods
def expose_methods (self.class.exposed_to_application || {}).each do |k, v| app.define_singleton_method(k, &method(v)) end (self.class.exposed_to_config || {}).each do |k, v| app.config_context.define_singleton_method(k, &method(v)) end (self.class.defined_helpers || []).each do |m| app.template_context_class.send(:include, m) end end
def expose_to_application(*symbols)
-
(void)
-
Parameters:
-
symbols
(Array
) -- An optional list of symbols representing instance methods to exposed., Hash
Other tags:
- Example: with Array: -
Example: with Hash: -
def expose_to_application(*symbols) self.exposed_to_application ||= {} if symbols.first && symbols.first.is_a?(Hash) self.exposed_to_application.merge!(symbols.first) elsif symbols.is_a? Array symbols.each do |sym| self.exposed_to_application[sym] = sym end end end
def expose_to_config(*symbols)
-
(void)
-
Parameters:
-
symbols
(Array
) -- An optional list of symbols representing instance methods to exposed., Hash
Other tags:
- Example: with Array: -
Example: with Hash: -
def expose_to_config(*symbols) self.exposed_to_config ||= {} if symbols.first && symbols.first.is_a?(Hash) self.exposed_to_config.merge!(symbols.first) elsif symbols.is_a? Array symbols.each do |sym| self.exposed_to_config[sym] = sym end end end
def expose_to_template(*symbols)
-
(void)
-
Parameters:
-
symbols
(Array
) -- An optional list of symbols representing instance methods to exposed., Hash
Other tags:
- Example: with Array: -
Example: with Hash: -
def expose_to_template(*symbols) self.exposed_to_template ||= {} if symbols.first && symbols.first.is_a?(Hash) self.exposed_to_template.merge!(symbols.first) elsif symbols.is_a? Array symbols.each do |sym| self.exposed_to_template[sym] = sym end end end
def generate_resources(resources)
def generate_resources(resources) generator_defs = self.class.resources_generators.reduce({}) do |sum, g| resource_definitions = if g.is_a? Hash g elsif g.is_a? Symbol definition = method(g) if definition.arity == 0 send(g) else send(g, resources) end else {} end sum.merge!(resource_definitions) end resources + generator_defs.map do |path, g| if g.is_a? Symbol definition = method(g) g = if definition.arity == 0 send(g) else send(g, resources) end end ::Middleman::Sitemap::StringResource.new( app.sitemap, path, g ) end end
def global_config
-
(Middleman::Configuration::ConfigurationManager)
- The defined global options for this extension.
Other tags:
- Api: - private
def global_config @_global_config ||= ::Middleman::Configuration::ConfigurationManager.new end
def helpers(*modules, &block)
-
(void)
-
Parameters:
-
block
(Proc
) -- A block which will be evaluated to create a new helper module -
modules
(Array
) -- An optional list of modules to add as helpers
Other tags:
- Example: With modules: -
Example: With a block: -
def helpers(*modules, &block) self.defined_helpers ||= [] if block_given? mod = Module.new mod.module_eval(&block) modules = [mod] end self.defined_helpers += modules end
def initialize(app, options_hash={}, &block)
- Yieldparam: options - Extension options
Other tags:
- Yield: - An optional block that can be used to customize options before the extension is activated.
Parameters:
-
options_hash
(Hash
) -- The raw options hash. Subclasses should not manipulate this directly - it will be turned into {#options}. -
app
(Middleman::Application
) -- The Middleman::Application instance
def initialize(app, options_hash={}, &block) @_helpers = [] @app = app expose_methods setup_options(options_hash, &block) # Bind app hooks to local methods bind_before_configuration bind_after_configuration bind_before_build bind_after_build bind_ready end
def option(key, default=nil, description=nil, options={})
-
description
(String
) -- A human-readable description of what the option does -
default
(Object
) -- The default value for the option -
key
(Symbol
) -- The name of the option
Other tags:
- See: Middleman::Configuration::ConfigurationManager#define_setting -
def option(key, default=nil, description=nil, options={}) config.define_setting(key, default, description, options) end
def resources(*generators)
-
generators
(Array
) -- The generator definitions
Other tags:
- Example: A generator which maps a path to a string -
Example: A generator which maps a path to a method -
Example: A generator which returns an array of resources -
def resources(*generators) self.resources_generators ||= [] self.resources_generators += generators end
def setup_options(options_hash)
- Yieldparam: Middleman::Configuration::ConfigurationManager - iddleman::Configuration::ConfigurationManager] options Extension options
Other tags:
- Yield: - An optional block that can be used to customize options before the extension is activated.
def setup_options(options_hash) @options = self.class.config.dup @options.finalize! options_hash.each do |k, v| @options[k] = v end yield @options, self if block_given? @options.all_settings.each do |o| next unless o.options[:required] && !o.value_set? logger.error "The `:#{o.key}` option of the `#{self.class.ext_name}` extension is required." exit(1) end end