Class: Redd::Base
Overview
A container for various models with attributes and a client.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
(also: #to_h)
readonly
A list of attributes returned by reddit for this object.
-
#client ⇒ Object
readonly
The client instance used to make requests with this object.
Class Method Summary collapse
-
.attr_reader(attr) ⇒ Object
Define and memoize the method that returns a key from the attributes hash.
- .define_attribute_method(method) ⇒ Object private
- .define_predicate_method(method) ⇒ Object private
Instance Method Summary collapse
- #[](method) ⇒ Object
-
#initialize(client, attributes) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(client, attributes) ⇒ Base
Returns a new instance of Base.
34 35 36 37 38 |
# File 'lib/redd/base.rb', line 34 def initialize(client, attributes) @client = client @attributes = attributes[:data] @attributes[:kind] = attributes[:kind] end |
Instance Attribute Details
#attributes ⇒ Hash (readonly) Also known as: to_h
Returns A list of attributes returned by reddit for this object.
10 11 12 |
# File 'lib/redd/base.rb', line 10 def attributes @attributes end |
#client ⇒ Object (readonly)
Returns The client instance used to make requests with this object.
15 16 17 |
# File 'lib/redd/base.rb', line 15 def client @client end |
Class Method Details
.attr_reader(attr) ⇒ Object
Define and memoize the method that returns a key from the attributes hash.
20 21 22 23 |
# File 'lib/redd/base.rb', line 20 def self.attr_reader(attr) define_attribute_method(attr) define_predicate_method(attr) end |
.define_attribute_method(method) ⇒ Object (private)
42 43 44 45 |
# File 'lib/redd/base.rb', line 42 def self.define_attribute_method(method) define_method(method) { @attributes[method] } memoize method end |
.define_predicate_method(method) ⇒ Object (private)
47 48 49 50 |
# File 'lib/redd/base.rb', line 47 def self.define_predicate_method(method) define_method(:"#{method}?") { !!@attributes[method] } memoize :"#{method}?" end |
Instance Method Details
#[](method) ⇒ Object
25 26 27 28 29 |
# File 'lib/redd/base.rb', line 25 def [](method) send(method.to_sym) rescue NoMethodError nil end |