Class: Enum

Inherits:
Object
  • Object
show all
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

Flags

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (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

#valueObject (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

.eachObject

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.

Returns:

  • (Boolean)


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

#hashObject

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:

Returns:

  • (Boolean)


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_sObject

Returns the name of the enumeration member.



60
61
62
# File 'lib/nice_enum.rb', line 60

def to_s
	@name
end