Permalink
Browse files

convert nested classes in Twitter::Base#attrs

  • Loading branch information...
stve authored and sferik committed Mar 12, 2013
1 parent 68915e0 commit e56c34c640189eb8b25a16994676a5e82b783cb3
Showing with 16 additions and 1 deletion.
  1. +5 −1 lib/twitter/base.rb
  2. +11 −0 spec/twitter/base_spec.rb
View
@@ -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
View
@@ -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.