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

crystal.partial_model_generic.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
  {{#description}}
  # {{{description}}}
  {{/description}}
  class {{classname}}{{#parent}} < {{{.}}}{{/parent}} include JSON::Serializable
   include JSON::Serializable {{#vars}}
    {{#description}}
    # {{{description}}}
    {{/description}}
    @[JSON::Field(key: {{{baseName}}}, type: {{{dataType}}}{{#default}}, default: {{{.}}}{{/default}}{{#isNullable}}, nilable: true, emit_null: true{{/isNullable}})]
    property {{{name}}} : {{{dataType}}}

  {{/vars}}
{{#hasEnums}}
    class EnumAttributeValidator
      getter datatype : String
      getter allowable_values : Array(String)

      def initialize(datatype, allowable_values)
        @datatype = datatype
        @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}}
    {{#anyOf}}
    {{#-first}}
    # List of class defined in anyOf (OpenAPI v3)
    def self.openapi_any_of
      [
    {{/-first}}
      :"{{{.}}}"{{^-last}},{{/-last}}
    {{#-last}}
      ]
    end

    {{/-last}}
    {{/anyOf}}
    {{#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({{#vars}}@{{{name}}} : {{{dataType}}}{{^required}} | Nil{{/required}}{{^-last}}, {{/-last}}{{/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}}
      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}}})
        raise ArgumentError.new("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?
        raise ArgumentError.new("{{{name}}} cannot be nil")
      end

      {{/required}}
      {{/isNullable}}
      {{#maxLength}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}}
        raise ArgumentError.new("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}}}
        raise ArgumentError.new("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}}}
        raise ArgumentError.new("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}}}
        raise ArgumentError.new("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
        raise ArgumentError.new("invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}.")
      end

      {{/pattern}}
      {{#maxItems}}
      if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.length > {{{maxItems}}}
        raise ArgumentError.new("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}}}
        raise ArgumentError.new("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}}{{^-last}}, {{/-last}}{{/vars}}].hash
    end

{{> base_object}}
  end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy