Module: Grape::DSL::Settings

Extended by:
ActiveSupport::Concern
Included in:
Configuration::ClassMethods, Desc, InsideRoute, Logger, 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.



23
24
25
# File 'lib/grape/dsl/settings.rb', line 23

def inheritable_setting
  @inheritable_setting ||= Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from top_level_setting }
end

#top_level_settingObject

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



17
18
19
# File 'lib/grape/dsl/settings.rb', line 17

def top_level_setting
  @top_level_setting ||= build_top_level_setting
end

Instance Method Details

#api_class_setting(key, value = nil) ⇒ Object

Returns either the old value, if it wasn’t nil, or the given value.

Parameters:

  • key (Symbol)
  • value (Object) (defaults to: nil)

Returns:

  • either the old value, if it wasn’t nil, or the given value



127
128
129
# File 'lib/grape/dsl/settings.rb', line 127

def api_class_setting(key, value = nil)
  get_or_set :api_class, key, value
end

#get_or_set(type, key, value) ⇒ Object

Returns either the old value, if it wasn’t nil, or the given value.

Parameters:

  • type (Symbol)
  • key (Symbol)
  • value (Object)

    will be stored if the value is currently empty

Returns:

  • either the old value, if it wasn’t nil, or the given value



38
39
40
41
42
43
44
45
# File 'lib/grape/dsl/settings.rb', line 38

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) ⇒ Object

Returns either the old value, if it wasn’t nil, or the given value.

Parameters:

  • key (Symbol)
  • value (Object) (defaults to: nil)

Returns:

  • either the old value, if it wasn’t nil, or the given value



50
51
52
# File 'lib/grape/dsl/settings.rb', line 50

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

#namespace_endObject

Set the inheritable settings pointer back up by one level.



144
145
146
147
# File 'lib/grape/dsl/settings.rb', line 144

def namespace_end
  route_end
  @inheritable_setting = inheritable_setting.parent
end

#namespace_inheritable(key, value = nil) ⇒ Object

Returns either the old value, if it wasn’t nil, or the given value.

Parameters:

  • key (Symbol)
  • value (Object) (defaults to: nil)

Returns:

  • either the old value, if it wasn’t nil, or the given value



80
81
82
# File 'lib/grape/dsl/settings.rb', line 80

def namespace_inheritable(key, value = nil)
  get_or_set :namespace_inheritable, key, value
end

#namespace_inheritable_to_nil(key) ⇒ Object

Parameters:

  • key (Symbol)


90
91
92
# File 'lib/grape/dsl/settings.rb', line 90

def namespace_inheritable_to_nil(key)
  inheritable_setting.namespace_inheritable[key] = nil
end

#namespace_reverse_stackable(key, value = nil) ⇒ Object



99
100
101
# File 'lib/grape/dsl/settings.rb', line 99

def namespace_reverse_stackable(key, value = nil)
  get_or_set :namespace_reverse_stackable, key, value
end

#namespace_reverse_stackable_with_hash(key) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/grape/dsl/settings.rb', line 109

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

#namespace_setting(key, value = nil) ⇒ Object

Returns either the old value, if it wasn’t nil, or the given value.

Parameters:

  • key (Symbol)
  • value (Object) (defaults to: nil)

Returns:

  • either the old value, if it wasn’t nil, or the given value



70
71
72
# File 'lib/grape/dsl/settings.rb', line 70

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

#namespace_stackable(key, value = nil) ⇒ Object

Returns either the old value, if it wasn’t nil, or the given value.

Parameters:

  • key (Symbol)
  • value (Object) (defaults to: nil)

Returns:

  • either the old value, if it wasn’t nil, or the given value



95
96
97
# File 'lib/grape/dsl/settings.rb', line 95

def namespace_stackable(key, value = nil)
  get_or_set :namespace_stackable, key, value
end

#namespace_stackable_with_hash(key) ⇒ Object



103
104
105
106
107
# File 'lib/grape/dsl/settings.rb', line 103

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_startObject

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.



139
140
141
# File 'lib/grape/dsl/settings.rb', line 139

def namespace_start
  @inheritable_setting = Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from inheritable_setting }
end

#route_endObject

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



151
152
153
# File 'lib/grape/dsl/settings.rb', line 151

def route_end
  inheritable_setting.route_end
end

#route_setting(key, value = nil) ⇒ Object

Returns either the old value, if it wasn’t nil, or the given value.

Parameters:

  • key (Symbol)
  • value (Object) (defaults to: nil)

Returns:

  • either the old value, if it wasn’t nil, or the given value



60
61
62
# File 'lib/grape/dsl/settings.rb', line 60

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

#unset(type, key) ⇒ Object

Parameters:

  • type (Symbol)
  • key (Symbol)


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

def unset(type, key)
  setting = inheritable_setting.send(type)
  setting.delete key
end

#unset_api_class_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


132
133
134
# File 'lib/grape/dsl/settings.rb', line 132

def unset_api_class_setting(key)
  unset :api_class, key
end

#unset_global_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


55
56
57
# File 'lib/grape/dsl/settings.rb', line 55

def unset_global_setting(key)
  unset :global, key
end

#unset_namespace_inheritable(key) ⇒ Object

Parameters:

  • key (Symbol)


85
86
87
# File 'lib/grape/dsl/settings.rb', line 85

def unset_namespace_inheritable(key)
  unset :namespace_inheritable, key
end

#unset_namespace_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


75
76
77
# File 'lib/grape/dsl/settings.rb', line 75

def unset_namespace_setting(key)
  unset :namespace, key
end

#unset_namespace_stackable(key) ⇒ Object

Parameters:

  • key (Symbol)


122
123
124
# File 'lib/grape/dsl/settings.rb', line 122

def unset_namespace_stackable(key)
  unset :namespace_stackable, key
end

#unset_route_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


65
66
67
# File 'lib/grape/dsl/settings.rb', line 65

def unset_route_setting(key)
  unset :route, key
end

#within_namespace(&_block) ⇒ Object

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



157
158
159
160
161
162
163
164
165
166
# File 'lib/grape/dsl/settings.rb', line 157

def within_namespace(&_block)
  namespace_start

  result = yield if block_given?

  namespace_end
  reset_validations!

  result
end