From d6d793c44cedba20234c031e97f3b4ff75c8926d Mon Sep 17 00:00:00 2001 From: Trevor Rowe Date: Fri, 5 Jun 2015 16:12:42 -0700 Subject: [PATCH] Structure classes can now be initialized with hashes. --- aws-sdk-core/lib/aws-sdk-core/structure.rb | 6 ++++++ aws-sdk-core/spec/aws/structure_spec.rb | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/aws-sdk-core/lib/aws-sdk-core/structure.rb b/aws-sdk-core/lib/aws-sdk-core/structure.rb index 0023e9b4392..a5e3f789681 100644 --- a/aws-sdk-core/lib/aws-sdk-core/structure.rb +++ b/aws-sdk-core/lib/aws-sdk-core/structure.rb @@ -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) diff --git a/aws-sdk-core/spec/aws/structure_spec.rb b/aws-sdk-core/spec/aws/structure_spec.rb index 2bb950524de..a8ce27f34c1 100644 --- a/aws-sdk-core/spec/aws/structure_spec.rb +++ b/aws-sdk-core/spec/aws/structure_spec.rb @@ -11,6 +11,11 @@ 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 @@ -18,7 +23,7 @@ module Aws 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