Sha256: f91778e1c5efb5a777cf85e59dd24e439653998381ec6954240b40aa542a1b7c

Contents?: true

Size: 1.34 KB

Versions: 719

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module TencentCloud
  module Common
    # common model
    class AbstractModel
      def serialize
        flat(self)
      end

      def self.format_params(prefix = nil, params)
        d = {}
        case params
        when Hash
          params.each do |k, v|
            key = prefix ? "#{prefix}.#{k}" : k.to_s
            d.update(format_params(key, v))
          end
        when Array
          params.each_with_index do |v, i|
            key = prefix ? "#{prefix}.#{i}" : i.to_s
            d.update(format_params(key, v))
          end
        else
          d[prefix] = params
        end
        d
      end

      private

      def flat(obj)
        case obj
        when AbstractModel
          params = nil
          keys = obj.instance_variables
          keys.each do |k|
            value = obj.instance_variable_get k
            key = k.to_s.split('@').at 1
            next unless value

            r = flat(value)
            unless r.nil?
              params ||= {}
              params[key] = r
            end
          end
          params
        when Array
          arr = nil
          obj.each do |v|
            r = flat(v)
            unless r.nil?
              arr ||= []
              arr << r
            end
          end
          arr
        else
          obj
        end
      end
    end
  end
end

Version data entries

719 entries across 719 versions & 1 rubygems

Version Path
tencentcloud-sdk-common-3.0.951 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.950 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.949 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.948 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.947 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.946 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.945 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.944 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.943 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.942 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.941 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.940 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.939 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.938 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.937 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.936 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.935 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.934 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.933 lib/tencentcloud-sdk-common/models.rb
tencentcloud-sdk-common-3.0.932 lib/tencentcloud-sdk-common/models.rb