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.

Examples:

UserRoles = ConstantMap.new(
  {
    GUEST: 'guest',
    USER:  'user',
    ADMIN: 'admin'
  }
)

UserRoles::GUEST
#=> 'guest'

UserRoles.user
#=> 'user'

UserRoles.all
#=> { :GUEST => 'guest', :USER => 'user', :ADMIN => 'admin' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constants) ⇒ ConstantMap



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 33

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



86
87
88
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 86

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 53


#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 60


#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 66


#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 73


#freezeself

Freezes the constant map and recursively freezes every constant value.



94
95
96
97
98
99
100
# File 'lib/sleeping_king_studios/tools/toolbox/constant_map.rb', line 94

def freeze
  super

  tools.hsh.deep_freeze(@to_h)

  self
end

#keysArray



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


#valuesArray



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