Class: Aws::Structure

Inherits:
Struct
  • Object
show all
Defined in:
lib/aws-sdk-core/structure.rb

Overview

A utilty class that makes it easier to work with Struct objects.

## Construction

You can construct a Structure with a simple hash.

person = Structure.new(name: 'John Doe', age: 40)
#=> #<struct  name="John Doe", age=40>

## Empty Structures

The stdlib Struct class does not work with empty member lists. Structure solves this by introducing the EmptyStructure class.

struct = Structure.new({})
#=> #<struct>

## Structure Classes

In addition to simpler object construction, struct classes are re-used automatically.

person1 = Structure.new(name: 'John Doe', age: 40)
person2 = Structure.new(name: 'Jane Doe', age: 40)

person1.class == person2.class

## Hash Conversion

Calling #to_h or #to_hash on a Structure object performs a deep conversion of Structure objects into hashes.

person = Structure.new(
  name: "John",
  age: 40,
  friend: Structure.new(name: "Jane", age: 40, friend: nil)
)
person.to_h
#=> {:name=>"John", :age=>40, :friend=>{:name=>"Jane", :age=>40}}

Direct Known Subclasses

EmptyStructure

Constant Summary collapse

@@classes =
{}
@@classes_mutex =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deeply converts hashes to Structure objects. Hashes with string keys are not converted, but their values are.



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/aws-sdk-core/structure.rb', line 155

def from_hash(value)
  case value
  when Hash
    data = value.each.with_object({}) do |(key, value), hash|
      hash[key] = from_hash(value)
    end
    Symbol === data.keys.first ? new(data) : data
  when Array
    value.map { |v| from_hash(v) }
  else value
  end
end

.new(member_names) ⇒ Struct .new(*member_names) ⇒ Struct .new(members) ⇒ Struct .newEmptyStructure

Defines a Struct class with the given member names. Returns an instance of that class with nil member values.

struct = Structure.new([:name, :age])
struct.members
#=> [:name, :age]

struct[:name] #=> nil
struct[:age] #=> nil

You can provide an ordered list of values to initialize structure members with:

struct = Structure.new([:name, :age], ['John Doe', 40])
struct.members
#=> [:name, :age]

struct[:name] #=> 'John Doe'
struct[:age] #=> 40

Calling new multiple times with the same list of members will reuse Struct classes.

struct1 = Structure.new([:name, :age])
struct2 = Structure.new([:name, :age])

struct1.class.equal?(struct2.class)
#=> true

Calling new without members, or with an empty list of members will return an EmptyStructure:

struct = Structure.new
struct.members
#=> []

You can also create an empty Structure via EmptyStructure.

Overloads:

  • .new(member_names) ⇒ Struct

    Parameters:

    • member_names (Array<Symbol>)

      An array of member names.

    Returns:

    • (Struct)
  • .new(*member_names) ⇒ Struct

    Parameters:

    • member_names (Symbol)

      A list of member names.

    Returns:

    • (Struct)
  • .new(members) ⇒ Struct

    Parameters:

    • members (Hash<Symbol,Object>)

      A hash of member names and values.

    Returns:

    • (Struct)
  • .newEmptyStructure

    Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/aws-sdk-core/structure.rb', line 136

def new(*args)
  members, values = parse_args(args)
  if members.empty? && self == Structure
    EmptyStructure.new
  else
    struct_class = @@classes[members]
    if struct_class.nil?
      @@classes_mutex.synchronize do
        struct_class = members.empty? ? super(:_) : super(*members)
        @@classes[members] = struct_class
      end
    end
    struct_class.new(*values)
  end
end

Instance Method Details

#orig_to_hObject



51
# File 'lib/aws-sdk-core/structure.rb', line 51

alias orig_to_h to_h

#to_h(obj = self) ⇒ Hash Also known as: to_hash

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.

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aws-sdk-core/structure.rb', line 61

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