Class: Dieses::Support::Enum

Inherits:
Object
  • Object
show all
Extended by:
ClassAttribute
Defined in:
lib/dieses/support/enum.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassAttribute

define, defines

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

#valueObject

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: ==

Returns:

  • (Boolean)


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

#hashObject



30
31
32
# File 'lib/dieses/support/enum.rb', line 30

def hash
  self.class.hash ^ values.hash ^ value.hash
end

#to_sObject



34
35
36
# File 'lib/dieses/support/enum.rb', line 34

def to_s
  value.to_s
end