Module: TypedEnum
- Included in:
- Foo
- Defined in:
- lib/typed_enum.rb
Overview
A TypedEnum can be used to represent a typed, enumerated set of values. A TypedEnum offers the following features:
* Instances are restricted (in the absence of class-opening) to the set of instances defined in
the class definition
* Available values can be enumerated in a consistent order via a call to the
* #values class method
* Instances can be retrieved via class constants
* Instances can be retrieved by parsing a string/symbol
* Instances can be serialized to a string
Basic usage example:
class Color
@enum = lambda {[
RED = self.new(:red),
GREEN = self.new(:green),
BLUE = self.new(:blue)
]}
include TypedEnum
end
Color::RED => #<Color:0x163d59f8 @name=:red>
Color.from_name(:red) => #<Color:0x163d59f8 @name=:red>
Color.from_name('red') => #<Color:0x163d59f8 @name=:red>
Color::RED.name => :red
Color.values => [#<Color:0x163d59f8 @name=:red>, #<Color:0x163d59a8 @name=:green>, #<Color:0x163d5980 @name=:blue>]
Defined Under Namespace
Modules: TypedEnumClassMethods
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
30
31
32
|
# File 'lib/typed_enum.rb', line 30
def name
@name
end
|
Class Method Details
.included(klass) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/typed_enum.rb', line 83
def TypedEnum.included(klass)
super
klass.extend TypedEnumClassMethods
klass.init
klass.private_class_method :new, :allocate
end
|
Instance Method Details
#clone ⇒ Object
35
36
37
|
# File 'lib/typed_enum.rb', line 35
def clone
raise TypeError, "Can't clone instance of TypedEnum #{self.class}"
end
|
#dump(depth = -1)) ⇒ Object
41
42
43
|
# File 'lib/typed_enum.rb', line 41
def dump(depth=-1)
@name.to_s
end
|
#dup ⇒ Object
38
39
40
|
# File 'lib/typed_enum.rb', line 38
def dup
raise TypeError, "Can't dup instance of TypedEnum #{self.class}"
end
|
#initialize(name) ⇒ Object
31
32
33
|
# File 'lib/typed_enum.rb', line 31
def initialize(name)
@name = name
end
|