Class: RichEngine::Enum::Value
- Inherits:
-
Object
- Object
- RichEngine::Enum::Value
- Includes:
- Comparable
- Defined in:
- lib/rich_engine/enum/value.rb
Overview
A single selected option of an RichEngine::Enum. Values are comparable (by their underlying option value) and expose a query method per option, e.g. +#idle?+. Only values from the same enum can be compared or are equal.
Instance Attribute Summary collapse
- #enum ⇒ Enum, Symbol readonly
- #selected ⇒ Enum, Symbol readonly
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compares two values from the same enum by their underlying values.
-
#==(other) ⇒ Boolean
Two values are equal when they come from the same enum and have the same selected option.
-
#initialize(enum:, selected:) ⇒ Value
constructor
Builds a value for a selected option and defines a query method per option (e.g. +#idle?+).
-
#value ⇒ Object
The underlying value of the selected option.
Constructor Details
#initialize(enum:, selected:) ⇒ Value
Builds a value for a selected option and defines a query method per option (e.g. +#idle?+).
26 27 28 29 30 31 32 33 34 |
# File 'lib/rich_engine/enum/value.rb', line 26 def initialize(enum:, selected:) @enum = enum @selected = selected check_selected_is_a_valid_option define_query_methods freeze end |
Instance Attribute Details
#enum ⇒ Enum, Symbol (readonly)
17 18 19 |
# File 'lib/rich_engine/enum/value.rb', line 17 def enum @enum end |
#selected ⇒ Enum, Symbol (readonly)
17 18 19 |
# File 'lib/rich_engine/enum/value.rb', line 17 def selected @selected end |
Instance Method Details
#<=>(other) ⇒ Integer
Compares two values from the same enum by their underlying values.
48 49 50 51 52 |
# File 'lib/rich_engine/enum/value.rb', line 48 def <=>(other) raise ArgumentError, "Can't compare values from different enums" if enum != other.enum value <=> other.value end |
#==(other) ⇒ Boolean
Two values are equal when they come from the same enum and have the same selected option.
59 60 61 62 63 |
# File 'lib/rich_engine/enum/value.rb', line 59 def ==(other) return @enum == other.enum && selected == other.selected if other.is_a? self.class super end |
#value ⇒ Object
The underlying value of the selected option.
39 40 41 |
# File 'lib/rich_engine/enum/value.rb', line 39 def value @enum[@selected] end |