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

     1      # Builds the object from hash
     2      # @param [Hash] attributes Model attributes in the form of hash
     3      # @return [Object] Returns the model itself
     4      def self.build_from_hash(attributes)
     5        new.build_from_hash(attributes)
     6      end
     7  
     8      # Builds the object from hash
     9      # @param [Hash] attributes Model attributes in the form of hash
    10      # @return [Object] Returns the model itself
    11      def build_from_hash(attributes)
    12        return nil unless attributes.is_a?(Hash)
    13        {{#parent}}
    14        super(attributes)
    15        {{/parent}}
    16        self.class.openapi_types.each_pair do |key, type|
    17          if type =~ /\AArray<(.*)>/i
    18            # check to ensure the input is an array given that the attribute
    19            # is documented as an array but the input is not
    20            if attributes[self.class.attribute_map[key]].is_a?(Array)
    21              self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
    22            end
    23          elsif !attributes[self.class.attribute_map[key]].nil?
    24            self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
    25          end # or else data not found in attributes(hash), not an issue as the data can be optional
    26        end
    27  
    28        self
    29      end
    30  
    31      # Deserializes the data based on type
    32      # @param string type Data type
    33      # @param string value Value to be deserialized
    34      # @return [Object] Deserialized data
    35      def _deserialize(type, value)
    36        case type.to_sym
    37        when :DateTime
    38          DateTime.parse(value)
    39        when :Date
    40          Date.parse(value)
    41        when :Time
    42          Time.parse(value)
    43        when :String
    44          value.to_s
    45        when :Integer
    46          value.to_i
    47        when :Float
    48          value.to_f
    49        when :Boolean
    50          if value.to_s =~ /\A(true|t|yes|y|1)\z/i
    51            true
    52          else
    53            false
    54          end
    55        when :Object
    56          # generic object (usually a Hash), return directly
    57          value
    58        when /\AArray<(?<inner_type>.+)>\z/
    59          inner_type = Regexp.last_match[:inner_type]
    60          value.map { |v| _deserialize(inner_type, v) }
    61        when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
    62          k_type = Regexp.last_match[:k_type]
    63          v_type = Regexp.last_match[:v_type]
    64          {}.tap do |hash|
    65            value.each do |k, v|
    66              hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
    67            end
    68          end
    69        else # model
    70          {{moduleName}}.const_get(type).build_from_hash(value)
    71        end
    72      end
    73  
    74      # Returns the string representation of the object
    75      # @return [String] String presentation of the object
    76      def to_s
    77        to_hash.to_s
    78      end
    79  
    80      # to_body is an alias to to_hash (backward compatibility)
    81      # @return [Hash] Returns the object in the form of hash
    82      def to_body
    83        to_hash
    84      end
    85  
    86      # Returns the object in the form of hash
    87      # @return [Hash] Returns the object in the form of hash
    88      def to_hash
    89        hash = {{^parent}}{}{{/parent}}{{#parent}}super{{/parent}}
    90        self.class.attribute_map.each_pair do |attr, param|
    91          value = self.send(attr)
    92          if value.nil?
    93            is_nullable = self.class.openapi_nullable.include?(attr)
    94            next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
    95          end
    96  
    97          hash[param] = _to_hash(value)
    98        end
    99        hash
   100      end
   101  
   102      # Outputs non-array value in the form of hash
   103      # For object, use to_hash. Otherwise, just return the value
   104      # @param [Object] value Any valid value
   105      # @return [Hash] Returns the value in the form of hash
   106      def _to_hash(value)
   107        if value.is_a?(Array)
   108          value.compact.map { |v| _to_hash(v) }
   109        elsif value.is_a?(Hash)
   110          {}.tap do |hash|
   111            value.each { |k, v| hash[k] = _to_hash(v) }
   112          end
   113        elsif value.respond_to? :to_hash
   114          value.to_hash
   115        else
   116          value
   117        end
   118      end