lib/grape/dsl/configuration.rb in grape-0.9.0 vs lib/grape/dsl/configuration.rb in grape-0.10.0
- old
+ new
@@ -5,22 +5,66 @@
module Configuration
extend ActiveSupport::Concern
module ClassMethods
attr_writer :logger
- attr_reader :settings
+ include Grape::DSL::Settings
+
def logger(logger = nil)
if logger
- @logger = logger
+ global_setting(:logger, logger)
else
- @logger ||= Logger.new($stdout)
+ global_setting(:logger) || global_setting(:logger, Logger.new($stdout))
end
end
# Add a description to the next namespace or function.
- def desc(description, options = {})
- @last_description = options.merge(description: description)
+ def desc(description, options = {}, &config_block)
+ if block_given?
+ config_class = Grape::DSL::Configuration.desc_container
+
+ config_class.configure do
+ description description
+ end
+
+ config_class.configure(&config_block)
+ options = config_class.settings
+ else
+ options = options.merge(description: description)
+ end
+
+ namespace_setting :description, options
+ route_setting :description, options
+ end
+ end
+
+ module_function
+
+ def stacked_hash_to_hash(settings)
+ return nil if settings.nil? || settings.blank?
+ settings.each_with_object(ActiveSupport::OrderedHash.new) { |value, result| result.deep_merge!(value) }
+ end
+
+ def desc_container
+ Module.new do
+ include Grape::Util::StrictHashConfiguration.module(
+ :description,
+ :detail,
+ :params,
+ :entity,
+ :http_codes,
+ :named,
+ :headers
+ )
+
+ def config_context.success(*args)
+ entity(*args)
+ end
+
+ def config_context.failure(*args)
+ http_codes(*args)
+ end
end
end
end
end
end