Class: RichEngine::Enum
- Inherits:
-
Object
- Object
- RichEngine::Enum
- Defined in:
- lib/rich_engine/enum.rb,
lib/rich_engine/enum/mixin.rb,
lib/rich_engine/enum/value.rb
Overview
An ergonomic, comparable enum with query methods. Options may be given as an array (auto-indexed from 0) or as a hash mapping each option to its value. A reader method is defined for every option that returns an Value.
Defined Under Namespace
Instance Attribute Summary collapse
- #name ⇒ Symbol, Hash readonly
- #options ⇒ Symbol, Hash readonly
Instance Method Summary collapse
-
#[](option) ⇒ Object
Looks up the value of an option.
-
#initialize(name, options) ⇒ Enum
constructor
Builds an enum and defines a reader method for each option.
Constructor Details
#initialize(name, options) ⇒ Enum
Builds an enum and defines a reader method for each option.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rich_engine/enum.rb', line 27 def initialize(name, ) @name = name @options = if .respond_to? :each_pair else .map.with_index.to_h end @options.each_pair do |option, _value| define_singleton_method(option) do Enum::Value.new(enum: self, selected: option) end end freeze end |
Instance Attribute Details
#name ⇒ Symbol, Hash (readonly)
20 21 22 |
# File 'lib/rich_engine/enum.rb', line 20 def name @name end |
#options ⇒ Symbol, Hash (readonly)
20 21 22 |
# File 'lib/rich_engine/enum.rb', line 20 def @options end |
Instance Method Details
#[](option) ⇒ Object
Looks up the value of an option.
49 50 51 |
# File 'lib/rich_engine/enum.rb', line 49 def [](option) @options.fetch(option) end |