Class: RedditKit::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/redditkit/base.rb

Overview

A base class for RedditKit’s model objects, automatically generating attribute and predicate methods.

Direct Known Subclasses

Multireddit, MultiredditDescription, Thing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



35
36
37
38
39
40
41
# File 'lib/redditkit/base.rb', line 35

def initialize(attributes = {})
  kind = attributes[:kind]
  data = attributes[:data]

  @attributes = data || {}
  @attributes[:kind] = kind
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/redditkit/base.rb', line 6

def attributes
  @attributes
end

Class Method Details

.attr_reader(*attrs) ⇒ Object



10
11
12
13
14
15
# File 'lib/redditkit/base.rb', line 10

def attr_reader(*attrs)
  attrs.each do |attr|
    define_attribute_method(attr)
    define_predicate_method(attr)
  end
end

Instance Method Details

#[](method) ⇒ Object



43
44
45
46
47
# File 'lib/redditkit/base.rb', line 43

def [](method)
  send(method.to_sym)
rescue NoMethodError
  nil
end