Skip to content

Commit

Permalink
Return a Twitter::NullObject for empty attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 20, 2014
1 parent 7bf3a1b commit bca179e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def define_uri_method(key1, key2)
# @param key2 [Symbol]
def define_attribute_method(key1, klass = nil, key2 = nil)
define_method(key1) do ||
if klass.nil?
@attrs[key1]
if @attrs[key1].nil? || @attrs[key1].respond_to?(:empty?) && @attrs[key1].empty?
NullObject.new
else
if @attrs[key1].nil?
NullObject.new
if klass.nil?
@attrs[key1]
else
attrs = attrs_for_object(key1, key2)
Twitter.const_get(klass).new(attrs)
Expand All @@ -115,7 +115,7 @@ def deprecate_attribute_method(key)
# @param key2 [Symbol]
def define_predicate_method(key1, key2 = key1)
define_method(:"#{key1}?") do ||
!@attrs[key2].nil? && @attrs[key2] != false
!@attrs[key2].nil? && @attrs[key2] != false && !(@attrs[key2].respond_to?(:empty?) && @attrs[key2].empty?)
end
memoize(:"#{key1}?")
end
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/tweet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@

describe '#metadata' do
it 'returns a Twitter::Metadata when metadata is set' do
tweet = Twitter::Tweet.new(:id => 28_669_546_014, :metadata => {})
tweet = Twitter::Tweet.new(:id => 28_669_546_014, :metadata => {:result_type => 'recent'})
expect(tweet.metadata).to be_a Twitter::Metadata
end
it 'returns nil when metadata is not set' do
Expand All @@ -221,7 +221,7 @@

describe '#metadata?' do
it 'returns true when metadata is set' do
tweet = Twitter::Tweet.new(:id => 28_669_546_014, :metadata => {})
tweet = Twitter::Tweet.new(:id => 28_669_546_014, :metadata => {:result_type => 'recent'})
expect(tweet.metadata?).to be true
end
it 'returns false when metadata is not set' do
Expand Down

0 comments on commit bca179e

Please sign in to comment.