github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/ruby-client/partial_model_generic.mustache (about)

     1    {{#description}}
     2    # {{{description}}}
     3    {{/description}}
     4    class {{classname}}{{#parent}} < {{{.}}}{{/parent}}
     5    {{#vars}}
     6      {{#description}}
     7      # {{{description}}}
     8      {{/description}}
     9      attr_accessor :{{{name}}}
    10  
    11    {{/vars}}
    12  {{#hasEnums}}
    13      class EnumAttributeValidator
    14        attr_reader :datatype
    15        attr_reader :allowable_values
    16  
    17        def initialize(datatype, allowable_values)
    18          @allowable_values = allowable_values.map do |value|
    19            case datatype.to_s
    20            when /Integer/i
    21              value.to_i
    22            when /Float/i
    23              value.to_f
    24            else
    25              value
    26            end
    27          end
    28        end
    29  
    30        def valid?(value)
    31          !value || allowable_values.include?(value)
    32        end
    33      end
    34  
    35  {{/hasEnums}}
    36      # Attribute mapping from ruby-style variable name to JSON key.
    37      def self.attribute_map
    38        {
    39          {{#vars}}
    40          :'{{{name}}}' => :'{{{baseName}}}'{{^-last}},{{/-last}}
    41          {{/vars}}
    42        }
    43      end
    44  
    45      # Attribute type mapping.
    46      def self.openapi_types
    47        {
    48          {{#vars}}
    49          :'{{{name}}}' => :'{{^isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isDateTime}}DateTime{{/isDateTime}}'{{^-last}},{{/-last}}
    50          {{/vars}}
    51        }
    52      end
    53  
    54      # List of attributes with nullable: true
    55      def self.openapi_nullable
    56        Set.new([
    57          {{#vars}}
    58          {{#isNullable}}
    59          :'{{{name}}}'{{^-last}},{{/-last}}
    60          {{/isNullable}}
    61          {{/vars}}
    62        ])
    63      end
    64  
    65      {{#anyOf}}
    66      {{#-first}}
    67      # List of class defined in anyOf (OpenAPI v3)
    68      def self.openapi_any_of
    69        [
    70      {{/-first}}
    71        :'{{{.}}}'{{^-last}},{{/-last}}
    72      {{#-last}}
    73        ]
    74      end
    75  
    76      {{/-last}}
    77      {{/anyOf}}
    78      {{#oneOf}}
    79      {{#-first}}
    80      # List of class defined in oneOf (OpenAPI v3)
    81      def self.openapi_one_of
    82        [
    83      {{/-first}}
    84        :'{{{.}}}'{{^-last}},{{/-last}}
    85      {{#-last}}
    86        ]
    87      end
    88  
    89      {{/-last}}
    90      {{/oneOf}}
    91      {{#allOf}}
    92      {{#-first}}
    93      # List of class defined in allOf (OpenAPI v3)
    94      def self.openapi_all_of
    95        [
    96      {{/-first}}
    97        :'{{{.}}}'{{^-last}},{{/-last}}
    98      {{#-last}}
    99        ]
   100      end
   101  
   102      {{/-last}}
   103      {{/allOf}}
   104      {{#discriminator}}
   105      {{#propertyName}}
   106      # discriminator's property name in OpenAPI v3
   107      def self.openapi_discriminator_name
   108        :'{{{.}}}'
   109      end
   110  
   111      {{/propertyName}}
   112      {{/discriminator}}
   113      # Initializes the object
   114      # @param [Hash] attributes Model attributes in the form of hash
   115      def initialize(attributes = {})
   116        if (!attributes.is_a?(Hash))
   117          fail ArgumentError, "The input argument (attributes) must be a hash in `{{{moduleName}}}::{{{classname}}}` initialize method"
   118        end
   119  
   120        # check to see if the attribute exists and convert string to symbol for hash key
   121        attributes = attributes.each_with_object({}) { |(k, v), h|
   122          if (!self.class.attribute_map.key?(k.to_sym))
   123            fail ArgumentError, "`#{k}` is not a valid attribute in `{{{moduleName}}}::{{{classname}}}`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
   124          end
   125          h[k.to_sym] = v
   126        }
   127        {{#parent}}
   128  
   129        # call parent's initialize
   130        super(attributes)
   131        {{/parent}}
   132        {{#vars}}
   133  
   134        if attributes.key?(:'{{{name}}}')
   135          {{#isArray}}
   136          if (value = attributes[:'{{{name}}}']).is_a?(Array)
   137            self.{{{name}}} = value
   138          end
   139          {{/isArray}}
   140          {{#isMap}}
   141          if (value = attributes[:'{{{name}}}']).is_a?(Hash)
   142            self.{{{name}}} = value
   143          end
   144          {{/isMap}}
   145          {{^isContainer}}
   146          self.{{{name}}} = attributes[:'{{{name}}}']
   147          {{/isContainer}}
   148        {{#defaultValue}}
   149        else
   150          self.{{{name}}} = {{{defaultValue}}}
   151        {{/defaultValue}}
   152        end
   153        {{/vars}}
   154      end
   155  
   156      # Show invalid properties with the reasons. Usually used together with valid?
   157      # @return Array for valid properties with the reasons
   158      def list_invalid_properties
   159        invalid_properties = {{^parent}}Array.new{{/parent}}{{#parent}}super{{/parent}}
   160        {{#vars}}
   161        {{^isNullable}}
   162        {{#required}}
   163        if @{{{name}}}.nil?
   164          invalid_properties.push('invalid value for "{{{name}}}", {{{name}}} cannot be nil.')
   165        end
   166  
   167        {{/required}}
   168        {{/isNullable}}
   169        {{#hasValidation}}
   170        {{#maxLength}}
   171        if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}}
   172          invalid_properties.push('invalid value for "{{{name}}}", the character length must be smaller than or equal to {{{maxLength}}}.')
   173        end
   174  
   175        {{/maxLength}}
   176        {{#minLength}}
   177        if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}}
   178          invalid_properties.push('invalid value for "{{{name}}}", the character length must be great than or equal to {{{minLength}}}.')
   179        end
   180  
   181        {{/minLength}}
   182        {{#maximum}}
   183        if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
   184          invalid_properties.push('invalid value for "{{{name}}}", must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.')
   185        end
   186  
   187        {{/maximum}}
   188        {{#minimum}}
   189        if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
   190          invalid_properties.push('invalid value for "{{{name}}}", must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.')
   191        end
   192  
   193        {{/minimum}}
   194        {{#pattern}}
   195        pattern = Regexp.new({{{pattern}}})
   196        if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ pattern
   197          invalid_properties.push("invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}.")
   198        end
   199  
   200        {{/pattern}}
   201        {{#maxItems}}
   202        if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length > {{{maxItems}}}
   203          invalid_properties.push('invalid value for "{{{name}}}", number of items must be less than or equal to {{{maxItems}}}.'
   204        end
   205  
   206        {{/maxItems}}
   207        {{#minItems}}
   208        if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length < {{{minItems}}}
   209          invalid_properties.push('invalid value for "{{{name}}}", number of items must be greater than or equal to {{{minItems}}}.'
   210        end
   211  
   212        {{/minItems}}
   213        {{/hasValidation}}
   214        {{/vars}}
   215        invalid_properties
   216      end
   217  
   218      # Check to see if the all the properties in the model are valid
   219      # @return true if the model is valid
   220      def valid?
   221        {{#vars}}
   222        {{^isNullable}}
   223        {{#required}}
   224        return false if @{{{name}}}.nil?
   225        {{/required}}
   226        {{/isNullable}}
   227        {{#isEnum}}
   228        {{^isContainer}}
   229        {{{name}}}_validator = EnumAttributeValidator.new('{{{dataType}}}', [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])
   230        return false unless {{{name}}}_validator.valid?(@{{{name}}})
   231        {{/isContainer}}
   232        {{/isEnum}}
   233        {{#hasValidation}}
   234        {{#maxLength}}
   235        return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}}
   236        {{/maxLength}}
   237        {{#minLength}}
   238        return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}}
   239        {{/minLength}}
   240        {{#maximum}}
   241        return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
   242        {{/maximum}}
   243        {{#minimum}}
   244        return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
   245        {{/minimum}}
   246        {{#pattern}}
   247        return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{pattern}}})
   248        {{/pattern}}
   249        {{#maxItems}}
   250        return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length > {{{maxItems}}}
   251        {{/maxItems}}
   252        {{#minItems}}
   253        return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length < {{{minItems}}}
   254        {{/minItems}}
   255        {{/hasValidation}}
   256        {{/vars}}
   257        {{#anyOf}}
   258        {{#-first}}
   259        _any_of_found = false
   260        self.class.openapi_any_of.each do |_class|
   261          _any_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash)
   262          if _any_of.valid?
   263            _any_of_found = true
   264          end
   265        end
   266  
   267        if !_any_of_found
   268          return false
   269        end
   270  
   271        {{/-first}}
   272        {{/anyOf}}
   273        {{#oneOf}}
   274        {{#-first}}
   275        _one_of_found = false
   276        self.class.openapi_one_of.each do |_class|
   277          _one_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash)
   278          if _one_of.valid?
   279            if _one_of_found
   280              return false
   281            else
   282              _one_of_found = true
   283            end
   284          end
   285        end
   286  
   287        if !_one_of_found
   288          return false
   289        end
   290  
   291        {{/-first}}
   292        {{/oneOf}}
   293        true{{#parent}} && super{{/parent}}
   294      end
   295  
   296      {{#vars}}
   297      {{#isEnum}}
   298      {{^isContainer}}
   299      # Custom attribute writer method checking allowed values (enum).
   300      # @param [Object] {{{name}}} Object to be assigned
   301      def {{{name}}}=({{{name}}})
   302        validator = EnumAttributeValidator.new('{{{dataType}}}', [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])
   303        unless validator.valid?({{{name}}})
   304          fail ArgumentError, "invalid value for \"{{{name}}}\", must be one of #{validator.allowable_values}."
   305        end
   306        @{{{name}}} = {{{name}}}
   307      end
   308  
   309      {{/isContainer}}
   310      {{/isEnum}}
   311      {{^isEnum}}
   312      {{#hasValidation}}
   313      # Custom attribute writer method with validation
   314      # @param [Object] {{{name}}} Value to be assigned
   315      def {{{name}}}=({{{name}}})
   316        {{^isNullable}}
   317        {{#required}}
   318        if {{{name}}}.nil?
   319          fail ArgumentError, '{{{name}}} cannot be nil'
   320        end
   321  
   322        {{/required}}
   323        {{/isNullable}}
   324        {{#maxLength}}
   325        if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}}
   326          fail ArgumentError, 'invalid value for "{{{name}}}", the character length must be smaller than or equal to {{{maxLength}}}.'
   327        end
   328  
   329        {{/maxLength}}
   330        {{#minLength}}
   331        if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length < {{{minLength}}}
   332          fail ArgumentError, 'invalid value for "{{{name}}}", the character length must be great than or equal to {{{minLength}}}.'
   333        end
   334  
   335        {{/minLength}}
   336        {{#maximum}}
   337        if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
   338          fail ArgumentError, 'invalid value for "{{{name}}}", must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.'
   339        end
   340  
   341        {{/maximum}}
   342        {{#minimum}}
   343        if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
   344          fail ArgumentError, 'invalid value for "{{{name}}}", must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.'
   345        end
   346  
   347        {{/minimum}}
   348        {{#pattern}}
   349        pattern = Regexp.new({{{pattern}}})
   350        if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} !~ pattern
   351          fail ArgumentError, "invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}."
   352        end
   353  
   354        {{/pattern}}
   355        {{#maxItems}}
   356        if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.length > {{{maxItems}}}
   357          fail ArgumentError, 'invalid value for "{{{name}}}", number of items must be less than or equal to {{{maxItems}}}.'
   358        end
   359  
   360        {{/maxItems}}
   361        {{#minItems}}
   362        if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.length < {{{minItems}}}
   363          fail ArgumentError, 'invalid value for "{{{name}}}", number of items must be greater than or equal to {{{minItems}}}.'
   364        end
   365  
   366        {{/minItems}}
   367        @{{{name}}} = {{{name}}}
   368      end
   369  
   370      {{/hasValidation}}
   371      {{/isEnum}}
   372      {{/vars}}
   373      # Checks equality by comparing each attribute.
   374      # @param [Object] Object to be compared
   375      def ==(o)
   376        return true if self.equal?(o)
   377        self.class == o.class{{#vars}} &&
   378            {{name}} == o.{{name}}{{/vars}}{{#parent}} && super(o){{/parent}}
   379      end
   380  
   381      # @see the `==` method
   382      # @param [Object] Object to be compared
   383      def eql?(o)
   384        self == o
   385      end
   386  
   387      # Calculates hash code according to all attributes.
   388      # @return [Integer] Hash code
   389      def hash
   390        [{{#vars}}{{name}}{{^-last}}, {{/-last}}{{/vars}}].hash
   391      end
   392  
   393  {{> base_object}}
   394    end