Module: Protobuf::Optionable::ClassMethods

Defined in:
lib/protobuf/optionable.rb

Instance Method Summary collapse

Instance Method Details

#get_option(name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/protobuf/optionable.rb', line 4

def get_option(name)
  name = name.to_s
  option = optionable_descriptor_class.get_field(name, true)
  fail ArgumentError, "invalid option=#{name}" unless option
  unless option.fully_qualified_name.to_s == name
    # Eventually we'll deprecate the use of simple names of fields completely, but for now make sure people
    # are accessing options correctly. We allow simple names in other places for backwards compatibility.
    fail ArgumentError, "must access option using its fully qualified name: #{option.fully_qualified_name.inspect}"
  end
  value =
    if @_optionable_options.try(:key?, name)
      @_optionable_options[name]
    else
      option.default_value
    end
  if option.type_class < ::Protobuf::Message
    option.type_class.new(value)
  else
    value
  end
end

#get_option!(name) ⇒ Object



26
27
28
# File 'lib/protobuf/optionable.rb', line 26

def get_option!(name)
  get_option(name) if @_optionable_options.try(:key?, name.to_s)
end