[Ruby] Change constants lookup methods in enum class (#13285)

* [Ruby] Add missing block ends

* [Ruby] Change constants lookup methods in enum class

* Update ruby samples
This commit is contained in:
miyucy 2022-09-20 14:41:26 +09:00 committed by GitHub
parent d75b7b6194
commit 1f9f94066a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 115 additions and 57 deletions

View File

@ -2,6 +2,10 @@
{{{name}}} = {{{value}}}.freeze{{/enumVars}}
{{/allowableValues}}
def self.all_vars
@all_vars ||= [{{#allowableValues}}{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -13,8 +17,7 @@
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = {{classname}}.constants.select { |c| {{classname}}::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #{{{classname}}}" if constantValues.empty?
value
return value if {{classname}}.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #{{{classname}}}"
end
end

View File

@ -60,6 +60,7 @@ require '{{{gemName}}}'
# }
{{/-last}}
{{/mappedModels}}
```
{{/discriminator}}
### build

View File

@ -19,6 +19,10 @@ module Petstore
EFG = "-efg".freeze
XYZ = "(xyz)".freeze
def self.all_vars
@all_vars ||= [ABC, EFG, XYZ].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = EnumClass.constants.select { |c| EnumClass::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #EnumClass" if constantValues.empty?
value
return value if EnumClass.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #EnumClass"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
APPROVED = "approved".freeze
DELIVERED = "delivered".freeze
def self.all_vars
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnum.constants.select { |c| OuterEnum::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnum" if constantValues.empty?
value
return value if OuterEnum.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnum"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
APPROVED = "approved".freeze
DELIVERED = "delivered".freeze
def self.all_vars
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumDefaultValue.constants.select { |c| OuterEnumDefaultValue::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue" if constantValues.empty?
value
return value if OuterEnumDefaultValue.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
N1 = 1.freeze
N2 = 2.freeze
def self.all_vars
@all_vars ||= [N0, N1, N2].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumInteger.constants.select { |c| OuterEnumInteger::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumInteger" if constantValues.empty?
value
return value if OuterEnumInteger.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumInteger"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
N1 = 1.freeze
N2 = 2.freeze
def self.all_vars
@all_vars ||= [N0, N1, N2].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumIntegerDefaultValue.constants.select { |c| OuterEnumIntegerDefaultValue::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue" if constantValues.empty?
value
return value if OuterEnumIntegerDefaultValue.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue"
end
end
end

View File

@ -18,6 +18,10 @@ module Petstore
ADMIN = "admin".freeze
USER = "user".freeze
def self.all_vars
@all_vars ||= [ADMIN, USER].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -29,9 +33,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = SingleRefType.constants.select { |c| SingleRefType::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #SingleRefType" if constantValues.empty?
value
return value if SingleRefType.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #SingleRefType"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
EFG = "-efg".freeze
XYZ = "(xyz)".freeze
def self.all_vars
@all_vars ||= [ABC, EFG, XYZ].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = EnumClass.constants.select { |c| EnumClass::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #EnumClass" if constantValues.empty?
value
return value if EnumClass.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #EnumClass"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
APPROVED = "approved".freeze
DELIVERED = "delivered".freeze
def self.all_vars
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnum.constants.select { |c| OuterEnum::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnum" if constantValues.empty?
value
return value if OuterEnum.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnum"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
APPROVED = "approved".freeze
DELIVERED = "delivered".freeze
def self.all_vars
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumDefaultValue.constants.select { |c| OuterEnumDefaultValue::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue" if constantValues.empty?
value
return value if OuterEnumDefaultValue.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
N1 = 1.freeze
N2 = 2.freeze
def self.all_vars
@all_vars ||= [N0, N1, N2].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumInteger.constants.select { |c| OuterEnumInteger::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumInteger" if constantValues.empty?
value
return value if OuterEnumInteger.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumInteger"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
N1 = 1.freeze
N2 = 2.freeze
def self.all_vars
@all_vars ||= [N0, N1, N2].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumIntegerDefaultValue.constants.select { |c| OuterEnumIntegerDefaultValue::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue" if constantValues.empty?
value
return value if OuterEnumIntegerDefaultValue.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue"
end
end
end

View File

@ -18,6 +18,10 @@ module Petstore
ADMIN = "admin".freeze
USER = "user".freeze
def self.all_vars
@all_vars ||= [ADMIN, USER].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -29,9 +33,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = SingleRefType.constants.select { |c| SingleRefType::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #SingleRefType" if constantValues.empty?
value
return value if SingleRefType.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #SingleRefType"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
EFG = "-efg".freeze
XYZ = "(xyz)".freeze
def self.all_vars
@all_vars ||= [ABC, EFG, XYZ].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = EnumClass.constants.select { |c| EnumClass::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #EnumClass" if constantValues.empty?
value
return value if EnumClass.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #EnumClass"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
APPROVED = "approved".freeze
DELIVERED = "delivered".freeze
def self.all_vars
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnum.constants.select { |c| OuterEnum::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnum" if constantValues.empty?
value
return value if OuterEnum.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnum"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
APPROVED = "approved".freeze
DELIVERED = "delivered".freeze
def self.all_vars
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumDefaultValue.constants.select { |c| OuterEnumDefaultValue::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue" if constantValues.empty?
value
return value if OuterEnumDefaultValue.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
N1 = 1.freeze
N2 = 2.freeze
def self.all_vars
@all_vars ||= [N0, N1, N2].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumInteger.constants.select { |c| OuterEnumInteger::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumInteger" if constantValues.empty?
value
return value if OuterEnumInteger.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumInteger"
end
end
end

View File

@ -19,6 +19,10 @@ module Petstore
N1 = 1.freeze
N2 = 2.freeze
def self.all_vars
@all_vars ||= [N0, N1, N2].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -30,9 +34,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = OuterEnumIntegerDefaultValue.constants.select { |c| OuterEnumIntegerDefaultValue::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue" if constantValues.empty?
value
return value if OuterEnumIntegerDefaultValue.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue"
end
end
end

View File

@ -18,6 +18,10 @@ module Petstore
ADMIN = "admin".freeze
USER = "user".freeze
def self.all_vars
@all_vars ||= [ADMIN, USER].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
@ -29,9 +33,8 @@ module Petstore
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
constantValues = SingleRefType.constants.select { |c| SingleRefType::const_get(c) == value }
raise "Invalid ENUM value #{value} for class #SingleRefType" if constantValues.empty?
value
return value if SingleRefType.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #SingleRefType"
end
end
end