Class: OpenStax::Utilities::Enum

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

Class Method Summary collapse

Class Method Details

.[](val) ⇒ Object

When given a numeric value, returns the constant name When given a name, returns the constant value



21
22
23
24
25
26
27
# File 'lib/openstax/utilities/enum.rb', line 21

def self.[](val)
  return self.constants.select{|c| self.const_get(c) == val}.last if val.is_a?(Numeric)
  val_sym = val.to_s.gsub(" ", "_").to_sym.upcase
  self.const_defined?(val_sym) ? self.const_get(val_sym) : \
  (self.const_defined?(val_sym.capitalize) ? self.const_get(val_sym.capitalize) : \
  raise(NameError.new("wrong enum name #{val.to_s}")))
end

.listObject

Humanized list of constants



30
31
32
# File 'lib/openstax/utilities/enum.rb', line 30

def self.list
  self.constants.collect{|c| c.to_s.humanize}
end

.optionsObject

Options ready to be used in a select tag



40
41
42
# File 'lib/openstax/utilities/enum.rb', line 40

def self.options
  self.constants.collect{|c| [c.to_s.humanize, self.const_get(c)]}
end

.valuesObject

List of constant values



35
36
37
# File 'lib/openstax/utilities/enum.rb', line 35

def self.values
  self.constants.collect{|c| self.const_get(c)}
end