Class: Wallaby::ClassHash
- Inherits:
-
Object
- Object
- Wallaby::ClassHash
- Defined in:
- lib/wallaby/class_hash.rb
Overview
This is a constant-safe hash that stores Class key/value as String and returns value as Class if it was a Class.
It can be used for global methods (e.g. Map.mode_map) which cache the computed result, so that when Rails reloads, it won’t complain that old Class constants still exist in ObjectSpace (see github.com/wallaby-rails/wallaby/issues/181).
“‘ A copy of SupportAdmin::ApplicationAuthorizer has been removed from the module tree but is still active! “`
As there won’t be any Class constants being stored, they will converted to String in #internal Hash.
Instance Attribute Summary collapse
-
#internal ⇒ Hash
readonly
The hash to store Class keys/values as String.
-
#origin ⇒ Hash
readonly
The original hash.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare ##origin with other.
-
#[](key) ⇒ Object
Return the value for the given key, and convert the value back to Class if it was a Class.
-
#[]=(key, value) ⇒ Object
Save the key/value to the #internal hash, and convert the Class key/value to String.
-
#freeze ⇒ ClassHash
Ensure to freeze the #internal.
-
#initialize(hash = {}) ⇒ ClassHash
constructor
A new instance of ClassHash.
-
#key? ⇒ Object
Return if #origin has the given key.
-
#keys ⇒ Object
Return the keys of #origin.
-
#merge(other) ⇒ ClassHash
New Class hash.
-
#select(&block) ⇒ ClassHash
New Class hash.
-
#values ⇒ Object
Return the values of #origin.
Constructor Details
#initialize(hash = {}) ⇒ ClassHash
Returns a new instance of ClassHash.
22 23 24 25 26 27 |
# File 'lib/wallaby/class_hash.rb', line 22 def initialize(hash = {}) @internal = (hash || {}) .transform_keys { |klass| class_name_of(klass) } .transform_values { |klass| class_name_of(klass) } end |
Instance Attribute Details
#internal ⇒ Hash (readonly)
Returns The hash to store Class keys/values as String.
19 20 21 |
# File 'lib/wallaby/class_hash.rb', line 19 def internal @internal end |
#origin ⇒ Hash (readonly)
Returns The original hash.
31 32 33 34 35 36 37 |
# File 'lib/wallaby/class_hash.rb', line 31 def origin # NOTE: DO NOT cache it by using instance variable! @internal .transform_keys { |klass| to_class(klass) } .transform_values { |klass| to_class(klass) } .reject { |k, v| k.nil? || v.nil? } end |
Instance Method Details
#==(other) ⇒ Object
Compare ##origin with other.
53 |
# File 'lib/wallaby/class_hash.rb', line 53 delegate :==, to: :origin |
#[](key) ⇒ Object
Return the value for the given key, and convert the value back to Class if it was a Class
61 62 63 |
# File 'lib/wallaby/class_hash.rb', line 61 def [](key) to_class @internal[class_name_of(key)] end |
#[]=(key, value) ⇒ Object
Save the key/value to the #internal hash, and convert the Class key/value to String
56 57 58 |
# File 'lib/wallaby/class_hash.rb', line 56 def []=(key, value) @internal[class_name_of(key)] = class_name_of(value) end |
#freeze ⇒ ClassHash
Ensure to freeze the #internal
78 79 80 81 |
# File 'lib/wallaby/class_hash.rb', line 78 def freeze @internal.freeze super end |
#key? ⇒ Object
Return if #origin has the given key.
41 |
# File 'lib/wallaby/class_hash.rb', line 41 delegate :key?, to: :origin |
#keys ⇒ Object
Return the keys of #origin.
45 |
# File 'lib/wallaby/class_hash.rb', line 45 delegate :keys, to: :origin |
#merge(other) ⇒ ClassHash
Returns new Class hash.
67 68 69 |
# File 'lib/wallaby/class_hash.rb', line 67 def merge(other) self.class.new origin.merge(other.try(:origin) || other) end |
#select(&block) ⇒ ClassHash
Returns new Class hash.
72 73 74 |
# File 'lib/wallaby/class_hash.rb', line 72 def select(&block) self.class.new origin.select(&block) end |
#values ⇒ Object
Return the values of #origin.
49 |
# File 'lib/wallaby/class_hash.rb', line 49 delegate :values, to: :origin |