Class: Grape::Util::InheritableSetting
- Inherits:
-
Object
- Object
- Grape::Util::InheritableSetting
- 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
-
#api_class ⇒ Object
Returns the value of attribute api_class.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#namespace_inheritable ⇒ Object
Returns the value of attribute namespace_inheritable.
-
#namespace_reverse_stackable ⇒ Object
Returns the value of attribute namespace_reverse_stackable.
-
#namespace_stackable ⇒ Object
Returns the value of attribute namespace_stackable.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#point_in_time_copies ⇒ Object
Returns the value of attribute point_in_time_copies.
-
#route ⇒ Object
Returns the value of attribute route.
Class Method Summary collapse
-
.global ⇒ Object
Retrieve global settings.
-
.reset_global! ⇒ Object
private
Clear all global settings.
Instance Method Summary collapse
-
#global ⇒ Object
Return the class-level global properties.
-
#inherit_from(parent) ⇒ Object
Set our inherited values to the given parent’s current values.
-
#initialize ⇒ InheritableSetting
constructor
Instantiate a new settings instance, with blank values.
-
#point_in_time_copy ⇒ Object
Create a point-in-time copy of this settings instance, with clones of all our values.
-
#route_end ⇒ Object
private
Resets the instance store of per-route settings.
-
#to_hash ⇒ Object
Return a serializable hash of our values.
Constructor Details
#initialize ⇒ InheritableSetting
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_class ⇒ Object
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 |
#namespace ⇒ Object
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/grape/util/inheritable_setting.rb', line 8 def namespace @namespace end |
#namespace_inheritable ⇒ Object
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_stackable ⇒ Object
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_stackable ⇒ Object
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 |
#parent ⇒ Object
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_copies ⇒ Object
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 |
#route ⇒ Object
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
.global ⇒ Object
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.
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
#global ⇒ Object
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.
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_copy ⇒ Object
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_end ⇒ 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.
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_hash ⇒ Object
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 |