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
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#namespace_inheritable ⇒ Object
readonly
Returns the value of attribute namespace_inheritable.
-
#namespace_reverse_stackable ⇒ Object
readonly
Returns the value of attribute namespace_reverse_stackable.
-
#namespace_stackable ⇒ Object
readonly
Returns the value of attribute namespace_stackable.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#route ⇒ Object
readonly
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
- #==(other) ⇒ Object (also: #eql?)
-
#api_class ⇒ Object
Lazy-allocated;
api_classandpoint_in_time_copiesare rarely written on most settings layers, so don’t pay for a Hash/Array each. -
#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.
- #namespace_stackable_with_hash(key) ⇒ Object
- #point_in_time_copies ⇒ Object
-
#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).
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
#namespace ⇒ Object (readonly)
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 (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_stackable ⇒ Object (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_stackable ⇒ Object (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 |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
8 9 10 |
# File 'lib/grape/util/inheritable_setting.rb', line 8 def parent @parent end |
#route ⇒ Object (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
.global ⇒ Object
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.
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_class ⇒ Object
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 |
#global ⇒ Object
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.
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_copies ⇒ Object
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_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.
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_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.
82 83 84 |
# File 'lib/grape/util/inheritable_setting.rb', line 82 def route_end @route = {} end |
#to_hash ⇒ Object
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 |