Skip to content
Newer
Older
100644 100 lines (88 sloc) 2.31 KB
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
1 require 'forwardable'
17880f4 @sferik Respond with NullObject if Tweet has no User
authored
2 require 'twitter/null_object'
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
3
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
4 module Twitter
5 class Base
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
6 extend Forwardable
7 attr_reader :attrs
8 alias to_h attrs
9 alias to_hash attrs
10 alias to_hsh attrs
11 def_delegators :attrs, :delete, :update
12
9e032aa @sferik Flesh out YARD documentation
authored
13 # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
14 #
3e8c4ac @sferik Remove explicit receiver where unnecessary
authored
15 # @param attrs [Array, Set, Symbol]
9c29181 @sferik Rename lazy_attr_reader to attr_reader since it's not actually lazy
authored
16 def self.attr_reader(*attrs)
84966f6 @sferik Mix-in anonymous module instead of defining reader methods directly o…
authored
17 mod = Module.new do
18 attrs.each do |attribute|
6732706 @sferik Actually, ivar assignment/caching is unnecessary
authored
19 define_method attribute do
d5b5d87 @sferik Use symbols as keys
authored
20 @attrs[attribute.to_sym]
6fb7b3f @sferik Implement lazy attribute setting
authored
21 end
a6da19b @sferik Automatically define inquirer methods
authored
22 define_method "#{attribute}?" do
23 !!@attrs[attribute.to_sym]
24 end
6732706 @sferik Actually, ivar assignment/caching is unnecessary
authored
25 end
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
26 end
441d7a0 @sferik Give the anonymous module a name
authored
27 const_set(:Attributes, mod)
ba61a3e @sferik Using send is not necessary
authored
28 include mod
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
29 end
30
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
31 # Construct an object from the response hash
ca8e285 @stve separate object initialization from identity map for #285
stve authored
32 #
f41d912 @sferik Add note about Ruby 1.8 hacks [ci skip]
authored
33 # @param response [Hash]
ca8e285 @stve separate object initialization from identity map for #285
stve authored
34 # @return [Twitter::Base]
4c63a73 @sferik Add Twitter::RateLimit class
authored
35 def self.from_response(response={})
ec7c2df @sferik Remove identity map
authored
36 new(response[:body])
218479f @sferik Implement identity map!
authored
37 end
38
99efde0 @sferik Add update method
authored
39 # Initializes a new object
9e032aa @sferik Flesh out YARD documentation
authored
40 #
41 # @param attrs [Hash]
42 # @return [Twitter::Base]
241d158 @sferik Update RateLimit via Faraday middleware
authored
43 def initialize(attrs={})
ec7c2df @sferik Remove identity map
authored
44 @attrs = attrs || {}
d63d2e5 @sferik Allow [] access to methods for greater backwards compatibility
authored
45 end
46
99efde0 @sferik Add update method
authored
47 # Fetches an attribute of an object using hash notation
9e032aa @sferik Flesh out YARD documentation
authored
48 #
49 # @param method [String, Symbol] Message to send to the object
6fb7b3f @sferik Implement lazy attribute setting
authored
50 def [](method)
4bcd678 @sferik Don't cache dynamic attributes
authored
51 send(method.to_sym)
acc1c79 @sferik Make Twitter::Base#[] behave more like a hash
authored
52 rescue NoMethodError
53 nil
cc1e2e4 @sferik Add #user method to Status class
authored
54 end
1dd37f2 @sferik Method spacing
authored
55
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
56 # Create a new object from another object
99efde0 @sferik Add update method
authored
57 #
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
58 # @param klass [Class]
59 # @param key1 [Symbol]
60 # @param key2 [Symbol]
61 def new_without_self(klass, key1, key2)
62 if @attrs[key1]
63 attrs = @attrs.dup
64 value = attrs.delete(key1)
65 klass.new(value.update(key2 => attrs))
17880f4 @sferik Respond with NullObject if Tweet has no User
authored
66 else
67 NullObject.new
68 end
69 end
70
71 # Create a new object (or NullObject) from attributes
72 #
73 # @param klass [Class]
74 # @param key [Symbol]
75 def new_or_null_object(klass, key)
76 if @attrs[key]
77 klass.new(@attrs[key])
78 else
79 NullObject.new
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
80 end
99efde0 @sferik Add update method
authored
81 end
82
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
83 private
77d9283 @sferik Make object equality more strict
authored
84
aaa1856 @sferik Refactor _equal methods into Twitter::Base#attr_equal
authored
85 # @param attr [Symbol]
86 # @param other [Twitter::Base]
87 # @return [Boolean]
88 def attr_equal(attr, other)
4bcd678 @sferik Don't cache dynamic attributes
authored
89 self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
aaa1856 @sferik Refactor _equal methods into Twitter::Base#attr_equal
authored
90 end
91
77d9283 @sferik Make object equality more strict
authored
92 # @param other [Twitter::Base]
93 # @return [Boolean]
94 def attrs_equal(other)
4bcd678 @sferik Don't cache dynamic attributes
authored
95 self.class == other.class && !other.attrs.empty? && attrs == other.attrs
77d9283 @sferik Make object equality more strict
authored
96 end
97
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
98 end
99 end
Something went wrong with that request. Please try again.