Class: SleepingKingStudios::Tools::Toolbox::ConstantMap
- Inherits:
-
Module
- Object
- Module
- SleepingKingStudios::Tools::Toolbox::ConstantMap
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/sleeping_king_studios/tools/toolbox/constant_map.rb
Overview
Provides an enumerable interface for defining a group of constants.
Instance Attribute Summary collapse
-
#to_h ⇒ Hash
(also: #all)
readonly
The defined constants.
Instance Method Summary collapse
-
#each {|key, value| ... } ⇒ Object
Iterates through the defined constants, yielding the name and value of each constant to the block.
-
#each_key {|key| ... } ⇒ Object
Iterates through the defined constants, yielding the name of each constant to the block.
-
#each_pair {|key, value| ... } ⇒ Object
Iterates through the defined constants, yielding the name and value of each constant to the block.
-
#each_value {|value| ... } ⇒ Object
Iterates through the defined constants, yielding the value of each constant to the block.
-
#freeze ⇒ self
Freezes the constant map and recursively freezes every constant value.
-
#initialize(constants = {}, **keywords) ⇒ ConstantMap
constructor
A new instance of ConstantMap.
-
#keys ⇒ Array
The names of the defined constants.
-
#values ⇒ Array
The values of the defined constants.
Constructor Details
#initialize(constants) ⇒ ConstantMap #initialize(**constants) ⇒ ConstantMap
Returns a new instance of ConstantMap.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 35 def initialize(constants = {}, **keywords) super() @to_h = constants.merge(**keywords).transform_keys(&:to_sym).freeze @to_h.each do |const_name, const_value| const_set(const_name, const_value) define_reader(const_name) end end |
Instance Attribute Details
#to_h ⇒ Hash (readonly) Also known as: all
Returns The defined constants.
88 89 90 |
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 88 def to_h @to_h end |
Instance Method Details
#each {|key, value| ... } ⇒ Object
Iterates through the defined constants, yielding the name and value of each constant to the block.
|
|
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 55
|
#each_key {|key| ... } ⇒ Object
Iterates through the defined constants, yielding the name of each constant to the block.
|
|
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 62
|
#each_pair {|key, value| ... } ⇒ Object
Iterates through the defined constants, yielding the name and value of each constant to the block.
|
|
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 68
|
#each_value {|value| ... } ⇒ Object
Iterates through the defined constants, yielding the value of each constant to the block.
|
|
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 75
|
#freeze ⇒ self
Freezes the constant map and recursively freezes every constant value.
96 97 98 99 100 101 102 |
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 96 def freeze super tools.hsh.deep_freeze(@to_h) self end |
#keys ⇒ Array
Returns the names of the defined constants.
|
|
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 81
|
#values ⇒ Array
Returns the values of the defined constants.
|
|
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 84
|