class OpenStax::Utilities::Enum

Public Class Methods

[](val) click to toggle source

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

# 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
list() click to toggle source

Humanized list of constants

# File lib/openstax/utilities/enum.rb, line 30
def self.list
  self.constants.collect{|c| c.to_s.humanize}
end
options() click to toggle source

Options ready to be used in a select tag

# File lib/openstax/utilities/enum.rb, line 40
def self.options
  self.constants.collect{|c| [c.to_s.humanize, self.const_get(c)]}
end
values() click to toggle source

List of constant values

# File lib/openstax/utilities/enum.rb, line 35
def self.values
  self.constants.collect{|c| self.const_get(c)}
end