1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
# frozen_string_literal: true
module Aws
# @api private
module Structure
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.each_pair.with_object({}) do |(member, value), hash|
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
# Wraps the default #to_s logic with filtering of sensitive parameters.
def to_s(obj = self)
Aws::Log::ParamFilter.new.filter(obj, obj.class).to_s
end
class << self
# @api private
def new(*args)
if args.empty?
Aws::EmptyStructure
else
struct = Struct.new(*args)
struct.send(:include, Aws::Structure)
struct
end
end
# @api private
def self.included(base_class)
base_class.send(:undef_method, :each)
end
end
end
# @api private
class EmptyStructure < Struct.new('AwsEmptyStructure')
include(Aws::Structure)
end
end
|