module Grape::DSL::Settings

Keeps track of settings (impemented as key-value pairs, grouped by types), in two contexts: top-level settings which apply globally no matter where they're defined, and inheritable settings which apply only in the current scope and scopes nested under it.

Attributes

inheritable_setting[RW]
top_level_setting[RW]

Public Instance Methods

api_class_setting(key, value = nil) click to toggle source

(see global_setting)

# File lib/grape/dsl/settings.rb, line 109
def api_class_setting(key, value = nil)
  get_or_set :api_class, key, value
end
get_or_set(type, key, value) click to toggle source

@param type [Symbol] @param key [Symbol] @param value [Object] will be stored if the value is currently empty @return either the old value, if it wasn't nil, or the given value

# File lib/grape/dsl/settings.rb, line 36
def get_or_set(type, key, value)
  setting = inheritable_setting.send(type)
  if value.nil?
    setting[key]
  else
    setting[key] = value
  end
end
global_setting(key, value = nil) click to toggle source

@param key [Symbol] @param value [Object] @return (see get_or_set)

# File lib/grape/dsl/settings.rb, line 48
def global_setting(key, value = nil)
  get_or_set :global, key, value
end
namespace_end() click to toggle source

Set the inheritable settings pointer back up by one level.

# File lib/grape/dsl/settings.rb, line 126
def namespace_end
  route_end
  @inheritable_setting = inheritable_setting.parent
end
namespace_inheritable(key, value = nil) click to toggle source

(see global_setting)

# File lib/grape/dsl/settings.rb, line 78
def namespace_inheritable(key, value = nil)
  get_or_set :namespace_inheritable, key, value
end
namespace_inheritable_to_nil(key) click to toggle source

@param key [Symbol]

# File lib/grape/dsl/settings.rb, line 88
def namespace_inheritable_to_nil(key)
  inheritable_setting.namespace_inheritable[key] = nil
end
namespace_setting(key, value = nil) click to toggle source

(see global_setting)

# File lib/grape/dsl/settings.rb, line 68
def namespace_setting(key, value = nil)
  get_or_set :namespace, key, value
end
namespace_stackable(key, value = nil) click to toggle source

(see global_setting)

# File lib/grape/dsl/settings.rb, line 93
def namespace_stackable(key, value = nil)
  get_or_set :namespace_stackable, key, value
end
namespace_stackable_with_hash(key) click to toggle source
# File lib/grape/dsl/settings.rb, line 97
def namespace_stackable_with_hash(key)
  settings = get_or_set :namespace_stackable, key, nil
  return if settings.blank?
  settings.each_with_object({}) { |value, result| result.deep_merge!(value) }
end
namespace_start() click to toggle source

Fork our inheritable settings to a new instance, copied from our parent's, but separate so we won't modify it. Every call to this method should have an answering call to namespace_end.

# File lib/grape/dsl/settings.rb, line 121
def namespace_start
  @inheritable_setting = Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from inheritable_setting }
end
route_end() click to toggle source

Stop defining settings for the current route and clear them for the next, within a namespace.

# File lib/grape/dsl/settings.rb, line 133
def route_end
  inheritable_setting.route_end
end
route_setting(key, value = nil) click to toggle source

(see global_setting)

# File lib/grape/dsl/settings.rb, line 58
def route_setting(key, value = nil)
  get_or_set :route, key, value
end
unset(type, key) click to toggle source

@param type [Symbol] @param key [Symbol]

# File lib/grape/dsl/settings.rb, line 27
def unset(type, key)
  setting = inheritable_setting.send(type)
  setting.delete key
end
unset_api_class_setting(key) click to toggle source

(see unset_global_setting)

# File lib/grape/dsl/settings.rb, line 114
def unset_api_class_setting(key)
  unset :api_class, key
end
unset_global_setting(key) click to toggle source

@param key [Symbol]

# File lib/grape/dsl/settings.rb, line 53
def unset_global_setting(key)
  unset :global, key
end
unset_namespace_inheritable(key) click to toggle source

(see unset_global_setting)

# File lib/grape/dsl/settings.rb, line 83
def unset_namespace_inheritable(key)
  unset :namespace_inheritable, key
end
unset_namespace_setting(key) click to toggle source

(see unset_global_setting)

# File lib/grape/dsl/settings.rb, line 73
def unset_namespace_setting(key)
  unset :namespace, key
end
unset_namespace_stackable(key) click to toggle source

(see unset_global_setting)

# File lib/grape/dsl/settings.rb, line 104
def unset_namespace_stackable(key)
  unset :namespace_stackable, key
end
unset_route_setting(key) click to toggle source

(see unset_global_setting)

# File lib/grape/dsl/settings.rb, line 63
def unset_route_setting(key)
  unset :route, key
end
within_namespace() { || ... } click to toggle source

Execute the block within a context where our inheritable settings are forked to a new copy (see namespace_start).

# File lib/grape/dsl/settings.rb, line 139
def within_namespace(&_block)
  namespace_start

  result = yield if block_given?

  namespace_end
  reset_validations!

  result
end