Move Ruby fielded object creation from generated constructor to ThriftStruct
authorDavid Reiss <dreiss@apache.org>
Tue, 26 Feb 2008 06:40:22 +0000 (06:40 +0000)
committerDavid Reiss <dreiss@apache.org>
Tue, 26 Feb 2008 06:40:22 +0000 (06:40 +0000)
Rather than the generated code needing to handle simple fielded
creation of objects, the ThriftStruct module constructor is extended to
handle hash arguments. Statements such as

  o = ThriftObject.new :field1 => value1, :field2 => value2, ...

are supported as before, and the string form,

  o = ThriftObject.new "field1" => value1, "field2" => value2, ...

disabled by the previous patch now also works.

Placing this code in the module is also just a cleaner solution.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665502 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_rb_generator.cc
lib/rb/lib/thrift/thrift.rb

index 220a3de..7faa736 100644 (file)
@@ -288,9 +288,6 @@ void t_rb_generator::generate_rb_struct(std::ofstream& out, t_struct* tstruct, b
   if (is_exception) {
     generate_rb_simple_exception_constructor(out, tstruct);
   }
-  else {
-    generate_rb_simple_constructor(out, tstruct);
-  }
 
   generate_accessors(out, tstruct);
   generate_field_defns(out, tstruct);
@@ -299,18 +296,6 @@ void t_rb_generator::generate_rb_struct(std::ofstream& out, t_struct* tstruct, b
   indent(out) << "end" << endl << endl;
 }
 
-void t_rb_generator::generate_rb_simple_constructor(std::ofstream& out, t_struct* tstruct) {
-  const vector<t_field*>& members = tstruct->get_members();
-
-  if (members.size() > 0) {
-    indent(out) << "def initialize(opts={})" << endl;
-    indent_up();
-    indent(out) << "opts.each { |k, v| send(\"#{k}=\", v) }" << endl;
-    indent_down();
-    indent(out) << "end" << endl << endl;
-  }
-}
-
 void t_rb_generator::generate_rb_simple_exception_constructor(std::ofstream& out, t_struct* tstruct) {
   const vector<t_field*>& members = tstruct->get_members();
 
index ab21646..a0a2f7e 100644 (file)
@@ -178,7 +178,7 @@ end
 module ThriftStruct
   def initialize(d={})
     each_field do |fid, type, name, default|
-      instance_variable_set("@#{name}", d[name.to_s] || default)
+      instance_variable_set("@#{name}", d[name.to_s] || d[name.intern] || default)
     end
   end