Class: SwifterEnum::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/swifter_enum/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Base

Returns a new instance of Base.



33
34
35
# File 'lib/swifter_enum/base.rb', line 33

def initialize(value)
  @value = value&.to_sym
end

Class Attribute Details

.valuesObject

Returns the value of attribute values.



4
5
6
# File 'lib/swifter_enum/base.rb', line 4

def values
  @values
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



31
32
33
# File 'lib/swifter_enum/base.rb', line 31

def value
  @value
end

Class Method Details

.allObject



53
54
55
# File 'lib/swifter_enum/base.rb', line 53

def self.all
  values.keys.map { |value| new(value) }
end

.all_casesObject



18
19
20
# File 'lib/swifter_enum/base.rb', line 18

def all_cases
  @values.keys.map { |key| new(key) }
end

.set_values(input) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/swifter_enum/base.rb', line 6

def set_values(input)
  case input
  when Hash
    @values = input.freeze
  when Array
    validate_array_elements!(input)
    @values = input.map { |item| [item.to_sym, item.to_s] }.to_h.freeze
  else
    raise ArgumentError, "Input must be a Hash or an Array of symbols or strings"
  end
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/swifter_enum/base.rb', line 37

def ==(other)
  if other.is_a?(Symbol) || other.is_a?(String)
    value.to_s == other.to_s
  else
    other.instance_of?(self.class) && value == other.value
  end
end

#tObject



45
46
47
# File 'lib/swifter_enum/base.rb', line 45

def t
  I18n.t("swifter_enum.#{self.class.name.demodulize.underscore}.#{value}")
end

#to_sObject



49
50
51
# File 'lib/swifter_enum/base.rb', line 49

def to_s
  value.to_s
end