Class: Redd::Models::BasicModel
- Inherits:
-
Object
- Object
- Redd::Models::BasicModel
- Defined in:
- lib/redd/models/basic_model.rb
Overview
The base class for all models.
Direct Known Subclasses
Access, FrontPage, LazyModel, Listing, LiveThread::LiveUpdate, ModMail, ModMail::Message, MoreComments, Session, Subreddit::ModAction
Instance Attribute Summary collapse
-
#client ⇒ APIClient
readonly
The client the model was initialized with.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(client, attributes = {}) ⇒ BasicModel
constructor
Create a non-lazily initialized class.
-
#inspect ⇒ String
An easily readable representation of the object.
-
#method_missing(method_name, *args, &block) ⇒ Object
Return an attribute or raise a NoMethodError if it doesn’t exist.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Checks whether an attribute is supported by method_missing.
-
#to_ary ⇒ Array<self>
An array representation of self.
-
#to_h ⇒ Hash
A Hash representation of the object.
Constructor Details
#initialize(client, attributes = {}) ⇒ BasicModel
Create a non-lazily initialized class.
22 23 24 25 26 |
# File 'lib/redd/models/basic_model.rb', line 22 def initialize(client, attributes = {}) @client = client @attributes = attributes after_initialize end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Return an attribute or raise a NoMethodError if it doesn’t exist.
54 55 56 57 58 |
# File 'lib/redd/models/basic_model.rb', line 54 def method_missing(method_name, *args, &block) return get_attribute(method_name) if @attributes.key?(method_name) return get_attribute(depredicate(method_name)) if @attributes.key?(depredicate(method_name)) super end |
Instance Attribute Details
#client ⇒ APIClient (readonly)
Returns the client the model was initialized with.
17 18 19 |
# File 'lib/redd/models/basic_model.rb', line 17 def client @client end |
Class Method Details
.from_id(_client, _value) ⇒ BasicModel
Create an instance from a value.
11 12 13 14 |
# File 'lib/redd/models/basic_model.rb', line 11 def self.from_id(_client, _value) # TODO: abstract this out? raise "coercion not implemented for #{name}" end |
Instance Method Details
#inspect ⇒ String
Returns an easily readable representation of the object.
39 40 41 |
# File 'lib/redd/models/basic_model.rb', line 39 def inspect "#{super}\n" + @attributes.map { |a, v| " #{a}: #{v}" }.join("\n") end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Checks whether an attribute is supported by method_missing.
47 48 49 |
# File 'lib/redd/models/basic_model.rb', line 47 def respond_to_missing?(method_name, include_private = false) @attributes.key?(method_name) || @attributes.key?(depredicate(method_name)) || super end |
#to_ary ⇒ Array<self>
Returns an array representation of self.
34 35 36 |
# File 'lib/redd/models/basic_model.rb', line 34 def to_ary [self] end |
#to_h ⇒ Hash
Returns a Hash representation of the object.
29 30 31 |
# File 'lib/redd/models/basic_model.rb', line 29 def to_h @attributes end |