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



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

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.point_in_time_copies = []

  self.parent = nil
end

Instance Attribute Details

#api_classObject

Returns the value of attribute api_class.



6
7
8
# File 'lib/grape/util/inheritable_setting.rb', line 6

def api_class
  @api_class
end

#namespaceObject

Returns the value of attribute namespace.



6
7
8
# File 'lib/grape/util/inheritable_setting.rb', line 6

def namespace
  @namespace
end

#namespace_inheritableObject

Returns the value of attribute namespace_inheritable.



6
7
8
# File 'lib/grape/util/inheritable_setting.rb', line 6

def namespace_inheritable
  @namespace_inheritable
end

#namespace_stackableObject

Returns the value of attribute namespace_stackable.



6
7
8
# File 'lib/grape/util/inheritable_setting.rb', line 6

def namespace_stackable
  @namespace_stackable
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#point_in_time_copiesObject

Returns the value of attribute point_in_time_copies.



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

def point_in_time_copies
  @point_in_time_copies
end

#routeObject

Returns the value of attribute route.



6
7
8
# File 'lib/grape/util/inheritable_setting.rb', line 6

def route
  @route
end

Class Method Details

.globalObject

Retrieve global settings.



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

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.



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

def self.reset_global!
  @global = {}
end

Instance Method Details

#globalObject

Return the class-level global properties.



38
39
40
# File 'lib/grape/util/inheritable_setting.rb', line 38

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:



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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/grape/util/inheritable_setting.rb', line 62

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



79
80
81
# File 'lib/grape/util/inheritable_setting.rb', line 79

def route_end
  @route = {}
end

#to_hashObject

Return a serializable hash of our values.



84
85
86
87
88
89
90
91
92
# File 'lib/grape/util/inheritable_setting.rb', line 84

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
  }
end