Module: Grape::DSL::Settings
- Included in:
- API::Instance, Desc, Endpoint
- Defined in:
- lib/grape/dsl/settings.rb
Overview
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.
Instance Attribute Summary collapse
-
#inheritable_setting ⇒ Object
Fetch our current inheritable settings, which are inherited by nested scopes but not shared across siblings.
Instance Method Summary collapse
- #global_setting(key, value = nil) ⇒ Object
- #namespace_setting(key, value = nil) ⇒ Object
- #route_setting(key, value = nil) ⇒ Object
-
#top_level_setting ⇒ Object
Fetch our top-level settings, which apply to all endpoints in the API.
Instance Attribute Details
#inheritable_setting ⇒ Object
Fetch our current inheritable settings, which are inherited by nested scopes but not shared across siblings.
25 26 27 |
# File 'lib/grape/dsl/settings.rb', line 25 def inheritable_setting @inheritable_setting ||= Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from top_level_setting } end |
Instance Method Details
#global_setting(key, value = nil) ⇒ Object
29 30 31 |
# File 'lib/grape/dsl/settings.rb', line 29 def global_setting(key, value = nil) get_or_set(inheritable_setting.global, key, value) end |
#namespace_setting(key, value = nil) ⇒ Object
37 38 39 |
# File 'lib/grape/dsl/settings.rb', line 37 def namespace_setting(key, value = nil) get_or_set(inheritable_setting.namespace, key, value) end |
#route_setting(key, value = nil) ⇒ Object
33 34 35 |
# File 'lib/grape/dsl/settings.rb', line 33 def route_setting(key, value = nil) get_or_set(inheritable_setting.route, key, value) end |
#top_level_setting ⇒ Object
Fetch our top-level settings, which apply to all endpoints in the API.
13 14 15 16 17 18 19 20 21 |
# File 'lib/grape/dsl/settings.rb', line 13 def top_level_setting @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 |