Class: SleepingKingStudios::Tools::Toolbox::ConstantMap

Inherits:
Module
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/toolbox/constant_map.rb

Overview

Provides an enumerable interface for defining a group of constants.

Instance Method Summary collapse

Constructor Details

#initialize(constants) ⇒ ConstantMap

Returns a new instance of ConstantMap.

Parameters:

  • constants (Hash)

    The constants to define.



10
11
12
13
14
15
16
17
18
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 10

def initialize(constants)
  super()

  constants.each do |const_name, const_value|
    const_set(const_name, const_value)

    define_reader(const_name)
  end
end

Instance Method Details

#allHash

Returns a hash with the names and values of the defined constants.

Returns:

  • (Hash)

    The defined constants.



23
24
25
26
27
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 23

def all
  constants.each.with_object({}) do |const_name, hsh|
    hsh[const_name] = const_get(const_name)
  end
end

#each {|key, value| ... } ⇒ Object

Iterates through the defined constants, yielding the name and value of each constant to the block.

Yield Parameters:

  • key (Symbol)

    The name of the symbol.

  • value (Object)

    The value of the symbol.



34
35
36
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 34

def each(&block)
  all.each(&block)
end

#freezeObject

Freezes the constant map and recursively freezes every constant value using ObjectTools#deep_freeze. Also pre-emptively defines any reader methods that are not already undefined.



43
44
45
46
47
48
49
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 43

def freeze
  constants.each do |const_name|
    object_tools.deep_freeze const_get(const_name)
  end

  super
end