Class: SimpleEnum::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_enum/enum.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hash) ⇒ Enum

Returns a new instance of Enum.



7
8
9
10
# File 'lib/simple_enum/enum.rb', line 7

def initialize(name, hash)
  @name = name.to_s
  @hash = hash
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/simple_enum/enum.rb', line 5

def hash
  @hash
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/simple_enum/enum.rb', line 5

def name
  @name
end

Instance Method Details

#each_pair(&block) ⇒ Object Also known as: each



32
33
34
# File 'lib/simple_enum/enum.rb', line 32

def each_pair(&block)
  hash.each_pair(&block)
end

#fetch(key) ⇒ Object



28
29
30
# File 'lib/simple_enum/enum.rb', line 28

def fetch(key)
  value(key) || raise("Key \"#{key}\" not found")
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/simple_enum/enum.rb', line 12

def include?(key)
  hash.key?(key.to_s) || hash.value?(key)
end

#key(value) ⇒ Object



16
17
18
19
# File 'lib/simple_enum/enum.rb', line 16

def key(value)
  key = hash.key(value)
  key.to_sym if key
end

#keysObject



41
42
43
# File 'lib/simple_enum/enum.rb', line 41

def keys
  hash.keys
end

#map(&block) ⇒ Object



37
38
39
# File 'lib/simple_enum/enum.rb', line 37

def map(&block)
  hash.map(&block)
end

#to_sObject



54
55
56
# File 'lib/simple_enum/enum.rb', line 54

def to_s
  name
end

#value(key) ⇒ Object Also known as: []



21
22
23
24
25
# File 'lib/simple_enum/enum.rb', line 21

def value(key)
  value = hash[key.to_s]
  value = key if hash.value?(key)
  value
end

#valuesObject



45
46
47
# File 'lib/simple_enum/enum.rb', line 45

def values
  hash.values
end

#values_at(*keys) ⇒ Object



49
50
51
52
# File 'lib/simple_enum/enum.rb', line 49

def values_at(*keys)
  keys = keys.map(&:to_s)
  hash.values_at(*keys)
end