Required options for extensions

This commit is contained in:
Thomas Reynolds 2014-07-23 21:08:20 -07:00
parent 21c38b707c
commit 5e20fca73e
3 changed files with 24 additions and 24 deletions

View file

@ -70,13 +70,14 @@ module Middleman
# @param [Symbol] key The name of the option
# @param [Object] default The default value for the option
# @param [String] description A human-readable description of what the option does
# @param [Hash] options Additional options.
# @return [ConfigSetting]
def define_setting(key, default=nil, description=nil)
def define_setting(key, default=nil, description=nil, options={})
raise "Setting #{key} doesn't exist" if @finalized
raise "Setting #{key} already defined" if @settings.key?(key)
raise 'Setting key must be a Symbol' unless key.is_a? Symbol
@settings[key] = ConfigSetting.new(key, default, description)
@settings[key] = ConfigSetting.new(key, default, description, options)
end
# Switch the configuration manager is finalized, it switches to read-only
@ -94,7 +95,7 @@ module Middleman
# Load in a list of settings
def load_settings(other_settings)
other_settings.each do |setting|
new_setting = define_setting(setting.key, setting.default, setting.description)
new_setting = define_setting(setting.key, setting.default, setting.description, setting.options)
new_setting.value = setting.value if setting.value_set?
end
end
@ -124,11 +125,15 @@ module Middleman
# A human-friendly description of the setting
attr_accessor :description
def initialize(key, default, description)
# Additional config.
attr_accessor :options
def initialize(key, default, description, options={})
@value_set = false
self.key = key
self.default = default
self.description = description
self.options = options
end
# The user-supplied value for this setting, overriding the default

View file

@ -106,8 +106,8 @@ module Middleman
# @param [Symbol] key The name of the option
# @param [Object] default The default value for the option
# @param [String] description A human-readable description of what the option does
def option(key, default=nil, description=nil)
config.define_setting(key, default, description)
def option(key, default=nil, description=nil, options={})
config.define_setting(key, default, description, options)
end
# Declare helpers to be added the global Middleman application.
@ -239,6 +239,13 @@ module Middleman
end
yield @options 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
def bind_before_configuration

View file

@ -1,26 +1,14 @@
class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
self.supports_multiple_instances = true
option :name, nil, 'The name of the pipeline'
option :command, nil, 'The command to initialize'
option :source, nil, 'Path to merge into sitemap'
option :name, nil, 'The name of the pipeline', required: true
option :command, nil, 'The command to initialize', required: true
option :source, nil, 'Path to merge into sitemap', required: true
option :latency, 0.25, 'Latency between refreshes of source'
def initialize(app, config={}, &block)
super
if options[:name].nil?
throw "Name is required"
end
if options[:command].nil?
throw "Command is required"
end
if options[:source].nil?
throw "Source is required"
end
require 'thread'
app.files.watch :source,