Module: TypesafeEnum::ClassMethods

Includes:
Enumerable
Included in:
Base
Defined in:
lib/typesafe_enum/class_methods.rb

Overview

Class methods for {TypesafeEnum{TypesafeEnum::Base}.

Instance Method Summary collapse

Instance Method Details

#each {|self| ... } ⇒ Enumerator<self>

Iterates over the set of enum instances

Yields:

  • (self)

    Each instance of this enum, in declaration order

Returns:

  • (Enumerator<self>)

    All instances of this enum, in declaration order



21
22
23
# File 'lib/typesafe_enum/class_methods.rb', line 21

def each(&block)
  to_a.each(&block)
end

#each_key {|Enumerator<Symbol>| ... } ⇒ Enumerator<Symbol>

Iterates over the set of keys of all instances (#keys)

Yields:

  • (Enumerator<Symbol>)

    Each key of each instance of this enum, in declaration order

Returns:

  • (Enumerator<Symbol>)


40
41
42
# File 'lib/typesafe_enum/class_methods.rb', line 40

def each_key(&block)
  keys.each(&block)
end

#each_value {|Enumerator<Object>| ... } ⇒ Enumerator<Object>

Iterates over the set of values of all instances (#values)

Yields:

  • (Enumerator<Object>)

    Each value of each instance of this enum, in declaration order

Returns:

  • (Enumerator<Object>)


47
48
49
# File 'lib/typesafe_enum/class_methods.rb', line 47

def each_value(&block)
  values.each(&block)
end

#find_by_key(key) ⇒ self?

Looks up an enum instance based on its key

Parameters:

  • key (Symbol)

    the key to look up

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



54
55
56
# File 'lib/typesafe_enum/class_methods.rb', line 54

def find_by_key(key)
  by_key[key]
end

#find_by_ord(ord) ⇒ self?

Looks up an enum instance based on its ordinal

Parameters:

  • ord (Integer)

    the ordinal to look up

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



96
97
98
99
100
# File 'lib/typesafe_enum/class_methods.rb', line 96

def find_by_ord(ord)
  return nil if ord > size || ord.negative?

  as_array[ord]
end

#find_by_value(value) ⇒ self?

Looks up an enum instance based on its value

Parameters:

  • value (Object)

    the value to look up

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



61
62
63
# File 'lib/typesafe_enum/class_methods.rb', line 61

def find_by_value(value)
  by_value[value]
end

#find_by_value!(value) ⇒ self, EnumValidationError

Looks up an enum instance based on its value

Parameters:

  • value (Object)

    the value to look up

Returns:

  • (self, EnumValidationError)

    the corresponding enum instance, or throws #EnumValidationError

Raises:



68
69
70
71
72
73
# File 'lib/typesafe_enum/class_methods.rb', line 68

def find_by_value!(value)
  valid = find_by_value(value)
  return valid unless valid.nil?

  raise Exceptions::EnumValidationError, "#{class_name}: #{value} is absurd"
end

#find_by_value_str(value_str) ⇒ self?

Looks up an enum instance based on the string representation of its value

Parameters:

  • value_str (String)

    the string form of the value

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



78
79
80
81
# File 'lib/typesafe_enum/class_methods.rb', line 78

def find_by_value_str(value_str)
  value_str = value_str.to_s
  by_value_str[value_str]
end

#find_by_value_str!(value_str) ⇒ self, EnumValidationError

Looks up an enum instance based on the string representation of its value

Parameters:

  • value_str (String)

    the string form of the value

Returns:

  • (self, EnumValidationError)

    the corresponding enum instance, or throws #EnumValidationError

Raises:



86
87
88
89
90
91
# File 'lib/typesafe_enum/class_methods.rb', line 86

def find_by_value_str!(value_str)
  valid = find_by_value_str(value_str)
  return valid unless valid.nil?

  raise Exceptions::EnumValidationError, "#{class_name}: #{value_str} is absurd"
end

#keysEnumerator<Symbol>

The set of all keys of all enum instances

Returns:

  • (Enumerator<Symbol>)

    All keys of all enums, in declaration order



27
28
29
# File 'lib/typesafe_enum/class_methods.rb', line 27

def keys
  to_a.map(&:key)
end

#sizeInteger

Returns the number of enum instances

Returns:

  • (Integer)

    the number of instances



14
15
16
# File 'lib/typesafe_enum/class_methods.rb', line 14

def size
  as_array.size
end

#to_aArray<self>

Returns an array of the enum instances in declaration order

Returns:

  • (Array<self>)

    All instances of this enum, in declaration order



8
9
10
# File 'lib/typesafe_enum/class_methods.rb', line 8

def to_a
  as_array.dup
end

#valuesEnumerator<Object>

The set of all values of all enum instances

Returns:

  • (Enumerator<Object>)

    All values of all enums, in declaration order



33
34
35
# File 'lib/typesafe_enum/class_methods.rb', line 33

def values
  to_a.map(&:value)
end