Class: Arf::RPC::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/arf/rpc/enum.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/arf/rpc/enum.rb', line 22

def options
  @options
end

Class Method Details

.option(**kwargs) ⇒ Object



6
7
8
9
# File 'lib/arf/rpc/enum.rb', line 6

def self.option(**kwargs)
  @options ||= {}
  kwargs.each { |k, v| @options[k.to_sym] = v }
end

.option_by_name(name) ⇒ Object



11
12
13
14
# File 'lib/arf/rpc/enum.rb', line 11

def self.option_by_name(name)
  @options ||= {}
  @options[name.to_sym]
end

.option_by_value(value) ⇒ Object



16
17
18
19
# File 'lib/arf/rpc/enum.rb', line 16

def self.option_by_value(value)
  @options ||= {}
  @options.find { _2 == value }&.first
end

.to_i(value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/arf/rpc/enum.rb', line 25

def self.to_i(value)
  case value
  when Symbol, String
    option_by_name(value) or raise(ArgumentError, "Invalid value #{value.inspect} for #{self}")
  when Integer
    option_by_value(value) or raise(ArgumentError, "Invalid value #{value.inspect} for #{self}")
    value
  else
    raise ArgumentError, "Invalid value type #{value.class}. Expected Symbol, String or Integer."
  end
end

.to_sym(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/arf/rpc/enum.rb', line 37

def self.to_sym(value)
  case value
  when Symbol, String
    value = value.to_sym
    option_by_name(value) or raise(ArgumentError, "Invalid value #{value.inspect} for #{self}")
    value
  when Integer
    option_by_value(value) or raise(ArgumentError, "Invalid value #{value.inspect} for #{self}")
  else
    raise ArgumentError, "Invalid value type #{value.class}. Expected Symbol, String or Integer."
  end
end