Class: Kind::Enum::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/kind/enum/item.rb

Constant Summary collapse

Underscore =
->(arg) do
  str = String(arg).strip
  str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  str.tr!("-", "_")
  str.downcase!
  str
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, val) ⇒ Item

Returns a new instance of Item.



20
21
22
23
24
25
26
27
# File 'lib/kind/enum/item.rb', line 20

def initialize(key, val)
  @value = val.frozen? ? val : val.dup.freeze

  @to_s = Kind.respond_to(key, :to_sym).to_s
  @name = Underscore[key].upcase.freeze
  @to_sym = key.to_sym
  @inspect = ('#<Kind::Enum::Item name=%p to_s=%p value=%p>' % [@name, @to_s, @value]).freeze
end

Instance Attribute Details

#inspectObject (readonly)

Returns the value of attribute inspect.



15
16
17
# File 'lib/kind/enum/item.rb', line 15

def inspect
  @inspect
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/kind/enum/item.rb', line 15

def name
  @name
end

#to_sObject (readonly) Also known as: key, to_str

Returns the value of attribute to_s.



15
16
17
# File 'lib/kind/enum/item.rb', line 15

def to_s
  @to_s
end

#to_symObject (readonly)

Returns the value of attribute to_sym.



15
16
17
# File 'lib/kind/enum/item.rb', line 15

def to_sym
  @to_sym
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/kind/enum/item.rb', line 15

def value
  @value
end

Instance Method Details

#==(arg) ⇒ Object Also known as: ===



29
30
31
# File 'lib/kind/enum/item.rb', line 29

def ==(arg)
  arg == value || arg == to_s || arg == to_sym
end

#to_aryObject



33
34
35
# File 'lib/kind/enum/item.rb', line 33

def to_ary
  [key, value]
end