Skip to content

Commit

Permalink
Merge pull request #32 from tkowark/to_attrs_fix
Browse files Browse the repository at this point in the history
Test and fix for the #to_attrs method of Sawyer::Resource
  • Loading branch information
pengwynn committed Dec 2, 2014
2 parents 826e09b + 4160a3d commit 454ca30
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sawyer/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def to_attrs
hash.keys.each do |k|
if hash[k].is_a?(Sawyer::Resource)
hash[k] = hash[k].to_attrs
elsif hash[k].is_a?(Array) && hash[k].all?{|el| el.is_a?(Sawyer::Resource)}
hash[k] = hash[k].collect{|el| el.to_attrs}
end
end
hash
Expand Down
6 changes: 6 additions & 0 deletions test/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def test_to_h
hash = {:a => 1 }
assert_equal hash, res.to_h
end

def test_to_attrs_for_sawyer_resource_arrays
res = Resource.new @agent, :a => 1, :b => [Resource.new(@agent, :a => 2)]
hash = {:a => 1, :b => [{:a => 2}]}
assert_equal hash, res.to_attrs
end

def test_handle_hash_notation_with_string_key
res = Resource.new @agent, :a => 1
Expand Down

0 comments on commit 454ca30

Please sign in to comment.