Class: SleepingKingStudios::Tools::Toolbox::ConstantMap
- Inherits:
-
Module
- Object
- Module
- SleepingKingStudios::Tools::Toolbox::ConstantMap
- 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
-
#all ⇒ Hash
Returns a hash with the names and values of the defined constants.
-
#each {|key, value| ... } ⇒ Object
Iterates through the defined constants, yielding the name and value of each constant to the block.
-
#freeze ⇒ Object
Freezes the constant map and recursively freezes every constant value using ObjectTools#deep_freeze.
-
#initialize(constants) ⇒ ConstantMap
constructor
A new instance of ConstantMap.
Constructor Details
#initialize(constants) ⇒ ConstantMap
Returns a new instance of ConstantMap.
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
#all ⇒ Hash
Returns a hash with the names and values of 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.
34 35 36 |
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 34 def each(&block) all.each(&block) end |
#freeze ⇒ Object
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 |