Module: Protobug::Enum

Defined in:
lib/protobug/enum.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reserved_rangesObject (readonly)

Returns the value of attribute reserved_ranges.



14
15
16
# File 'lib/protobug/enum.rb', line 14

def reserved_ranges
  @reserved_ranges
end

#valuesObject (readonly)

Returns the value of attribute values.



14
15
16
# File 'lib/protobug/enum.rb', line 14

def values
  @values
end

Class Method Details

.extended(klass) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/protobug/enum.rb', line 5

def self.extended(klass)
  klass.class_eval do
    @values = {}
    @reserved_ranges = []
    extend BaseDescriptor
    include Protobug::Enum::InstanceMethods
  end
end

Instance Method Details

#const_added(name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/protobug/enum.rb', line 16

def const_added(name)
  super
  value = const_get(name)

  const_name = value.name.start_with?("k") ? "K_#{value.name[1..]}" : value.name
  const_name = "K_#{const_name}" unless const_name.match?(/\A[A-Z]/)

  raise "expected #{self}::#{name} to be a #{self}, got #{value.inspect}" unless value.is_a? self
  raise "expected #{value.name} to be #{const_name}" unless name.name == const_name
  raise "duplicate value #{value.inspect}" if values[value.name]

  @values[value.name] = value
  @values[value.value] ||= value
end

#decode(value) ⇒ Object



72
73
74
75
76
77
# File 'lib/protobug/enum.rb', line 72

def decode(value)
  raise "expected Integer, got #{value.inspect}" unless value.is_a? Integer

  # TODO: use fetch instead of []
  values[value] || new("<unknown:#{value}>", value)
end

#decode_json_hash(json, registry: nil, ignore_unknown_fields: false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/protobug/enum.rb', line 55

def decode_json_hash(json, registry: nil, ignore_unknown_fields: false)
  _ = registry
  _ = ignore_unknown_fields

  case json
  when NilClass
    UNSET
  when String
    values[json] || (ignore_unknown_fields && UNSET) ||
      raise(DecodeError, "unknown value: #{json.inspect}")
  when Integer
    values[json] || new("<unknown:#{json}>", json)
  else
    raise ArgumentError, "expected string for #{full_name}, got #{json.inspect}"
  end
end

#defaultObject



79
80
81
82
# File 'lib/protobug/enum.rb', line 79

def default
  # TODO: default is 0 on proto3, first value on proto2
  values.values.first
end

#freezeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/protobug/enum.rb', line 37

def freeze
  # const_added was added in 3.2
  unless RUBY_VERSION >= "3.2"
    constants.each do |n|
      c = const_get(n)
      next unless c.is_a? self

      @values[c.name] ||= c
      @values[c.value] ||= c
    end
  end

  @values.freeze
  @reserved_ranges.freeze
  full_name.freeze
  super
end

#reserved_range(range) ⇒ Object



31
32
33
34
35
# File 'lib/protobug/enum.rb', line 31

def reserved_range(range)
  raise "expected Range, got #{range.inspect}" unless range.is_a? Range

  reserved_ranges << range
end