Module: BaseEnumz
- Defined in:
- lib/enumz/base_enumz.rb
Constant Summary collapse
- RESERVED_METHOD_NAMES =
list of ruby module method names that cannot be overridden i.e. overridding enum.class is too destructive
[:class, :object_id, :method_missing, :instance_eval, :send, :should_not, :nil?, :const_get, :const_missing, :inspect, :to_s]
Instance Attribute Summary collapse
-
#data_elements ⇒ Object
readonly
Returns the value of attribute data_elements.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#[](value) ⇒ Object
return the corresponding enum name for a given value.
-
#method_missing(meth, *args, &block) ⇒ Object
create a method for each enum name.
-
#names ⇒ Object
return list of enum names.
-
#values ⇒ Object
return list of enum values.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
create a method for each enum name
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/enumz/base_enumz.rb', line 28 def method_missing(meth, *args, &block) if data_elements[meth.to_s.to_sym] self.instance_eval <<-EVAL def #{meth} data_elements[:#{meth}] end EVAL __send__(meth, *args, &block) else super end end |
Instance Attribute Details
#data_elements ⇒ Object (readonly)
Returns the value of attribute data_elements.
2 3 4 |
# File 'lib/enumz/base_enumz.rb', line 2 def data_elements @data_elements end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'lib/enumz/base_enumz.rb', line 2 def @options end |
Instance Method Details
#[](value) ⇒ Object
return the corresponding enum name for a given value
22 23 24 25 |
# File 'lib/enumz/base_enumz.rb', line 22 def [](value) @inverted_data_elements ||= data_elements.invert @inverted_data_elements[value] end |
#names ⇒ Object
return list of enum names
12 13 14 |
# File 'lib/enumz/base_enumz.rb', line 12 def names @names ||= data_elements.keys end |
#values ⇒ Object
return list of enum values
17 18 19 |
# File 'lib/enumz/base_enumz.rb', line 17 def values @values ||= data_elements.values end |