Sha256: 135a0150059ca714e6c43d0d9f10e1e1f75e47d9d848eb85f6482404603b6a47

Contents?: true

Size: 1.13 KB

Versions: 69

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

class ReeEnum::Value
  attr_reader :enum_class, :enum_name, :value, :label, :number

  contract(Class, Symbol, Symbol, Integer, Nilor[String] => Any)
  def initialize(enum_class, enum_name, value, number, label)
    @enum_class = enum_class
    @enum_name = enum_name
    @value = value
    @label = label
    @number = number
  end

  def to_s
    @value.to_s
  end

  def to_sym
    @value
  end

  def to_i
    @number
  end

  def as_json(*args)
    to_s
  end

  def label
    @label
  end

  contract(Or[ReeEnum::Value, Symbol, Integer, Any] => Bool)
  def ==(compare)
    if compare.is_a?(self.class)
      @value == compare.value
    else
      @value == compare || @number == compare
    end
  end

  contract(Or[ReeEnum::Value, String, Integer] => Bool)
  def <=>(other)
    if other.is_a?(self.class)
      @number <=> other.number
    elsif other.is_a?(Symbol)
      @value == other
    elsif other.is_a?(Integer)
      @number == other
    else
      raise ArgumentError.new("unable to compare ReeEnum::Value with other classes")
    end
  end
  
  def inspect
    "#{enum_class.name}##{@value.to_s}"
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
ree_lib-1.0.69 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.65 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.64 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.63 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.62 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.61 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.60 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.59 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.58 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.57 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.56 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.55 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.54 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.53 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.52 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.51 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
ree_lib-1.0.50 lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb