middleman/middleman-core/lib/middleman-core/config_context.rb

91 lines
2.1 KiB
Ruby
Raw Normal View History

2014-01-01 03:21:30 +01:00
module Middleman
class ConfigContext
extend Forwardable
2014-01-01 03:21:30 +01:00
attr_reader :app
# Whitelist methods that can reach out.
def_delegators :@app, :config, :logger, :activate, :use, :map, :mime_type, :data, :files, :root
2014-01-01 03:21:30 +01:00
2014-01-02 06:19:05 +01:00
def initialize(app, template_context_class)
2014-01-01 03:21:30 +01:00
@app = app
2014-01-02 06:19:05 +01:00
@template_context_class = template_context_class
2014-01-01 03:21:30 +01:00
@ready_callbacks = []
@after_build_callbacks = []
@after_configuration_callbacks = []
@configure_callbacks = {}
end
2014-01-02 06:19:05 +01:00
def helpers(*helper_modules, &block)
helper_modules ||= []
if block_given?
block_module = Module.new
block_module.module_eval(&block)
helper_modules << block_module
end
helper_modules.each do |mod|
@template_context_class.send :include, mod
end
end
2014-06-07 00:32:00 +02:00
def include_environment(name)
path = File.dirname(__FILE__)
other_config = File.join(path, name.to_s)
2014-07-02 20:05:57 +02:00
return unless File.exist? other_config
instance_eval File.read(other_config), other_config, 1
2014-06-07 00:32:00 +02:00
end
2014-01-01 03:21:30 +01:00
def ready(&block)
@ready_callbacks << block
end
def execute_ready_callbacks
@ready_callbacks.each do |b|
instance_exec(&b)
end
end
def after_build(&block)
@after_build_callbacks << block
end
def execute_after_build_callbacks(*args)
@after_build_callbacks.each do |b|
instance_exec(*args, &b)
end
end
2014-04-29 19:50:21 +02:00
2014-01-01 03:21:30 +01:00
def after_configuration(&block)
@after_configuration_callbacks << block
end
def execute_after_configuration_callbacks
@after_configuration_callbacks.each do |b|
instance_exec(&b)
end
end
def configure(key, &block)
@configure_callbacks[key] ||= []
@configure_callbacks[key] << block
end
def execute_configure_callbacks(key)
@configure_callbacks[key] ||= []
@configure_callbacks[key].each do |b|
instance_exec(&b)
end
end
def set(key, default=nil, &block)
config.define_setting(key, default) unless config.defines_setting?(key)
@app.config[key] = block_given? ? block : default
end
end
2014-04-29 19:50:21 +02:00
end