Class: Protobuf::Field::FieldProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/message/field.rb

Instance Method Summary collapse

Constructor Details

#initialize(message_class, rule, type, name, tag, opts = {}) ⇒ FieldProxy

Returns a new instance of FieldProxy.



196
197
198
199
# File 'lib/protobuf/message/field.rb', line 196

def initialize(message_class, rule, type, name, tag, opts={})
  @message_class, @rule, @type, @name, @tag, @opts =
    message_class, rule, type, name, tag, opts
end

Instance Method Details

#ready?Boolean

Returns:

  • (Boolean)


201
# File 'lib/protobuf/message/field.rb', line 201

def ready?; false end

#setupObject



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/protobuf/message/field.rb', line 203

def setup
  type = typename_to_class @message_class, @type
  field_class =
    if type.superclass == Protobuf::Enum
      Protobuf::Field::EnumField
    elsif type.superclass == Protobuf::Message
      Protobuf::Field::MessageField
    else
      raise TypeError.new(type.inspect)
    end
  field_class.new @message_class, @rule, type, @name, @tag, @opts
end

#typename_to_class(message_class, type) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/protobuf/message/field.rb', line 216

def typename_to_class(message_class, type)
  suffix = type.to_s.split('::')
  modules = message_class.to_s.split('::')
  args = (Object.method(:const_defined?).arity == 1) ? [] : [nil, false]
  while
    mod = modules.empty? ? Object : eval(modules.join('::'))
    mod = suffix.inject(mod) {|m, s|
      args[0] = s
      m and m.const_defined?(*args) and m.const_get(s)
    }
    break if mod
    raise NameError.new("type not found: #{type}", type) if modules.empty?
    modules.pop
  end
  mod
end