Move Ruby fielded object creation from generated constructor to ThriftStruct

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
This commit is contained in:
David Reiss 2008-02-26 06:40:22 +00:00
parent ebbfbd8395
commit 68ccc699a3
2 changed files with 1 additions and 16 deletions

View 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();

View 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