Class: Redd::Base

Inherits:
Object
  • Object
show all
Includes:
Memoizable
Defined in:
lib/redd/base.rb

Overview

A container for various models with attributes and a client.

Direct Known Subclasses

Object::MoreComments, Object::WikiPage, Thing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes) ⇒ Base

Returns a new instance of Base.

Parameters:

  • client

    The client to use when making requests with this object. This is similar to reddit_session in praw.

  • attributes (Hash)


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

#attributesHash (readonly) Also known as: to_h

Returns A list of attributes returned by reddit for this object.

Returns:

  • (Hash)

    A list of attributes returned by reddit for this object.



10
11
12
# File 'lib/redd/base.rb', line 10

def attributes
  @attributes
end

#clientObject (readonly)

Returns The client instance used to make requests with this object.

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.

Parameters:

  • attr (Symbol, String)

    The attribute to construct a method out of.



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