Skip to content

Commit

Permalink
convert nested classes in Twitter::Base#attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
stve authored and sferik committed Mar 14, 2013
1 parent 68915e0 commit e56c34c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/twitter/base.rb
Expand Up @@ -94,7 +94,11 @@ def [](method)
# @return [Hash]
def attrs
@attrs.inject({}) do |attrs, (key, value)|
attrs.merge!(key => respond_to?(key) ? send(key) : value)
if value.respond_to?(:attrs)
attrs.merge!(key => value.attrs)
else
attrs.merge!(key => respond_to?(key) ? send(key) : value)
end
end
end
alias to_hash attrs
Expand Down
11 changes: 11 additions & 0 deletions spec/twitter/base_spec.rb
Expand Up @@ -108,4 +108,15 @@
end
end

describe '#attrs' do
it 'returns a hash of attributes' do
expect(Twitter::Base.new(:id => 1).attrs).to eq({:id => 1})
end

it 'converts nested classes' do
base = Twitter::Base.new(:id => 2, :user => Twitter::User.new(:id => 4))
expect(base.attrs).to eq({:id => 2, :user => {:id => 4}})
end
end

end

0 comments on commit e56c34c

Please sign in to comment.