Class: Twitter::Base
- Inherits:
-
Object
- Object
- Twitter::Base
- Extended by:
- Forwardable
- Includes:
- Memoizable, Utils
- Defined in:
- lib/twitter/base.rb
Direct Known Subclasses
Configuration, Entity, Geo, Identity, Language, Media::VideoInfo, Metadata, OEmbed, ProfileBanner, RateLimit, Relationship, Settings, Size, Streaming::StallWarning, Suggestion, Token, Trend, Variant
Instance Attribute Summary collapse
- #attrs ⇒ Hash (also: #to_h) readonly
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from attributes.
-
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ Object
Dynamically define a method for an attribute.
-
.define_predicate_method(key1, key2 = key1) ⇒ Object
Dynamically define a predicate method for an attribute.
-
.define_uri_method(key1, key2) ⇒ Object
Dynamically define a method for a URI.
- .deprecate_attribute_method(key) ⇒ Object
-
.display_uri_attr_reader ⇒ Object
Define display_uri attribute methods.
-
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object
Define object methods from attributes.
- .predicate_attr_reader(*attrs) ⇒ Object
-
.uri_attr_reader(*attrs) ⇒ Object
Define URI methods from attributes.
Instance Method Summary collapse
-
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation.
-
#initialize(attrs = {}) ⇒ Twitter::Base
constructor
Initializes a new object.
Methods included from Utils
Constructor Details
#initialize(attrs = {}) ⇒ Twitter::Base
Initializes a new object
125 126 127 |
# File 'lib/twitter/base.rb', line 125 def initialize(attrs = {}) @attrs = attrs || {} end |
Instance Attribute Details
#attrs ⇒ Hash (readonly) Also known as: to_h
13 14 15 |
# File 'lib/twitter/base.rb', line 13 def attrs @attrs end |
Class Method Details
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from attributes
22 23 24 25 26 27 |
# File 'lib/twitter/base.rb', line 22 def attr_reader(*attrs) attrs.each do |attr| define_attribute_method(attr) define_predicate_method(attr) end end |
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ Object
Dynamically define a method for an attribute
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/twitter/base.rb', line 86 def define_attribute_method(key1, klass = nil, key2 = nil) define_method(key1) do || if attr_falsey_or_empty?(key1) NullObject.new elsif klass.nil? @attrs[key1] else attrs = attrs_for_object(key1, key2) Twitter.const_get(klass).new(attrs) end end memoize(key1) end |
.define_predicate_method(key1, key2 = key1) ⇒ Object
Dynamically define a predicate method for an attribute
113 114 115 116 117 118 |
# File 'lib/twitter/base.rb', line 113 def define_predicate_method(key1, key2 = key1) define_method(:"#{key1}?") do || !attr_falsey_or_empty?(key2) end memoize(:"#{key1}?") end |
.define_uri_method(key1, key2) ⇒ Object
Dynamically define a method for a URI
74 75 76 77 78 79 |
# File 'lib/twitter/base.rb', line 74 def define_uri_method(key1, key2) define_method(key1) do || Addressable::URI.parse(@attrs[key2].chomp('#')) unless @attrs[key2].nil? end memoize(key1) end |
.deprecate_attribute_method(key) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/twitter/base.rb', line 101 def deprecate_attribute_method(key) define_method(key) do || warn "#{Kernel.caller.first}: [DEPRECATION] ##{key} is deprecated. Use ##{key}? instead." @attrs[key] end memoize(key) end |
.display_uri_attr_reader ⇒ Object
Define display_uri attribute methods
63 64 65 66 67 68 |
# File 'lib/twitter/base.rb', line 63 def display_uri_attr_reader define_attribute_method(:display_url) alias_method(:display_uri, :display_url) define_predicate_method(:display_uri, :display_url) alias_method(:display_url?, :display_uri?) end |
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object
Define object methods from attributes
41 42 43 44 |
# File 'lib/twitter/base.rb', line 41 def object_attr_reader(klass, key1, key2 = nil) define_attribute_method(key1, klass, key2) define_predicate_method(key1) end |
.predicate_attr_reader(*attrs) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/twitter/base.rb', line 29 def predicate_attr_reader(*attrs) attrs.each do |attr| define_predicate_method(attr) deprecate_attribute_method(attr) end end |
.uri_attr_reader(*attrs) ⇒ Object
Define URI methods from attributes
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/twitter/base.rb', line 49 def uri_attr_reader(*attrs) attrs.each do |uri_key| array = uri_key.to_s.split('_') index = array.index('uri') array[index] = 'url' url_key = array.join('_').to_sym define_uri_method(uri_key, url_key) alias_method(url_key, uri_key) define_predicate_method(uri_key, url_key) alias_method(:"#{url_key}?", :"#{uri_key}?") end end |
Instance Method Details
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation
132 133 134 135 136 137 |
# File 'lib/twitter/base.rb', line 132 def [](method) warn "#{Kernel.caller.first}: [DEPRECATION] #[#{method.inspect}] is deprecated. Use ##{method} to fetch the value." send(method.to_sym) rescue NoMethodError nil end |