Thrift/Ruby: Generate nicer constructors for Thrift structs.
Submitted by William Morgan.
Approved by Kevin Clark.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665446 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc
index 171e623..1e2fa7d 100644
--- a/compiler/cpp/src/generate/t_rb_generator.cc
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -288,6 +288,9 @@
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);
@@ -296,6 +299,18 @@
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();
diff --git a/compiler/cpp/src/generate/t_rb_generator.h b/compiler/cpp/src/generate/t_rb_generator.h
index 0cee5e4..d2b7265 100644
--- a/compiler/cpp/src/generate/t_rb_generator.h
+++ b/compiler/cpp/src/generate/t_rb_generator.h
@@ -56,6 +56,7 @@
void generate_rb_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_rb_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_rb_function_helpers(t_function* tfunction);
+ void generate_rb_simple_constructor(std::ofstream& out, t_struct* tstruct);
void generate_rb_simple_exception_constructor(std::ofstream& out, t_struct* tstruct);
void generate_accessors (std::ofstream& out, t_struct* tstruct);
void generate_field_defns (std::ofstream& out, t_struct* tstruct);