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



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/grape/util/inheritable_setting.rb', line 25

def initialize
  self.route = {}
  self.api_class = {}
  self.namespace = InheritableValues.new # only inheritable from a parent when
  # used with a mount, or should every API::Class be a separate namespace by default?
  self.namespace_inheritable = InheritableValues.new
  self.namespace_stackable = StackableValues.new
  self.namespace_reverse_stackable = ReverseStackableValues.new

  self.point_in_time_copies = []

  self.parent = nil
end

Instance Attribute Details

#api_classObject

Returns the value of attribute api_class.



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

def api_class
  @api_class
end

#namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#namespace_inheritableObject

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

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

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

Returns the value of attribute parent.



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

def parent
  @parent
end

#point_in_time_copiesObject

Returns the value of attribute point_in_time_copies.



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

def point_in_time_copies
  @point_in_time_copies
end

#routeObject

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.



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

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.



18
19
20
# File 'lib/grape/util/inheritable_setting.rb', line 18

def self.reset_global!
  @global = {}
end

Instance Method Details

#globalObject

Return the class-level global properties.



40
41
42
# File 'lib/grape/util/inheritable_setting.rb', line 40

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:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/grape/util/inheritable_setting.rb', line 48

def inherit_from(parent)
  return if parent.nil?

  self.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
  self.route = parent.route.merge(route)

  point_in_time_copies.map { |cloned_one| cloned_one.inherit_from parent }
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.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/grape/util/inheritable_setting.rb', line 65

def point_in_time_copy
  self.class.new.tap do |new_setting|
    point_in_time_copies << new_setting
    new_setting.point_in_time_copies = []

    new_setting.namespace = namespace.clone
    new_setting.namespace_inheritable = namespace_inheritable.clone
    new_setting.namespace_stackable = namespace_stackable.clone
    new_setting.namespace_reverse_stackable = namespace_reverse_stackable.clone
    new_setting.route = route.clone
    new_setting.api_class = api_class

    new_setting.inherit_from(parent)
  end
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.



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

def route_end
  @route = {}
end

#to_hashObject

Return a serializable hash of our values.



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

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