Class: Context::EnumeratedType
Overview
Class that holds a series of constants that act as an enumerated type. The first ID added to the type is set to the value 0 (or at the value specified in start_at). each subsequent ID added to the type is automatically incremented
Direct Known Subclasses
Class Method Summary collapse
-
.add(id) ⇒ Object
Add a single ID to the enumerated type Usually called like: add MY_ID.
-
.method_missing(id) ⇒ Object
Retrieve the value of the ID If the ID is defined, return it’s value.
-
.responds_to?(id) ⇒ Boolean
Returns true if the ID has been defined in the enumerated type.
-
.start_at(n) ⇒ Object
Set the value that the first ID should start at.
-
.symbols(array) ⇒ Object
Add a group of IDs to the enumerated type.
Class Method Details
.add(id) ⇒ Object
Add a single ID to the enumerated type Usually called like:
add MY_ID
20 21 22 23 24 |
# File 'lib/Context/EnumeratedType.rb', line 20 def add(id) @nextValue ||= 0 const_set(id, @nextValue) @nextValue += 1 end |
.method_missing(id) ⇒ Object
Retrieve the value of the ID If the ID is defined, return it’s value.
44 45 46 47 |
# File 'lib/Context/EnumeratedType.rb', line 44 def method_missing(id) name = id.id2name self.const_get(name) if self.const_defined?(name) end |
.responds_to?(id) ⇒ Boolean
Returns true if the ID has been defined in the enumerated type.
37 38 39 40 |
# File 'lib/Context/EnumeratedType.rb', line 37 def responds_to?(id) name = id.id2name return true if self.const_defined?(name) end |
.start_at(n) ⇒ Object
Set the value that the first ID should start at. Note: This must called before add() or symbols()
13 14 15 |
# File 'lib/Context/EnumeratedType.rb', line 13 def start_at(n) @nextValue = n end |
.symbols(array) ⇒ Object
Add a group of IDs to the enumerated type. This method takes an array. Usually called like:
symbols [ID1, ID2, ID3,...]
30 31 32 33 34 |
# File 'lib/Context/EnumeratedType.rb', line 30 def symbols(array) array.each do |id| add(id) end end |