Class: Grape::Util::InheritableSetting

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInheritableSetting

Returns a new instance of InheritableSetting.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grape/util/inheritable_setting.rb', line 15

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



4
5
6
# File 'lib/grape/util/inheritable_setting.rb', line 4

def api_class
  @api_class
end

#namespaceObject

Returns the value of attribute namespace.



4
5
6
# File 'lib/grape/util/inheritable_setting.rb', line 4

def namespace
  @namespace
end

#namespace_inheritableObject

Returns the value of attribute namespace_inheritable.



4
5
6
# File 'lib/grape/util/inheritable_setting.rb', line 4

def namespace_inheritable
  @namespace_inheritable
end

#namespace_stackableObject

Returns the value of attribute namespace_stackable.



4
5
6
# File 'lib/grape/util/inheritable_setting.rb', line 4

def namespace_stackable
  @namespace_stackable
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#point_in_time_copiesObject

Returns the value of attribute point_in_time_copies.



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

def point_in_time_copies
  @point_in_time_copies
end

#routeObject

Returns the value of attribute route.



4
5
6
# File 'lib/grape/util/inheritable_setting.rb', line 4

def route
  @route
end

Class Method Details

.globalObject



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

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

.reset_global!Object

only for testing



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

def self.reset_global! # only for testing
  @global = {}
end

Instance Method Details

#globalObject



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

def global
  self.class.global
end

#inherit_from(parent) ⇒ Object



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

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



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

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



59
60
61
# File 'lib/grape/util/inheritable_setting.rb', line 59

def route_end
  @route = {}
end

#to_hashObject



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

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