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

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

Instance Method Summary collapse

Constructor Details

#initialize(constants) ⇒ ConstantMap

Returns a new instance of ConstantMap.

Parameters:

  • constants (Hash)

    The constants to define.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 15

def initialize(constants)
  super()

  @to_h = constants.dup

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

    define_reader(const_name)
  end
end

Instance Attribute Details

#to_hHash (readonly) Also known as: all

Returns The defined constants.

Returns:

  • (Hash)

    The defined constants.



68
69
70
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 68

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.

Yield Parameters:

  • key (Symbol)

    The name of the constant.

  • value (Object)

    The value of the constant.



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

#each_key {|key| ... } ⇒ Object

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

Yield Parameters:

  • key (Symbol)

    The name of the constant.



# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 42

#each_pair {|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 constant.

  • value (Object)

    The value of the constant.



# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 48

#each_value {|value| ... } ⇒ Object

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

Yield Parameters:

  • value (Object)

    The value of the constant.



# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 55

#freezeObject

Freezes the constant map and recursively freezes every constant value using ObjectTools#deep_freeze.



75
76
77
78
79
80
81
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 75

def freeze
  super

  tools.hsh.deep_freeze(@to_h)

  self
end

#keysArray

Returns the names of the defined constants.

Returns:

  • (Array)

    the names of the defined constants.



# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 61

#valuesArray

Returns the values of the defined constants.

Returns:

  • (Array)

    the values of the defined constants.



# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 64