Sha256: 47e54d7c4e064dec3035a80bb9b79a5bbf992863f769e2b4ace05c39ac166b6b

Contents?: true

Size: 1.51 KB

Versions: 997

Compression:

Stored size: 1.51 KB

Contents

module Aws
  # @api private
  class Structure < Struct

    if Struct.instance_methods.include?(:to_h)
      alias orig_to_h to_h
    end

    def initialize(values = {})
      values.each do |k, v|
        self[k] = v
      end
    end

    # @return [Boolean] Returns `true` if this structure has a value
    #   set for the given member.
    def key?(member_name)
      !self[member_name].nil?
    end

    # @return [Boolean] Returns `true` if all of the member values are `nil`.
    def empty?
      values.compact == []
    end

    # Deeply converts the Structure into a hash.  Structure members that
    # are `nil` are omitted from the resultant hash.
    #
    # You can call #orig_to_h to get vanilla #to_h behavior as defined
    # in stdlib Struct.
    #
    # @return [Hash]
    def to_h(obj = self)
      case obj
      when Struct
        obj.members.each.with_object({}) do |member, hash|
          value = obj[member]
          hash[member] = to_hash(value) unless value.nil?
        end
      when Hash
        obj.each.with_object({}) do |(key, value), hash|
          hash[key] = to_hash(value)
        end
      when Array
        obj.collect { |value| to_hash(value) }
      else
        obj
      end
    end
    alias to_hash to_h

    undef_method :each

    class << self

      # @api private
      def new(*args)
        if args == ['AwsEmptyStructure']
          super
        elsif args.empty? || args.first == []
          EmptyStructure
        else
          super(*args)
        end
      end

    end
  end
end

Version data entries

997 entries across 997 versions & 2 rubygems

Version Path
aws-sdk-core-2.11.632 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.631 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.630 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.629 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.628 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.627 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.626 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.625 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.624 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.623 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.622 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.621 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.620 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.619 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.618 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.617 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.616 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.615 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.614 lib/aws-sdk-core/structure.rb
aws-sdk-core-2.11.613 lib/aws-sdk-core/structure.rb