Class: Dieses::Support::Enum
- Inherits:
-
Object
- Object
- Dieses::Support::Enum
- Extended by:
- ClassAttribute
- Defined in:
- lib/dieses/support/enum.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.of(*members) ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(value) ⇒ Enum
constructor
A new instance of Enum.
- #to_s ⇒ Object
Methods included from ClassAttribute
Constructor Details
#initialize(value) ⇒ Enum
Returns a new instance of Enum.
12 13 14 |
# File 'lib/dieses/support/enum.rb', line 12 def initialize(value) self.value = value end |
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
10 11 12 |
# File 'lib/dieses/support/enum.rb', line 10 def value @value end |
Class Method Details
.of(*members) ⇒ Object
rubocop:disable Metrics/MethodLength
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dieses/support/enum.rb', line 41 def of(*members) # rubocop:disable Metrics/MethodLength values Set.new(members.map(&:to_sym)).freeze members.each do |member| define_method("#{member}?") do value == member end end Class.new(self) do def self.call(value) new(value) end define_singleton_method(:default) { members.first } end end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
22 23 24 25 26 |
# File 'lib/dieses/support/enum.rb', line 22 def eql?(other) return false unless other.is_a? self.class value == other.value end |
#hash ⇒ Object
30 31 32 |
# File 'lib/dieses/support/enum.rb', line 30 def hash self.class.hash ^ values.hash ^ value.hash end |
#to_s ⇒ Object
34 35 36 |
# File 'lib/dieses/support/enum.rb', line 34 def to_s value.to_s end |