Skip to content

HTTPS clone URL

Subversion checkout URL

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