33 package com.google.protobuf.jruby;
37 import org.jruby.Ruby;
38 import org.jruby.RubyClass;
39 import org.jruby.RubyModule;
40 import org.jruby.RubyObject;
41 import org.jruby.RubyNumeric;
42 import org.jruby.anno.JRubyClass;
43 import org.jruby.anno.JRubyMethod;
44 import org.jruby.runtime.Block;
45 import org.jruby.runtime.ObjectAllocator;
46 import org.jruby.runtime.ThreadContext;
47 import org.jruby.runtime.builtin.IRubyObject;
49 @JRubyClass(
name =
"EnumDescriptor", include =
"Enumerable")
52 RubyModule mProtobuf = runtime.getClassFromPath(
"Google::Protobuf");
53 RubyClass
cEnumDescriptor = mProtobuf.defineClassUnder(
"EnumDescriptor", runtime.getObject(),
new ObjectAllocator() {
55 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
64 super(runtime, klazz);
77 this.builder = DescriptorProtos.EnumDescriptorProto.newBuilder();
87 @JRubyMethod(
name =
"name")
88 public IRubyObject getName(ThreadContext context) {
99 @JRubyMethod(
name =
"name=")
100 public IRubyObject setName(ThreadContext context, IRubyObject
name) {
103 return context.runtime.getNil();
114 @JRubyMethod(
name =
"add_value")
115 public IRubyObject addValue(ThreadContext context, IRubyObject
name, IRubyObject
number) {
116 DescriptorProtos.EnumValueDescriptorProto.Builder valueBuilder = DescriptorProtos.EnumValueDescriptorProto.newBuilder();
117 valueBuilder.setName(
name.asJavaString());
118 valueBuilder.setNumber(RubyNumeric.num2int(
number));
119 this.builder.addValue(valueBuilder);
120 return context.runtime.getNil();
131 public IRubyObject
each(ThreadContext context, Block block) {
132 Ruby runtime = context.runtime;
134 block.yield(context, runtime.newArray(runtime.newSymbol(enumValueDescriptor.getName()),
135 runtime.newFixnum(enumValueDescriptor.getNumber())));
137 return runtime.getNil();
149 if (this.klazz ==
null) {
150 this.klazz = buildModuleFromDescriptor(context);
163 public DescriptorProtos.EnumDescriptorProto.Builder
getBuilder() {
168 Ruby runtime = context.runtime;
171 RubyModule enumModule = RubyModule.newModule(runtime);
173 enumModule.defineConstant(
value.getName(), runtime.newFixnum(
value.getNumber()));
177 enumModule.defineAnnotatedMethods(
RubyEnum.class);
184 private DescriptorProtos.EnumDescriptorProto.Builder
builder;