Sha256: 87be7505247647e57aa5c031b1cb52cf4143446d733485ea2766aa439e40c109

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

require 'active_support/concern'

module Grape
  module DSL
    module Configuration
      extend ActiveSupport::Concern

      module ClassMethods
        attr_writer :logger

        include Grape::DSL::Settings

        def logger(logger = nil)
          if logger
            global_setting(:logger, logger)
          else
            global_setting(:logger) || global_setting(:logger, Logger.new($stdout))
          end
        end

        # Add a description to the next namespace or function.
        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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grape-0.11.0 lib/grape/dsl/configuration.rb
grape-0.10.1 lib/grape/dsl/configuration.rb
grape-0.10.0 lib/grape/dsl/configuration.rb