class Redd::Models::BasicModel
The base class for all models.
Attributes
@return [APIClient] the client the model was initialized with
Public Class Methods
@abstract Create an instance from a value. @param _client [APIClient] the api client to initialize the object with @param _value [Object] the object to coerce @return [BasicModel]
# 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
Create a non-lazily initialized class. @param client [APIClient] the client that the model uses to make requests @param attributes [Hash] the class's attributes
# File lib/redd/models/basic_model.rb, line 22 def initialize(client, attributes = {}) @client = client @attributes = attributes after_initialize end
Public Instance Methods
@return [String] an easily readable representation of the object
# File lib/redd/models/basic_model.rb, line 39 def inspect "#{super}\n" + @attributes.map { |a, v| " #{a}: #{v}" }.join("\n") end
Return an attribute or raise a NoMethodError if it doesn't exist. @param method_name [Symbol] the name of the attribute @return [Object] the result of the attribute check
# 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
Checks whether an attribute is supported by method_missing. @param method_name [Symbol] the method name or attribute to check @param include_private [Boolean] whether to also include private methods @return [Boolean] whether the method is handled by method_missing
# 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
@return [Array<self>] an array representation of self
# File lib/redd/models/basic_model.rb, line 34 def to_ary [self] end
@return [Hash] a Hash representation of the object
# File lib/redd/models/basic_model.rb, line 29 def to_h @attributes end
Private Instance Methods
@abstract Lets us plug in custom code without making a mess
# File lib/redd/models/basic_model.rb, line 63 def after_initialize; end
Remove a trailing '?' from a symbol name. @param method_name [Symbol] the symbol to “depredicate” @return [Symbol] the symbol but with the '?' removed
# File lib/redd/models/basic_model.rb, line 68 def depredicate(method_name) method_name.to_s.chomp('?').to_sym end
Get an attribute, raising KeyError if not present. @param name [Symbol] the attribute to check and get @return [Object] the value of the attribute
# File lib/redd/models/basic_model.rb, line 75 def get_attribute(name) @attributes.fetch(name) end