Class: Enum
- Inherits:
-
Object
- Object
- Enum
- Extended by:
- Enumerable
- Includes:
- Comparable
- Defined in:
- lib/nice_enum.rb
Overview
Base class for all Enumerations. To create an Enumeration, subclass this class and add enum calls in the class body. There are many samples in README.rdoc and samples/ .
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the name of the enumeration member.
-
#value ⇒ Object
readonly
Returns the value of the enumeration member.
Class Method Summary collapse
-
.default_value(attr) ⇒ Object
Returns the default value for the attribute
attr
. -
.each ⇒ Object
Enumerates all known enumeration members.
-
.new(value) ⇒ Object
Returns the
Enum
instance for the given value.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares the value of
self
with the value ofother
. -
#eql?(other) ⇒ Boolean
Returns whether the value of
self
and the value ofother
are equal. -
#hash ⇒ Object
Returns the hashcode of
value
. -
#method_missing(sym, *args, &block) ⇒ Object
:nodoc:.
-
#old_respond_to? ⇒ Object
:nodoc:.
-
#respond_to?(sym) ⇒ Boolean
:nodoc:.
-
#to_s ⇒ Object
Returns the name of the enumeration member.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
:nodoc:
69 70 71 |
# File 'lib/nice_enum.rb', line 69 def method_missing(sym, *args, &block) # :nodoc: @value.send(sym, *args, &block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the name of the enumeration member. This is the first parameter passed to enum
, converted to a string.
36 37 38 |
# File 'lib/nice_enum.rb', line 36 def name @name end |
#value ⇒ Object (readonly)
Returns the value of the enumeration member. This is the second parameter passed to enum
.
40 41 42 |
# File 'lib/nice_enum.rb', line 40 def value @value end |
Class Method Details
.default_value(attr) ⇒ Object
Returns the default value for the attribute attr
. If no default value has been set, nil
is returned.
86 87 88 89 |
# File 'lib/nice_enum.rb', line 86 def default_value(attr) @defaults ||= {} @defaults[attr.to_sym] end |
.each ⇒ Object
Enumerates all known enumeration members.
92 93 94 95 |
# File 'lib/nice_enum.rb', line 92 def each @enumvalues ||= {} @enumvalues.each_value { |enumvalue| yield enumvalue } end |
.new(value) ⇒ Object
Returns the Enum
instance for the given value. If no enumeration member has been created for this value, a new instance is created, setting name
to value.to_s
.
79 80 81 82 |
# File 'lib/nice_enum.rb', line 79 def new(value) return @enumvalues[value] if @enumvalues.has_key? value _setup_enumvalue(value, value, allocate) end |
Instance Method Details
#<=>(other) ⇒ Object
Compares the value of self
with the value of other
.
43 44 45 46 |
# File 'lib/nice_enum.rb', line 43 def <=>(other) other = other.value if other.is_a? self.class return value <=> other end |
#eql?(other) ⇒ Boolean
Returns whether the value of self
and the value of other
are equal.
49 50 51 52 |
# File 'lib/nice_enum.rb', line 49 def eql?(other) other = other.value if other.is_a? self.class return value.eql?(other) end |
#hash ⇒ Object
Returns the hashcode of value
.
55 56 57 |
# File 'lib/nice_enum.rb', line 55 def hash value.hash end |
#old_respond_to? ⇒ Object
:nodoc:
64 |
# File 'lib/nice_enum.rb', line 64 alias old_respond_to? respond_to? |
#respond_to?(sym) ⇒ Boolean
:nodoc:
65 66 67 |
# File 'lib/nice_enum.rb', line 65 def respond_to?(sym) # :nodoc: old_respond_to?(sym) || @value.respond_to?(sym) end |
#to_s ⇒ Object
Returns the name of the enumeration member.
60 61 62 |
# File 'lib/nice_enum.rb', line 60 def to_s @name end |