Class: Enumeration::Base
- Inherits:
-
Struct
- Object
- Struct
- Enumeration::Base
- Defined in:
- lib/enumerations.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
Class Method Summary collapse
-
.find(id) ⇒ Object
lookup a specific enum.
- .values(values) ⇒ Object
Instance Method Summary collapse
-
#==(object) ⇒ Object
FIXME for some reason this doesn’t work for case..when expressions.
- #method_missing(method_name, *args) ⇒ Object
- #singleton_class ⇒ Object
- #to_i ⇒ Object
- #to_param ⇒ Object
-
#to_s ⇒ Object
TODO alias method??.
- #to_sym ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/enumerations.rb', line 121 def method_missing(method_name, *args) symbol = method_name.to_s.gsub(/[?]$/, '') if self.class.find(symbol) && method_name.to_s.last=="?" self.symbol == symbol.to_sym else super(method_name, args) end end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id
50 51 52 |
# File 'lib/enumerations.rb', line 50 def id @id end |
#name ⇒ Object
Returns the value of attribute name
50 51 52 |
# File 'lib/enumerations.rb', line 50 def name @name end |
#symbol ⇒ Object
Returns the value of attribute symbol
50 51 52 |
# File 'lib/enumerations.rb', line 50 def symbol @symbol end |
Class Method Details
.find(id) ⇒ Object
lookup a specific enum
85 86 87 88 89 90 91 |
# File 'lib/enumerations.rb', line 85 def self.find(id) if id.is_a?(Fixnum) self.id_index[id] elsif id.is_a?(Symbol) || id.is_a?(String) self.symbol_index[id.to_sym] end end |
.values(values) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/enumerations.rb', line 59 def self.values(values) # all values self.all = [] # for id based lookup self.id_index = {} # for symbol based lookup self.symbol_index = {} values.each_pair do |symbol, attributes| attributes[:name] ||= symbol.to_s.humanize object = self.new(attributes[:id], attributes[:name], symbol) self.singleton_class.send(:define_method, symbol) do object end raise "Duplicate id #{attributes[:id]}" if self.id_index[attributes[:id]] raise "Duplicate symbol #{symbol}" if self.symbol_index[symbol] self.id_index[attributes[:id]] = object self.symbol_index[symbol] = object self.all << object end end |
Instance Method Details
#==(object) ⇒ Object
FIXME for some reason this doesn’t work for case..when expressions
94 95 96 97 98 99 100 101 102 |
# File 'lib/enumerations.rb', line 94 def ==(object) if object.is_a?(Fixnum) object == self.id elsif object.is_a?(Symbol) object == self.symbol elsif object.is_a?(self.class) object.id == self.id end end |
#singleton_class ⇒ Object
53 54 55 56 57 |
# File 'lib/enumerations.rb', line 53 def singleton_class class << self self end end |
#to_i ⇒ Object
109 110 111 |
# File 'lib/enumerations.rb', line 109 def to_i id end |
#to_param ⇒ Object
117 118 119 |
# File 'lib/enumerations.rb', line 117 def to_param id end |
#to_s ⇒ Object
TODO alias method??
105 106 107 |
# File 'lib/enumerations.rb', line 105 def to_s name end |
#to_sym ⇒ Object
113 114 115 |
# File 'lib/enumerations.rb', line 113 def to_sym symbol end |