module Grape::DSL::Settings
Keeps track of settings (implemented 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
Public Instance Methods
(see global_setting)
# File lib/grape/dsl/settings.rb, line 125 def api_class_setting(key, value = nil) get_or_set :api_class, key, value end
@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
@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
Fetch our current inheritable settings, which are inherited by nested scopes but not shared across siblings.
# File lib/grape/dsl/settings.rb, line 21 def inheritable_setting @inheritable_setting ||= Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from top_level_setting } end
Set the inheritable settings pointer back up by one level.
# File lib/grape/dsl/settings.rb, line 142 def namespace_end route_end @inheritable_setting = inheritable_setting.parent end
(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
@param key [Symbol]
# File lib/grape/dsl/settings.rb, line 88 def namespace_inheritable_to_nil(key) inheritable_setting.namespace_inheritable[key] = nil end
# File lib/grape/dsl/settings.rb, line 97 def namespace_reverse_stackable(key, value = nil) get_or_set :namespace_reverse_stackable, key, value end
# File lib/grape/dsl/settings.rb, line 107 def namespace_reverse_stackable_with_hash(key) settings = get_or_set :namespace_reverse_stackable, key, nil return if settings.blank? result = {} settings.each do |setting| setting.each do |field, value| result[field] ||= value end end result end
(see global_setting)
# File lib/grape/dsl/settings.rb, line 68 def namespace_setting(key, value = nil) get_or_set :namespace, key, value end
(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
# File lib/grape/dsl/settings.rb, line 101 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
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 137 def namespace_start @inheritable_setting = Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from inheritable_setting } end
Stop defining settings for the current route and clear them for the next, within a namespace.
# File lib/grape/dsl/settings.rb, line 149 def route_end inheritable_setting.route_end end
(see global_setting)
# File lib/grape/dsl/settings.rb, line 58 def route_setting(key, value = nil) get_or_set :route, key, value end
Fetch our top-level settings, which apply to all endpoints in the API.
# File lib/grape/dsl/settings.rb, line 15 def top_level_setting @top_level_setting ||= build_top_level_setting end
@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
(see unset_global_setting)
# File lib/grape/dsl/settings.rb, line 130 def unset_api_class_setting(key) unset :api_class, key end
@param key [Symbol]
# File lib/grape/dsl/settings.rb, line 53 def unset_global_setting(key) unset :global, key end
(see unset_global_setting)
# File lib/grape/dsl/settings.rb, line 83 def unset_namespace_inheritable(key) unset :namespace_inheritable, key end
(see unset_global_setting)
# File lib/grape/dsl/settings.rb, line 73 def unset_namespace_setting(key) unset :namespace, key end
(see unset_global_setting)
# File lib/grape/dsl/settings.rb, line 120 def unset_namespace_stackable(key) unset :namespace_stackable, key end
(see unset_global_setting)
# File lib/grape/dsl/settings.rb, line 63 def unset_route_setting(key) unset :route, key end
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 155 def within_namespace(&_block) namespace_start result = yield if block_given? namespace_end reset_validations! result end
Private Instance Methods
Builds the current class :inheritable_setting. If available, it inherits from the superclass's :inheritable_setting.
# File lib/grape/dsl/settings.rb, line 170 def build_top_level_setting Grape::Util::InheritableSetting.new.tap do |setting| if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API setting.inherit_from superclass.inheritable_setting end end end