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

64 lines
1.8 KiB
Ruby
Raw Normal View History

require 'rack/mime'
require 'middleman-core/callback_manager'
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.
2015-04-26 22:22:58 +02:00
def_delegators :@app, :config, :logger, :use, :map, :mime_type, :files, :root, :build?, :server?, :environment?
def_delegator :"@app.extensions", :activate
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
@callbacks = ::Middleman::CallbackManager.new
2015-05-04 18:58:29 +02:00
@callbacks.install_methods!(self, [:before_build, :after_build, :configure, :after_configuration, :ready])
# Trigger internal callbacks when app level are executed.
2015-05-04 18:58:29 +02:00
app.subscribe_to_callbacks(&method(:execute_callbacks))
2014-01-01 03:21:30 +01:00
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 set(key, default=nil, &block)
config.define_setting(key, default) unless config.defines_setting?(key)
@app.config[key] = block_given? ? block : default
end
# Add a new mime-type for a specific extension
#
# @param [Symbol] type File extension
# @param [String] value Mime type
# @return [void]
def mime_type(type, value)
type = ".#{type}" unless type.to_s[0] == '.'
::Rack::Mime::MIME_TYPES[type] = value
end
2014-01-01 03:21:30 +01:00
end
2014-04-29 19:50:21 +02:00
end