Skip to content

Commit

Permalink
Structure classes can now be initialized with hashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorrowe committed Jun 6, 2015
1 parent 1aa6b93 commit d6d793c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions aws-sdk-core/lib/aws-sdk-core/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class Structure < Struct
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)
Expand Down
7 changes: 6 additions & 1 deletion aws-sdk-core/spec/aws/structure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ module Aws
expect(Structure.new(:abc, :xyz).members).to eq([:abc, :xyz])
end

it 'accepts values to its constructor as a hash, not, positional' do
klass = Structure.new(:name)
expect(klass.new(name:'value').name).to eq('value')
end

describe '#to_hash' do

it 'returns a hash' do
expect(Structure.new(:abc).new.to_hash).to eq({})
end

it 'only serializes non-nil members' do
s = Structure.new(:abc, :mno).new('abc')
s = Structure.new(:abc, :mno).new(abc:'abc')
expect(s.to_hash).to eq(abc: 'abc')
end

Expand Down

0 comments on commit d6d793c

Please sign in to comment.