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.



21
22
23
# 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

#top_level_settingObject

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



15
16
17
# File 'lib/grape/dsl/settings.rb', line 15

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



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

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



36
37
38
39
40
41
42
43
# 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

#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



48
49
50
# File 'lib/grape/dsl/settings.rb', line 48

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.



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

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



78
79
80
# File 'lib/grape/dsl/settings.rb', line 78

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

#namespace_inheritable_to_nil(key) ⇒ Object

Parameters:

  • key (Symbol)


88
89
90
# File 'lib/grape/dsl/settings.rb', line 88

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

#namespace_reverse_stackable(key, value = nil) ⇒ Object



97
98
99
# File 'lib/grape/dsl/settings.rb', line 97

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

#namespace_reverse_stackable_with_hash(key) ⇒ Object



108
109
110
111
112
113
114
115
# 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

#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



68
69
70
# File 'lib/grape/dsl/settings.rb', line 68

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



93
94
95
# File 'lib/grape/dsl/settings.rb', line 93

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

#namespace_stackable_with_hash(key) ⇒ Object



101
102
103
104
105
106
# 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

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



135
136
137
# 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

#route_endObject

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



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

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



58
59
60
# File 'lib/grape/dsl/settings.rb', line 58

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

#unset(type, key) ⇒ Object

Parameters:

  • type (Symbol)
  • key (Symbol)


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

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

#unset_api_class_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


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

def unset_api_class_setting(key)
  unset :api_class, key
end

#unset_global_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


53
54
55
# File 'lib/grape/dsl/settings.rb', line 53

def unset_global_setting(key)
  unset :global, key
end

#unset_namespace_inheritable(key) ⇒ Object

Parameters:

  • key (Symbol)


83
84
85
# File 'lib/grape/dsl/settings.rb', line 83

def unset_namespace_inheritable(key)
  unset :namespace_inheritable, key
end

#unset_namespace_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


73
74
75
# File 'lib/grape/dsl/settings.rb', line 73

def unset_namespace_setting(key)
  unset :namespace, key
end

#unset_namespace_stackable(key) ⇒ Object

Parameters:

  • key (Symbol)


118
119
120
# File 'lib/grape/dsl/settings.rb', line 118

def unset_namespace_stackable(key)
  unset :namespace_stackable, key
end

#unset_route_setting(key) ⇒ Object

Parameters:

  • key (Symbol)


63
64
65
# File 'lib/grape/dsl/settings.rb', line 63

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/grape/dsl/settings.rb', line 153

def within_namespace(&block)
  namespace_start

  result = yield if block

  namespace_end
  reset_validations!

  result
end