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
Source
# File lib/grape/dsl/settings.rb, line 123 def api_class_setting(key, value = nil) get_or_set :api_class, key, value end
(see global_setting
)
Source
# 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 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
Source
# File lib/grape/dsl/settings.rb, line 48 def global_setting(key, value = nil) get_or_set :global, key, value end
@param key [Symbol] @param value [Object] @return (see get_or_set
)
Source
# 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
Fetch our current inheritable settings, which are inherited by nested scopes but not shared across siblings.
Source
# File lib/grape/dsl/settings.rb, line 140 def namespace_end route_end @inheritable_setting = inheritable_setting.parent end
Set the inheritable settings pointer back up by one level.
Source
# File lib/grape/dsl/settings.rb, line 78 def namespace_inheritable(key, value = nil) get_or_set :namespace_inheritable, key, value end
(see global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 88 def namespace_inheritable_to_nil(key) inheritable_setting.namespace_inheritable[key] = nil end
@param key [Symbol]
Source
# File lib/grape/dsl/settings.rb, line 97 def namespace_reverse_stackable(key, value = nil) get_or_set :namespace_reverse_stackable, key, value end
Source
# File lib/grape/dsl/settings.rb, line 108 def namespace_reverse_stackable_with_hash(key) settings = get_or_set :namespace_reverse_stackable, key, nil return if settings.blank? settings.each_with_object({}) do |setting, result| result.merge!(setting) { |_k, s1, _s2| s1 } end end
Source
# File lib/grape/dsl/settings.rb, line 68 def namespace_setting(key, value = nil) get_or_set :namespace, key, value end
(see global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 93 def namespace_stackable(key, value = nil) get_or_set :namespace_stackable, key, value end
(see global_setting
)
Source
# 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
Source
# File lib/grape/dsl/settings.rb, line 135 def namespace_start @inheritable_setting = Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from inheritable_setting } 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
.
Source
# File lib/grape/dsl/settings.rb, line 147 def route_end inheritable_setting.route_end end
Stop defining settings for the current route and clear them for the next, within a namespace.
Source
# File lib/grape/dsl/settings.rb, line 58 def route_setting(key, value = nil) get_or_set :route, key, value end
(see global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 15 def top_level_setting @top_level_setting ||= build_top_level_setting end
Fetch our top-level settings, which apply to all endpoints in the API
.
Source
# File lib/grape/dsl/settings.rb, line 27 def unset(type, key) setting = inheritable_setting.send(type) setting.delete key end
@param type [Symbol] @param key [Symbol]
Source
# File lib/grape/dsl/settings.rb, line 128 def unset_api_class_setting(key) unset :api_class, key end
(see unset_global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 53 def unset_global_setting(key) unset :global, key end
@param key [Symbol]
Source
# File lib/grape/dsl/settings.rb, line 83 def unset_namespace_inheritable(key) unset :namespace_inheritable, key end
(see unset_global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 73 def unset_namespace_setting(key) unset :namespace, key end
(see unset_global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 118 def unset_namespace_stackable(key) unset :namespace_stackable, key end
(see unset_global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 63 def unset_route_setting(key) unset :route, key end
(see unset_global_setting
)
Source
# File lib/grape/dsl/settings.rb, line 153 def within_namespace(&block) namespace_start result = yield if block namespace_end reset_validations! result end
Execute the block within a context where our inheritable settings are forked to a new copy (see namespace_start
).
Private Instance Methods
Source
# File lib/grape/dsl/settings.rb, line 168 def build_top_level_setting Grape::Util::InheritableSetting.new.tap do |setting| # Doesn't try to inherit settings from +Grape::API::Instance+ which also responds to # +inheritable_setting+, however, it doesn't contain any user-defined settings. # Otherwise, it would lead to an extra instance of +Grape::Util::InheritableSetting+ # in the chain for every endpoint. setting.inherit_from superclass.inheritable_setting if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API::Instance end end
Builds the current class :inheritable_setting. If available, it inherits from the superclass’s :inheritable_setting.