All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ruby-client.partial_model_generic.mustache Maven / Gradle / Ivy

  {{#description}}
  # {{{description}}}
  {{/description}}
  class {{classname}}{{#parent}} < {{{.}}}{{/parent}}
  {{#vars}}
    {{#description}}
    # {{{description}}}
    {{/description}}
    attr_accessor :{{{name}}}

  {{/vars}}
{{#hasEnums}}
    class EnumAttributeValidator
      attr_reader :datatype
      attr_reader :allowable_values

      def initialize(datatype, allowable_values)
        @allowable_values = allowable_values.map do |value|
          case datatype.to_s
          when /Integer/i
            value.to_i
          when /Float/i
            value.to_f
          else
            value
          end
        end
      end

      def valid?(value)
        !value || allowable_values.include?(value)
      end
    end

{{/hasEnums}}
    # Attribute mapping from ruby-style variable name to JSON key.
    def self.attribute_map
      {
        {{#vars}}
        :'{{{name}}}' => :'{{{baseName}}}'{{#hasMore}},{{/hasMore}}
        {{/vars}}
      }
    end

    # Attribute type mapping.
    def self.openapi_types
      {
        {{#vars}}
        :'{{{name}}}' => :'{{{dataType}}}'{{#hasMore}},{{/hasMore}}
        {{/vars}}
      }
    end

    # List of attributes with nullable: true
    def self.openapi_nullable
      Set.new([
        {{#vars}}
        {{#isNullable}}
        :'{{{name}}}'{{#hasMore}},{{/hasMore}}
        {{/isNullable}}
        {{/vars}}
      ])
    end

    {{#anyOf}}
    {{#-first}}
    # List of class defined in anyOf (OpenAPI v3)
    def self.openapi_any_of
      [
    {{/-first}}
      :'{{{.}}}'{{^-last}},{{/-last}}
    {{#-last}}
      ]
    end

    {{/-last}}
    {{/anyOf}}
    {{#oneOf}}
    {{#-first}}
    # List of class defined in oneOf (OpenAPI v3)
    def self.openapi_one_of
      [
    {{/-first}}
      :'{{{.}}}'{{^-last}},{{/-last}}
    {{#-last}}
      ]
    end

    {{/-last}}
    {{/oneOf}}
    {{#allOf}}
    {{#-first}}
    # List of class defined in allOf (OpenAPI v3)
    def self.openapi_all_of
      [
    {{/-first}}
      :'{{{.}}}'{{^-last}},{{/-last}}
    {{#-last}}
      ]
    end

    {{/-last}}
    {{/allOf}}
    {{#discriminator}}
    {{#propertyName}}
    # discriminator's property name in OpenAPI v3
    def self.openapi_discriminator_name
      :'{{{.}}}'
    end

    {{/propertyName}}
    {{/discriminator}}
    # Initializes the object
    # @param [Hash] attributes Model attributes in the form of hash
    def initialize(attributes = {})
      if (!attributes.is_a?(Hash))
        fail ArgumentError, "The input argument (attributes) must be a hash in `{{{moduleName}}}::{{{classname}}}` initialize method"
      end

      # check to see if the attribute exists and convert string to symbol for hash key
      attributes = attributes.each_with_object({}) { |(k, v), h|
        if (!self.class.attribute_map.key?(k.to_sym))
          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
        end
        h[k.to_sym] = v
      }
      {{#parent}}

      # call parent's initialize
      super(attributes)
      {{/parent}}
      {{#vars}}

      if attributes.key?(:'{{{name}}}')
        {{#isListContainer}}
        if (value = attributes[:'{{{name}}}']).is_a?(Array)
          self.{{{name}}} = value
        end
        {{/isListContainer}}
        {{#isMapContainer}}
        if (value = attributes[:'{{{name}}}']).is_a?(Hash)
          self.{{{name}}} = value
        end
        {{/isMapContainer}}
        {{^isContainer}}
        self.{{{name}}} = attributes[:'{{{name}}}']
        {{/isContainer}}
      {{#defaultValue}}
      else
        self.{{{name}}} = {{{defaultValue}}}
      {{/defaultValue}}
      end
      {{/vars}}
    end

    # Show invalid properties with the reasons. Usually used together with valid?
    # @return Array for valid properties with the reasons
    def list_invalid_properties
      invalid_properties = {{^parent}}Array.new{{/parent}}{{#parent}}super{{/parent}}
      {{#vars}}
      {{^isNullable}}
      {{#required}}
      if @{{{name}}}.nil?
        invalid_properties.push('invalid value for "{{{name}}}", {{{name}}} cannot be nil.')
      end

      {{/required}}
      {{/isNullable}}
      {{#hasValidation}}
      {{#maxLength}}
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}}
        invalid_properties.push('invalid value for "{{{name}}}", the character length must be smaller than or equal to {{{maxLength}}}.')
      end

      {{/maxLength}}
      {{#minLength}}
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}}
        invalid_properties.push('invalid value for "{{{name}}}", the character length must be great than or equal to {{{minLength}}}.')
      end

      {{/minLength}}
      {{#maximum}}
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
        invalid_properties.push('invalid value for "{{{name}}}", must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.')
      end

      {{/maximum}}
      {{#minimum}}
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
        invalid_properties.push('invalid value for "{{{name}}}", must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.')
      end

      {{/minimum}}
      {{#pattern}}
      pattern = Regexp.new({{{pattern}}})
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ pattern
        invalid_properties.push("invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}.")
      end

      {{/pattern}}
      {{#maxItems}}
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length > {{{maxItems}}}
        invalid_properties.push('invalid value for "{{{name}}}", number of items must be less than or equal to {{{maxItems}}}.'
      end

      {{/maxItems}}
      {{#minItems}}
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length < {{{minItems}}}
        invalid_properties.push('invalid value for "{{{name}}}", number of items must be greater than or equal to {{{minItems}}}.'
      end

      {{/minItems}}
      {{/hasValidation}}
      {{/vars}}
      invalid_properties
    end

    # Check to see if the all the properties in the model are valid
    # @return true if the model is valid
    def valid?
      {{#vars}}
      {{^isNullable}}
      {{#required}}
      return false if @{{{name}}}.nil?
      {{/required}}
      {{/isNullable}}
      {{#isEnum}}
      {{^isContainer}}
      {{{name}}}_validator = EnumAttributeValidator.new('{{{dataType}}}', [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])
      return false unless {{{name}}}_validator.valid?(@{{{name}}})
      {{/isContainer}}
      {{/isEnum}}
      {{#hasValidation}}
      {{#maxLength}}
      return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}}
      {{/maxLength}}
      {{#minLength}}
      return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}}
      {{/minLength}}
      {{#maximum}}
      return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
      {{/maximum}}
      {{#minimum}}
      return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
      {{/minimum}}
      {{#pattern}}
      return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{pattern}}})
      {{/pattern}}
      {{#maxItems}}
      return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length > {{{maxItems}}}
      {{/maxItems}}
      {{#minItems}}
      return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.length < {{{minItems}}}
      {{/minItems}}
      {{/hasValidation}}
      {{/vars}}
      {{#anyOf}}
      {{#-first}}
      _any_of_found = false
      self.class.openapi_any_of.each do |_class|
        _any_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash)
        if _any_of.valid?
          _any_of_found = true
        end
      end

      if !_any_of_found
        return false
      end

      {{/-first}}
      {{/anyOf}}
      {{#oneOf}}
      {{#-first}}
      _one_of_found = false
      self.class.openapi_one_of.each do |_class|
        _one_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash)
        if _one_of.valid?
          if _one_of_found
            return false
          else
            _one_of_found = true
          end
        end
      end

      if !_one_of_found
        return false
      end

      {{/-first}}
      {{/oneOf}}
      true{{#parent}} && super{{/parent}}
    end

    {{#vars}}
    {{#isEnum}}
    {{^isContainer}}
    # Custom attribute writer method checking allowed values (enum).
    # @param [Object] {{{name}}} Object to be assigned
    def {{{name}}}=({{{name}}})
      validator = EnumAttributeValidator.new('{{{dataType}}}', [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])
      unless validator.valid?({{{name}}})
        fail ArgumentError, "invalid value for \"{{{name}}}\", must be one of #{validator.allowable_values}."
      end
      @{{{name}}} = {{{name}}}
    end

    {{/isContainer}}
    {{/isEnum}}
    {{^isEnum}}
    {{#hasValidation}}
    # Custom attribute writer method with validation
    # @param [Object] {{{name}}} Value to be assigned
    def {{{name}}}=({{{name}}})
      {{^isNullable}}
      {{#required}}
      if {{{name}}}.nil?
        fail ArgumentError, '{{{name}}} cannot be nil'
      end

      {{/required}}
      {{/isNullable}}
      {{#maxLength}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}}
        fail ArgumentError, 'invalid value for "{{{name}}}", the character length must be smaller than or equal to {{{maxLength}}}.'
      end

      {{/maxLength}}
      {{#minLength}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length < {{{minLength}}}
        fail ArgumentError, 'invalid value for "{{{name}}}", the character length must be great than or equal to {{{minLength}}}.'
      end

      {{/minLength}}
      {{#maximum}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
        fail ArgumentError, 'invalid value for "{{{name}}}", must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.'
      end

      {{/maximum}}
      {{#minimum}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
        fail ArgumentError, 'invalid value for "{{{name}}}", must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.'
      end

      {{/minimum}}
      {{#pattern}}
      pattern = Regexp.new({{{pattern}}})
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} !~ pattern
        fail ArgumentError, "invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}."
      end

      {{/pattern}}
      {{#maxItems}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.length > {{{maxItems}}}
        fail ArgumentError, 'invalid value for "{{{name}}}", number of items must be less than or equal to {{{maxItems}}}.'
      end

      {{/maxItems}}
      {{#minItems}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.length < {{{minItems}}}
        fail ArgumentError, 'invalid value for "{{{name}}}", number of items must be greater than or equal to {{{minItems}}}.'
      end

      {{/minItems}}
      @{{{name}}} = {{{name}}}
    end

    {{/hasValidation}}
    {{/isEnum}}
    {{/vars}}
    # Checks equality by comparing each attribute.
    # @param [Object] Object to be compared
    def ==(o)
      return true if self.equal?(o)
      self.class == o.class{{#vars}} &&
          {{name}} == o.{{name}}{{/vars}}{{#parent}} && super(o){{/parent}}
    end

    # @see the `==` method
    # @param [Object] Object to be compared
    def eql?(o)
      self == o
    end

    # Calculates hash code according to all attributes.
    # @return [Integer] Hash code
    def hash
      [{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash
    end

{{> base_object}}
  end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy