Class: Wallaby::ClassHash

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ ClassHash

Returns a new instance of ClassHash.

Parameters:

  • hash (Hash) (defaults to: {})


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

#internalHash (readonly)

Returns The hash to store Class keys/values as String.

Returns:

  • (Hash)

    The hash to store Class keys/values as String.



19
20
21
# File 'lib/wallaby/class_hash.rb', line 19

def internal
  @internal
end

#originHash (readonly)

Returns The original hash.

Returns:

  • (Hash)

    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

#freezeClassHash

Ensure to freeze the #internal

Returns:



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

#keysObject

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.

Parameters:

  • other (Hash)

Returns:



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.

Returns:



72
73
74
# File 'lib/wallaby/class_hash.rb', line 72

def select(&block)
  self.class.new origin.select(&block)
end

#valuesObject

Return the values of #origin.



49
# File 'lib/wallaby/class_hash.rb', line 49

delegate :values, to: :origin