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

ruby.partial_model_generic.mustache Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
  {{#description}}
  # {{{description}}}
  {{/description}}
  class {{classname}}
  {{#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

    # Initializes the object
    # @param [Hash] attributes Model attributes in the form of hash
    def initialize(attributes = {})
      return unless attributes.is_a?(Hash)

      # convert string to symbol for hash key
      attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
      {{#vars}}

      if attributes.has_key?(:'{{{baseName}}}')
        {{#isListContainer}}
        if (value = attributes[:'{{{baseName}}}']).is_a?(Array)
          self.{{{name}}} = value
        end
        {{/isListContainer}}
        {{#isMapContainer}}
        if (value = attributes[:'{{{baseName}}}']).is_a?(Hash)
          self.{{{name}}} = value
        end
        {{/isMapContainer}}
        {{^isContainer}}
        self.{{{name}}} = attributes[:'{{{baseName}}}']
        {{/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 = Array.new
      {{#vars}}
      {{#required}}
      if @{{{name}}}.nil?
        invalid_properties.push('invalid value for "{{{name}}}", {{{name}}} cannot be nil.')
      end

      {{/required}}
      {{#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}}
      if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{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}}
      {{#required}}
      return false if @{{{name}}}.nil?
      {{/required}}
      {{#isEnum}}
      {{^isContainer}}
      {{{name}}}_validator = EnumAttributeValidator.new('{{{dataType}}}', [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/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}}
      true
    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}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/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}}})
      {{#required}}
      if {{{name}}}.nil?
        fail ArgumentError, '{{{name}}} cannot be nil'
      end

      {{/required}}
      {{#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}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} !~ Regexp.new({{{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}}
    end

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

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

{{> base_object}}
  end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy