class Redd::Models::BasicModel

The base class for all models.

Attributes

client[R]

@return [APIClient] the client the model was initialized with

Public Class Methods

from_id(_client, _value) click to toggle source

@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
new(client, attributes = {}) click to toggle source

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

inspect() click to toggle source

@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
method_missing(method_name, *args, &block) click to toggle source

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

Calls superclass method
# 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
respond_to_missing?(method_name, include_private = false) click to toggle source

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

Calls superclass method
# 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() click to toggle source

@return [Array<self>] an array representation of self

# File lib/redd/models/basic_model.rb, line 34
def to_ary
  [self]
end
to_h() click to toggle source

@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

after_initialize() click to toggle source

@abstract Lets us plug in custom code without making a mess

# File lib/redd/models/basic_model.rb, line 63
def after_initialize; end
depredicate(method_name) click to toggle source

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_attribute(name) click to toggle source

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