class {{ name }} include BaseGenericModel {%- for attr, definition in model.get("properties", {}).items() %} {%- set required = attr in model.get("required", []) %} {%- set has_validation = ( (not definition.nullable and required) or definition.maxLength is defined or definition.minLength is defined or definition.maximum is defined or definition.minimum is defined or definition.pattern is defined or definition.maxItems is defined or definition.minItems is defined ) %}{# FIXME add validation for enum or definition.enum is defined #} {{ definition.description|block_comment|indent(4) }} {%- if has_validation %} attr_reader :{{ attr|attribute_name }} {%- else %} attr_accessor :{{ attr|attribute_name }} {%- endif %} {%- endfor %} {%- if model.get("additionalProperties") %} attr_accessor :additional_properties {%- endif %} # Attribute mapping from ruby-style variable name to JSON key. # @!visibility private def self.attribute_map { {%- for attr in model.properties %} :'{{ attr|attribute_name }}' => :'{{ attr }}'{% if not loop.last %},{% endif %} {%- endfor %} } end # Attribute type mapping. # @!visibility private def self.openapi_types { {%- set nullables = [] %} {%- for attr, spec in model.properties.items() %} {%- if spec.nullable %}{%- set tmp = nullables.append(attr) %}{%- endif %} :'{{ attr|attribute_name }}' => :'{{ get_type_for_attribute(model, attr, current_name=name) }}'{% if not loop.last %},{% endif %} {%- endfor %} } end {% if nullables %} # List of attributes with nullable: true # @!visibility private def self.openapi_nullable Set.new([ {%- for attr in nullables %} :'{{ attr|attribute_name }}', {%- endfor %} ]) end {% endif %} # Initializes the object # @param attributes [Hash] Model attributes in the form of hash # @!visibility private def initialize(attributes = {}) {%- if model.deprecated %} warn "[DEPRECATION] `{{ name }}` is deprecated." {%- endif %} if (!attributes.is_a?(Hash)) fail ArgumentError, "The input argument (attributes) must be a hash in `{{ module_name }}::{{ version|upper }}::{{ name }}` initialize method" end {# preserve blank line #} {%- if model.get("additionalProperties") %} self.additional_properties = {} {%- endif %} # 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)) {%- if model.get("additionalProperties") %} self.additional_properties[k.to_sym] = v else h[k.to_sym] = v end {%- else %} fail ArgumentError, "`#{k}` is not a valid attribute in `{{ module_name }}::{{ version|upper }}::{{ name }}`. 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 {%- endif %} } {%- for attr, definition in model.get("properties", {}).items() %} if attributes.key?(:'{{ attr|attribute_name }}') {%- if definition.get("type", "object") == "array" %} if (value = attributes[:'{{ attr|attribute_name }}']).is_a?(Array) self.{{ attr|attribute_name }} = value end {%- else %} self.{{ attr|attribute_name }} = attributes[:'{{ attr|attribute_name }}'] {%- endif %} end {%- endfor %} end # Check to see if the all the properties in the model are valid # @return true if the model is valid # @!visibility private def valid? {%- for attr, definition in model.get("properties", {}).items() %} {%- set required = attr in model.get("required", []) %} {%- if not definition.nullable and required %} return false if @{{ attr|attribute_name }}.nil? {%- endif %} {%- if definition.maxLength is defined %} return false if {% if not required %}!@{{ attr|attribute_name }}.nil? && {% endif %}@{{ attr|attribute_name }}.to_s.length > {{ definition.maxLength }} {%- endif %} {%- if definition.minLength is defined %} return false if {% if not required %}!@{{ attr|attribute_name }}.nil? && {% endif %}@{{ attr|attribute_name }}.to_s.length < {{ definition.minLength }} {%- endif %} {%- if definition.maximum is defined %} return false if {% if not required %}!@{{ attr|attribute_name }}.nil? && {% endif %}@{{ attr|attribute_name }} >{% if definition.exclusiveMaximum %}={% endif %} {{ definition.maximum }} {%- endif %} {%- if definition.minimum is defined %} return false if {% if not required %}!@{{ attr|attribute_name }}.nil? && {% endif %}@{{ attr|attribute_name }} <{% if definition.exclusiveMinimum %}={% endif %} {{ definition.minimum }} {%- endif %} {%- if definition.pattern is defined %} pattern = Regexp.new({{ definition.pattern }}) return false if {% if not required %}!@{{ attr|attribute_name }}.nil? && {% endif %}@{{ attr|attribute_name }} !~ pattern {%- endif %} {%- if definition.maxItems is defined %} return false if {% if not required %}!@{{ attr|attribute_name }}.nil? && {% endif %}@{{ attr|attribute_name }}.length > {{ definition.maxItems }} {%- endif %} {%- if definition.minItems is defined %} return false if {% if not required %}!@{{ attr|attribute_name }}.nil? && {% endif %}@{{ attr|attribute_name }}.length < {{ definition.minItems }} {%- endif %} {%- endfor %} true end {%- for attr, definition in model.get("properties", {}).items() %} {%- set required = attr in model.get("required", []) %} {%- set has_validation = ( (not definition.nullable and required) or definition.maxLength is defined or definition.minLength is defined or definition.maximum is defined or definition.minimum is defined or definition.pattern is defined or definition.maxItems is defined or definition.minItems is defined ) %}{# FIXME add validation for enum or definition.enum is defined #} {%- if has_validation %} # Custom attribute writer method with validation # @param {{ attr|attribute_name }} [Object] Object to be assigned # @!visibility private def {{ attr|attribute_name }}=({{ attr|attribute_name }}) {%- if False and definition.enum is defined %} validator = {{ module_name }}::{{ version|upper }}::EnumAttributeValidator.new('{{ get_name(definition) }}', [{% for value in definition.enum %}{{ value|format_value }}{%- if not loop.last %}, {% endif %}{% endfor %}]) unless validator.valid?({{ attr|attribute_name }}) fail ArgumentError, "invalid value for \"{{ attr|attribute_name }}\", must be one of #{validator.allowable_values}." end {%- endif %} {%- if not definition.nullable and required %} if {{ attr|attribute_name }}.nil? fail ArgumentError, 'invalid value for "{{ attr|attribute_name }}", {{ attr|attribute_name }} cannot be nil.' end {%- endif %} {%- if definition.maxLength is defined %} if {% if not required %}!{{ attr|attribute_name }}.nil? && {% endif %}{{ attr|attribute_name }}.to_s.length > {{ definition.maxLength }} fail ArgumentError, 'invalid value for "{{ attr|attribute_name }}", the character length must be smaller than or equal to {{ definition.maxLength }}.' end {%- endif %} {%- if definition.minLength is defined %} if {% if not required %}!{{ attr|attribute_name }}.nil? && {% endif %}{{ attr|attribute_name }}.to_s.length < {{ definition.minLength }} fail ArgumentError, 'invalid value for "{{ attr|attribute_name }}", the character length must be great than or equal to {{ definition.minLength }}.' end {%- endif %} {%- if definition.maximum is defined %} if {% if not required %}!{{ attr|attribute_name }}.nil? && {% endif %}{{ attr|attribute_name }} >{% if definition.exclusiveMaximum %}={% endif %} {{ definition.maximum }} fail ArgumentError, 'invalid value for "{{ attr|attribute_name }}", must be smaller than {% if not definition.exclusiveMaximum %}or equal to {% endif %}{{ definition.maximum }}.' end {%- endif %} {%- if definition.minimum is defined %} if {% if not required %}!{{ attr|attribute_name }}.nil? && {% endif %}{{ attr|attribute_name }} <{% if definition.exclusiveMinimum %}={% endif %} {{ definition.minimum }} fail ArgumentError, 'invalid value for "{{ attr|attribute_name }}", must be greater than {% if not definition.exclusiveMinimum %}or equal to {% endif %}{{ definition.minimum }}.' end {%- endif %} {%- if definition.pattern is defined %} pattern = Regexp.new({{ definition.pattern }}) if {% if not required %}!{{ attr|attribute_name }}.nil? && {% endif %}{{ attr|attribute_name }} !~ pattern fail ArgumentError, "invalid value for \"{{ attr|attribute_name }}\", must conform to the pattern #{pattern}." end {%- endif %} {%- if definition.maxItems is defined %} if {% if not required %}!{{ attr|attribute_name }}.nil? && {% endif %}{{ attr|attribute_name }}.length > {{ definition.maxItems }} fail ArgumentError, 'invalid value for "{{ attr|attribute_name }}", number of items must be less than or equal to {{ definition.maxItems }}.' end {%- endif %} {%- if definition.minItems is defined %} if {% if not required %}!{{ attr|attribute_name }}.nil? && {% endif %}{{ attr|attribute_name }}.length < {{ definition.minItems }} fail ArgumentError, 'invalid value for "{{ attr|attribute_name }}", number of items must be greater than or equal to {{ definition.minItems }}.' end {%- endif %} @{{ attr|attribute_name }} = {{ attr|attribute_name }} end {%- endif %} {%- endfor %} {%- if model.get("additionalProperties") %} # Returns the object in the form of hash, with additionalProperties support. # @return [Hash] Returns the object in the form of hash # @!visibility private def to_hash hash = {} self.class.attribute_map.each_pair do |attr, param| value = self.send(attr) if value.nil? is_nullable = self.class.openapi_nullable.include?(attr) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) end hash[param] = _to_hash(value) end self.additional_properties.each_pair do |attr, value| hash[attr] = value end hash end {%- endif %} # Checks equality by comparing each attribute. # @param o [Object] Object to be compared # @!visibility private def ==(o) return true if self.equal?(o) self.class == o.class && {%- for attr in model.properties %} {{ attr|attribute_name }} == o.{{ attr|attribute_name }}{% if not loop.last or model.get("additionalProperties") %} &&{% endif %} {%- endfor %} {%- if model.get("additionalProperties") %} additional_properties == o.additional_properties {%- endif %} end # Calculates hash code according to all attributes. # @return [Integer] Hash code # @!visibility private def hash [{% for attr in model.properties %}{{ attr|attribute_name }}{% if not loop.last or model.get("additionalProperties") %}, {% endif %}{% endfor %}{% if model.get("additionalProperties") %}additional_properties{% endif %}].hash end end