Class: Grape::Util::InheritableSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/util/inheritable_setting.rb

Overview

A branchable, inheritable settings object which can store both stackable and inheritable values (see InheritableValues and StackableValues).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInheritableSetting

Instantiate a new settings instance, with blank values. The fresh instance can then be set to inherit from an existing instance (see #inherit_from).



35
36
37
38
39
40
41
42
43
44
# File 'lib/grape/util/inheritable_setting.rb', line 35

def initialize
  @route = {}
  @namespace = InheritableValues.new # only inheritable from a parent when
  # used with a mount, or should every API::Class be a separate namespace by default?
  @namespace_inheritable = InheritableValues.new
  @namespace_stackable = StackableValues.new
  @namespace_reverse_stackable = ReverseStackableValues.new
  @parent = nil
  # @api_class and @point_in_time_copies stay nil until first access.
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



8
9
10
# File 'lib/grape/util/inheritable_setting.rb', line 8

def namespace
  @namespace
end

#namespace_inheritableObject (readonly)

Returns the value of attribute namespace_inheritable.



8
9
10
# File 'lib/grape/util/inheritable_setting.rb', line 8

def namespace_inheritable
  @namespace_inheritable
end

#namespace_reverse_stackableObject (readonly)

Returns the value of attribute namespace_reverse_stackable.



8
9
10
# File 'lib/grape/util/inheritable_setting.rb', line 8

def namespace_reverse_stackable
  @namespace_reverse_stackable
end

#namespace_stackableObject (readonly)

Returns the value of attribute namespace_stackable.



8
9
10
# File 'lib/grape/util/inheritable_setting.rb', line 8

def namespace_stackable
  @namespace_stackable
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/grape/util/inheritable_setting.rb', line 8

def parent
  @parent
end

#routeObject (readonly)

Returns the value of attribute route.



8
9
10
# File 'lib/grape/util/inheritable_setting.rb', line 8

def route
  @route
end

Class Method Details

.globalObject

Retrieve global settings.



21
22
23
# File 'lib/grape/util/inheritable_setting.rb', line 21

def self.global
  @global ||= {}
end

.reset_global!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

only for testing

Clear all global settings.



28
29
30
# File 'lib/grape/util/inheritable_setting.rb', line 28

def self.reset_global!
  @global = {}
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



98
99
100
# File 'lib/grape/util/inheritable_setting.rb', line 98

def ==(other)
  other.is_a?(self.class) && to_hash == other.to_hash
end

#api_classObject

Lazy-allocated; api_class and point_in_time_copies are rarely written on most settings layers, so don’t pay for a Hash/Array each.



12
13
14
# File 'lib/grape/util/inheritable_setting.rb', line 12

def api_class
  @api_class ||= {}
end

#globalObject

Return the class-level global properties.



47
48
49
# File 'lib/grape/util/inheritable_setting.rb', line 47

def global
  self.class.global
end

#inherit_from(parent) ⇒ Object

Set our inherited values to the given parent’s current values. Also, update the inherited values on any settings instances which were forked from us.

Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grape/util/inheritable_setting.rb', line 55

def inherit_from(parent)
  return if parent.nil?

  @parent = parent

  namespace_inheritable.inherited_values = parent.namespace_inheritable
  namespace_stackable.inherited_values = parent.namespace_stackable
  namespace_reverse_stackable.inherited_values = parent.namespace_reverse_stackable
  @route = parent.route.merge(route)

  @point_in_time_copies&.each { |cloned_one| cloned_one.inherit_from parent }
end

#namespace_stackable_with_hash(key) ⇒ Object



103
104
105
106
107
108
# File 'lib/grape/util/inheritable_setting.rb', line 103

def namespace_stackable_with_hash(key)
  data = namespace_stackable[key]
  return if data.blank?

  data.each_with_object({}) { |value, result| result.deep_merge!(value) }
end

#point_in_time_copiesObject



16
17
18
# File 'lib/grape/util/inheritable_setting.rb', line 16

def point_in_time_copies
  @point_in_time_copies ||= []
end

#point_in_time_copyObject

Create a point-in-time copy of this settings instance, with clones of all our values. Note that, should this instance’s parent be set or changed via #inherit_from, it will copy that inheritence to any copies which were made.



72
73
74
75
76
77
78
# File 'lib/grape/util/inheritable_setting.rb', line 72

def point_in_time_copy
  new_setting = self.class.new
  point_in_time_copies << new_setting
  new_setting.copy_state_from(self)
  new_setting.inherit_from(parent)
  new_setting
end

#route_endObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the instance store of per-route settings.



82
83
84
# File 'lib/grape/util/inheritable_setting.rb', line 82

def route_end
  @route = {}
end

#to_hashObject

Return a serializable hash of our values.



87
88
89
90
91
92
93
94
95
96
# File 'lib/grape/util/inheritable_setting.rb', line 87

def to_hash
  {
    global: global.clone,
    route: route.clone,
    namespace: namespace.to_hash,
    namespace_inheritable: namespace_inheritable.to_hash,
    namespace_stackable: namespace_stackable.to_hash,
    namespace_reverse_stackable: namespace_reverse_stackable.to_hash
  }
end