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



129
130
131
# File 'lib/grape/dsl/settings.rb', line 129

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

#get_or_set(type, key, value) ⇒ Object



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



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.



146
147
148
149
# File 'lib/grape/dsl/settings.rb', line 146

def namespace_end
  route_end
  @inheritable_setting = inheritable_setting.parent
end

#namespace_inheritable(key, value = nil) ⇒ Object



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



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



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

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



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



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
108
# 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.



141
142
143
# File 'lib/grape/dsl/settings.rb', line 141

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.



153
154
155
# File 'lib/grape/dsl/settings.rb', line 153

def route_end
  inheritable_setting.route_end
end

#route_setting(key, value = nil) ⇒ Object



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



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



134
135
136
# File 'lib/grape/dsl/settings.rb', line 134

def unset_api_class_setting(key)
  unset :api_class, key
end

#unset_global_setting(key) ⇒ Object



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



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



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



124
125
126
# File 'lib/grape/dsl/settings.rb', line 124

def unset_namespace_stackable(key)
  unset :namespace_stackable, key
end

#unset_route_setting(key) ⇒ Object



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).



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

def within_namespace(&block)
  namespace_start

  result = yield if block

  namespace_end
  reset_validations!

  result
end