From 05ac516ffa25613e43e5320e020134750f3754f6 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 18 May 2015 04:30:26 +0800 Subject: [PATCH] clean up ruby sample, remove commented code, bug fix --- .../codegen/languages/RubyClientCodegen.java | 2 +- .../main/resources/ruby/base_object.mustache | 2 +- .../src/main/resources/ruby/model.mustache | 78 ----------------- .../petstore/ruby/lib/model/base_object.rb | 83 ------------------- .../petstore/ruby/models/base_object.rb | 11 +-- .../client/petstore/ruby/models/category.rb | 78 ----------------- samples/client/petstore/ruby/models/order.rb | 78 ----------------- samples/client/petstore/ruby/models/pet.rb | 78 ----------------- samples/client/petstore/ruby/models/tag.rb | 78 ----------------- samples/client/petstore/ruby/models/user.rb | 78 ----------------- 10 files changed, 6 insertions(+), 560 deletions(-) delete mode 100644 samples/client/petstore/ruby/lib/model/base_object.rb diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java index 962f7ea692c1..99f56bf5d415 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java @@ -72,7 +72,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig { supportingFiles.add(new SupportingFile("swagger/response.mustache", "", "lib/swagger/response.rb")); supportingFiles.add(new SupportingFile("swagger/version.mustache", "", "lib/swagger/version.rb")); supportingFiles.add(new SupportingFile("swagger/configuration.mustache", "", "lib/swagger/configuration.rb")); - supportingFiles.add(new SupportingFile("base_object.mustache", "", "lib/model/base_object.rb")); + supportingFiles.add(new SupportingFile("base_object.mustache", "", "models/base_object.rb")); } @Override diff --git a/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache index 398e434e9bd2..ecad551c77b0 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache @@ -31,7 +31,7 @@ class BaseObject end def _deserialize(type, value) - case type.to_s + case type.to_sym when :DateTime DateTime.parse(value) when :string diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index f3c1b85f2997..362664aa2b76 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -36,84 +36,6 @@ require_relative 'base_object' {{/vars}} end -# # return the object in the form of hash -# def to_body -# body = {} -# self.class.attribute_map.each_pair do |key, value| -# body[value] = self.send(key) unless self.send(key).nil? -# end -# body -# end -# -# # build the object from hash -# def build_from_hash(attributes) -# self.class.swagger_types.each_pair do |key, type| -# if type =~ /^array\[(.*)\]/i -# if attributes[self.class.attribute_map[key]].is_a?(Array) -# self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } ) -# else -# #TODO show warning in debug mode -# end -# elsif !attributes[self.class.attribute_map[key]].nil? -# self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) -# else -# # data not found in attributes(hash), not an issue as the data can be optional -# end -# end -# -# self -# end -# -# def _deserialize(type, value) -# case type -# when :DateTime -# DateTime.parse(value) -# when :string -# value.to_s -# when :int -# value.to_i -# when :double -# value.to_f -# when :boolean -# if value =~ /^(true|t|yes|y|1)$/i -# true -# else -# false -# end -# else # model -# _model = Object.const_get(type).new -# _model.build_from_hash(value) -# end -# end -# -# -# -# # to_body is an alias to to_body (backward compatibility) -# def to_hash -# hash = {} -# self.class.attribute_map.each_pair do |key, value| -# if self.send(key).is_a?(Array) -# next if self.send(key).empty? -# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? -# else -# unless (_tmp_value = _to_hash self.send(key)).nil? -# hash[value] = _tmp_value -# end -# end -# end -# hash -# end -# -# # Method to output non-array value in the form of hash -# # For object, use to_hash. Otherwise, just return the value -# def _to_hash(value) -# if value.respond_to? :to_hash -# value.to_hash -# else -# value -# end -# end - end {{/model}} {{/models}} diff --git a/samples/client/petstore/ruby/lib/model/base_object.rb b/samples/client/petstore/ruby/lib/model/base_object.rb deleted file mode 100644 index 398e434e9bd2..000000000000 --- a/samples/client/petstore/ruby/lib/model/base_object.rb +++ /dev/null @@ -1,83 +0,0 @@ -# base class containing fundamental method such as to_hash, build_from_hash and more -class BaseObject - - # return the object in the form of hash - def to_body - body = {} - self.class.attribute_map.each_pair do |key, value| - body[value] = self.send(key) unless self.send(key).nil? - end - body - end - - # build the object from hash - def build_from_hash(attributes) - return nil unless attributes.is_a?(Hash) - self.class.swagger_types.each_pair do |key, type| - if type =~ /^array\[(.*)\]/i - if attributes[self.class.attribute_map[key]].is_a?(Array) - self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } ) - else - #TODO show warning in debug mode - end - elsif !attributes[self.class.attribute_map[key]].nil? - self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) - else - # data not found in attributes(hash), not an issue as the data can be optional - end - end - - self - end - - def _deserialize(type, value) - case type.to_s - when :DateTime - DateTime.parse(value) - when :string - value.to_s - when :int - value.to_i - when :double - value.to_f - when :boolean - if value =~ /^(true|t|yes|y|1)$/i - true - else - false - end - else # model - _model = Object.const_get(type).new - _model.build_from_hash(value) - end - end - - - - # to_body is an alias to to_body (backward compatibility) - def to_hash - hash = {} - self.class.attribute_map.each_pair do |key, value| - if self.send(key).is_a?(Array) - next if self.send(key).empty? - hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? - else - unless (_tmp_value = _to_hash self.send(key)).nil? - hash[value] = _tmp_value - end - end - end - hash - end - - # Method to output non-array value in the form of hash - # For object, use to_hash. Otherwise, just return the value - def _to_hash(value) - if value.respond_to? :to_hash - value.to_hash - else - value - end - end - -end diff --git a/samples/client/petstore/ruby/models/base_object.rb b/samples/client/petstore/ruby/models/base_object.rb index 337ca7f822ed..ecad551c77b0 100644 --- a/samples/client/petstore/ruby/models/base_object.rb +++ b/samples/client/petstore/ruby/models/base_object.rb @@ -1,7 +1,6 @@ -require 'json' -# +# base class containing fundamental method such as to_hash, build_from_hash and more class BaseObject - + # return the object in the form of hash def to_body body = {} @@ -13,6 +12,7 @@ class BaseObject # build the object from hash def build_from_hash(attributes) + return nil unless attributes.is_a?(Hash) self.class.swagger_types.each_pair do |key, type| if type =~ /^array\[(.*)\]/i if attributes[self.class.attribute_map[key]].is_a?(Array) @@ -30,7 +30,6 @@ class BaseObject self end - # deserialize value based on type def _deserialize(type, value) case type.to_sym when :DateTime @@ -53,9 +52,7 @@ class BaseObject end end - #def to_json - # self.to_hash.to_json - #end + # to_body is an alias to to_body (backward compatibility) def to_hash diff --git a/samples/client/petstore/ruby/models/category.rb b/samples/client/petstore/ruby/models/category.rb index b8ef8b6106a3..c1da69482e02 100644 --- a/samples/client/petstore/ruby/models/category.rb +++ b/samples/client/petstore/ruby/models/category.rb @@ -42,82 +42,4 @@ class Category < BaseObject end -# # return the object in the form of hash -# def to_body -# body = {} -# self.class.attribute_map.each_pair do |key, value| -# body[value] = self.send(key) unless self.send(key).nil? -# end -# body -# end -# -# # build the object from hash -# def build_from_hash(attributes) -# self.class.swagger_types.each_pair do |key, type| -# if type =~ /^array\[(.*)\]/i -# if attributes[self.class.attribute_map[key]].is_a?(Array) -# self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } ) -# else -# #TODO show warning in debug mode -# end -# elsif !attributes[self.class.attribute_map[key]].nil? -# self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) -# else -# # data not found in attributes(hash), not an issue as the data can be optional -# end -# end -# -# self -# end -# -# def _deserialize(type, value) -# case type -# when :DateTime -# DateTime.parse(value) -# when :string -# value.to_s -# when :int -# value.to_i -# when :double -# value.to_f -# when :boolean -# if value =~ /^(true|t|yes|y|1)$/i -# true -# else -# false -# end -# else # model -# _model = Object.const_get(type).new -# _model.build_from_hash(value) -# end -# end -# -# -# -# # to_body is an alias to to_body (backward compatibility) -# def to_hash -# hash = {} -# self.class.attribute_map.each_pair do |key, value| -# if self.send(key).is_a?(Array) -# next if self.send(key).empty? -# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? -# else -# unless (_tmp_value = _to_hash self.send(key)).nil? -# hash[value] = _tmp_value -# end -# end -# end -# hash -# end -# -# # Method to output non-array value in the form of hash -# # For object, use to_hash. Otherwise, just return the value -# def _to_hash(value) -# if value.respond_to? :to_hash -# value.to_hash -# else -# value -# end -# end - end diff --git a/samples/client/petstore/ruby/models/order.rb b/samples/client/petstore/ruby/models/order.rb index ade8fea251f7..6347167c860e 100644 --- a/samples/client/petstore/ruby/models/order.rb +++ b/samples/client/petstore/ruby/models/order.rb @@ -74,82 +74,4 @@ class Order < BaseObject end -# # return the object in the form of hash -# def to_body -# body = {} -# self.class.attribute_map.each_pair do |key, value| -# body[value] = self.send(key) unless self.send(key).nil? -# end -# body -# end -# -# # build the object from hash -# def build_from_hash(attributes) -# self.class.swagger_types.each_pair do |key, type| -# if type =~ /^array\[(.*)\]/i -# if attributes[self.class.attribute_map[key]].is_a?(Array) -# self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } ) -# else -# #TODO show warning in debug mode -# end -# elsif !attributes[self.class.attribute_map[key]].nil? -# self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) -# else -# # data not found in attributes(hash), not an issue as the data can be optional -# end -# end -# -# self -# end -# -# def _deserialize(type, value) -# case type -# when :DateTime -# DateTime.parse(value) -# when :string -# value.to_s -# when :int -# value.to_i -# when :double -# value.to_f -# when :boolean -# if value =~ /^(true|t|yes|y|1)$/i -# true -# else -# false -# end -# else # model -# _model = Object.const_get(type).new -# _model.build_from_hash(value) -# end -# end -# -# -# -# # to_body is an alias to to_body (backward compatibility) -# def to_hash -# hash = {} -# self.class.attribute_map.each_pair do |key, value| -# if self.send(key).is_a?(Array) -# next if self.send(key).empty? -# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? -# else -# unless (_tmp_value = _to_hash self.send(key)).nil? -# hash[value] = _tmp_value -# end -# end -# end -# hash -# end -# -# # Method to output non-array value in the form of hash -# # For object, use to_hash. Otherwise, just return the value -# def _to_hash(value) -# if value.respond_to? :to_hash -# value.to_hash -# else -# value -# end -# end - end diff --git a/samples/client/petstore/ruby/models/pet.rb b/samples/client/petstore/ruby/models/pet.rb index 4e41eb451ff8..9accf4d1c818 100644 --- a/samples/client/petstore/ruby/models/pet.rb +++ b/samples/client/petstore/ruby/models/pet.rb @@ -78,82 +78,4 @@ class Pet < BaseObject end -# # return the object in the form of hash -# def to_body -# body = {} -# self.class.attribute_map.each_pair do |key, value| -# body[value] = self.send(key) unless self.send(key).nil? -# end -# body -# end -# -# # build the object from hash -# def build_from_hash(attributes) -# self.class.swagger_types.each_pair do |key, type| -# if type =~ /^array\[(.*)\]/i -# if attributes[self.class.attribute_map[key]].is_a?(Array) -# self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } ) -# else -# #TODO show warning in debug mode -# end -# elsif !attributes[self.class.attribute_map[key]].nil? -# self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) -# else -# # data not found in attributes(hash), not an issue as the data can be optional -# end -# end -# -# self -# end -# -# def _deserialize(type, value) -# case type -# when :DateTime -# DateTime.parse(value) -# when :string -# value.to_s -# when :int -# value.to_i -# when :double -# value.to_f -# when :boolean -# if value =~ /^(true|t|yes|y|1)$/i -# true -# else -# false -# end -# else # model -# _model = Object.const_get(type).new -# _model.build_from_hash(value) -# end -# end -# -# -# -# # to_body is an alias to to_body (backward compatibility) -# def to_hash -# hash = {} -# self.class.attribute_map.each_pair do |key, value| -# if self.send(key).is_a?(Array) -# next if self.send(key).empty? -# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? -# else -# unless (_tmp_value = _to_hash self.send(key)).nil? -# hash[value] = _tmp_value -# end -# end -# end -# hash -# end -# -# # Method to output non-array value in the form of hash -# # For object, use to_hash. Otherwise, just return the value -# def _to_hash(value) -# if value.respond_to? :to_hash -# value.to_hash -# else -# value -# end -# end - end diff --git a/samples/client/petstore/ruby/models/tag.rb b/samples/client/petstore/ruby/models/tag.rb index dc4611953e65..7eb332a32182 100644 --- a/samples/client/petstore/ruby/models/tag.rb +++ b/samples/client/petstore/ruby/models/tag.rb @@ -42,82 +42,4 @@ class Tag < BaseObject end -# # return the object in the form of hash -# def to_body -# body = {} -# self.class.attribute_map.each_pair do |key, value| -# body[value] = self.send(key) unless self.send(key).nil? -# end -# body -# end -# -# # build the object from hash -# def build_from_hash(attributes) -# self.class.swagger_types.each_pair do |key, type| -# if type =~ /^array\[(.*)\]/i -# if attributes[self.class.attribute_map[key]].is_a?(Array) -# self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } ) -# else -# #TODO show warning in debug mode -# end -# elsif !attributes[self.class.attribute_map[key]].nil? -# self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) -# else -# # data not found in attributes(hash), not an issue as the data can be optional -# end -# end -# -# self -# end -# -# def _deserialize(type, value) -# case type -# when :DateTime -# DateTime.parse(value) -# when :string -# value.to_s -# when :int -# value.to_i -# when :double -# value.to_f -# when :boolean -# if value =~ /^(true|t|yes|y|1)$/i -# true -# else -# false -# end -# else # model -# _model = Object.const_get(type).new -# _model.build_from_hash(value) -# end -# end -# -# -# -# # to_body is an alias to to_body (backward compatibility) -# def to_hash -# hash = {} -# self.class.attribute_map.each_pair do |key, value| -# if self.send(key).is_a?(Array) -# next if self.send(key).empty? -# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? -# else -# unless (_tmp_value = _to_hash self.send(key)).nil? -# hash[value] = _tmp_value -# end -# end -# end -# hash -# end -# -# # Method to output non-array value in the form of hash -# # For object, use to_hash. Otherwise, just return the value -# def _to_hash(value) -# if value.respond_to? :to_hash -# value.to_hash -# else -# value -# end -# end - end diff --git a/samples/client/petstore/ruby/models/user.rb b/samples/client/petstore/ruby/models/user.rb index 376c5a20f0a0..20420e1c69b7 100644 --- a/samples/client/petstore/ruby/models/user.rb +++ b/samples/client/petstore/ruby/models/user.rb @@ -90,82 +90,4 @@ class User < BaseObject end -# # return the object in the form of hash -# def to_body -# body = {} -# self.class.attribute_map.each_pair do |key, value| -# body[value] = self.send(key) unless self.send(key).nil? -# end -# body -# end -# -# # build the object from hash -# def build_from_hash(attributes) -# self.class.swagger_types.each_pair do |key, type| -# if type =~ /^array\[(.*)\]/i -# if attributes[self.class.attribute_map[key]].is_a?(Array) -# self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } ) -# else -# #TODO show warning in debug mode -# end -# elsif !attributes[self.class.attribute_map[key]].nil? -# self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) -# else -# # data not found in attributes(hash), not an issue as the data can be optional -# end -# end -# -# self -# end -# -# def _deserialize(type, value) -# case type -# when :DateTime -# DateTime.parse(value) -# when :string -# value.to_s -# when :int -# value.to_i -# when :double -# value.to_f -# when :boolean -# if value =~ /^(true|t|yes|y|1)$/i -# true -# else -# false -# end -# else # model -# _model = Object.const_get(type).new -# _model.build_from_hash(value) -# end -# end -# -# -# -# # to_body is an alias to to_body (backward compatibility) -# def to_hash -# hash = {} -# self.class.attribute_map.each_pair do |key, value| -# if self.send(key).is_a?(Array) -# next if self.send(key).empty? -# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? -# else -# unless (_tmp_value = _to_hash self.send(key)).nil? -# hash[value] = _tmp_value -# end -# end -# end -# hash -# end -# -# # Method to output non-array value in the form of hash -# # For object, use to_hash. Otherwise, just return the value -# def _to_hash(value) -# if value.respond_to? :to_hash -# value.to_hash -# else -# value -# end -# end - end