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

Instance Method Summary collapse

Instance Attribute Details

#inheritable_settingObject

Fetch our current inheritable settings, which are inherited by nested scopes but not shared across siblings.



27
28
29
30
31
32
33
# File 'lib/grape/dsl/settings.rb', line 27

def inheritable_setting
  return @inheritable_setting if @inheritable_setting

  @inheritable_setting = Grape::Util::InheritableSetting.new
  @inheritable_setting.inherit_from top_level_setting
  @inheritable_setting
end

Instance Method Details

#global_setting(key, value = nil) ⇒ Object



35
36
37
# File 'lib/grape/dsl/settings.rb', line 35

def global_setting(key, value = nil)
  get_or_set(inheritable_setting.global, key, value)
end

#namespace_setting(key, value = nil) ⇒ Object



43
44
45
# File 'lib/grape/dsl/settings.rb', line 43

def namespace_setting(key, value = nil)
  get_or_set(inheritable_setting.namespace, key, value)
end

#route_setting(key, value = nil) ⇒ Object



39
40
41
# File 'lib/grape/dsl/settings.rb', line 39

def route_setting(key, value = nil)
  get_or_set(inheritable_setting.route, key, value)
end

#top_level_settingObject

Fetch our top-level settings, which apply to all endpoints in the API.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/grape/dsl/settings.rb', line 13

def top_level_setting
  return @top_level_setting if @top_level_setting

  @top_level_setting = Grape::Util::InheritableSetting.new
  # 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.
  @top_level_setting.inherit_from superclass.inheritable_setting if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API::Instance
  @top_level_setting
end