Module: Enum
- Defined in:
- lib/iron/enum.rb,
lib/iron/enum/core.rb,
lib/iron/enum/attr_support.rb,
lib/iron/enum/model_support.rb
Overview
Provides enumerated value support for use as magic constants (status flags, types, etc…)
Sample usage:
module Fruit
enum :apple, 1
enum :pear, 2
end
Yields:
Fruit::APPLE => 1
Fruit.name(1) => 'Apple'
Fruit.keys => [:apple, :pear]
etc…
Defined Under Namespace
Modules: AttrSupport, Core, ModelSupport
Constant Summary collapse
- KEY_IDX =
Indexes into our internal enum list
0
- VALUE_IDX =
1
- NAME_IDX =
2
Instance Method Summary collapse
-
#define_enum(*items) ⇒ Object
Legacy method of enum creation.
-
#enum(key, value, name = nil) ⇒ Object
Add an enumerated constant to the given module/class.
Instance Method Details
#define_enum(*items) ⇒ Object
Legacy method of enum creation. Call with a set of arrays, one for each desired enum. Arrays should contain the parameters supported by #enum, e.g. [:key, <value>, “<optional name>”]
26 27 28 29 30 |
# File 'lib/iron/enum.rb', line 26 def define_enum(*items) items.each do |item| enum(*item) end end |
#enum(key, value, name = nil) ⇒ Object
Add an enumerated constant to the given module/class. The key should be a symbol, the value a fixed integer that the symbol represents. The name is an optional user-friendly name for the enum, which will default to a capitalized version of the key.
Sample usage:
module HTTPCode
enum :success, 200
enum :missing, 404
enum :error, 500, 'Server Error'
end
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/iron/enum.rb', line 43 def enum(key, value, name = nil) # Make sure we have our enum stuff in here self.extend(Enum::Core) unless respond_to?(:enum_list) # Validate input raise "Invalid enum key: #{key.inspect}" unless key.is_a?(Symbol) raise "Invalid enum value: #{value.inspect}" unless value.is_a?(Fixnum) raise "Invalid enum name: #{name.inspect}" unless name.nil? || name.is_a?(String) # Set our constant const_set(key.to_s.upcase, value) # Ensure we have a valid name name ||= key.to_s.split('_').collect(&:capitalize).join(' ') # Save to our list enum_list << [key, value, name] end |