Class: Redd::Models::BasicModel

Inherits:
Object
  • Object
show all
Defined in:
lib/redd/models/basic_model.rb

Overview

The base class for all models.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes = {}) ⇒ BasicModel

Create a non-lazily initialized class.

Parameters:

  • client (APIClient)

    the client that the model uses to make requests

  • attributes (Hash) (defaults to: {})

    the class’s attributes



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.

Parameters:

  • method_name (Symbol)

    the name of the attribute

Returns:

  • (Object)

    the result of the attribute check



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

#clientAPIClient (readonly)

Returns the client the model was initialized with.

Returns:

  • (APIClient)

    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

This method is abstract.

Create an instance from a value.

Parameters:

  • _client (APIClient)

    the api client to initialize the object with

  • _value (Object)

    the object to coerce

Returns:



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

#inspectString

Returns an easily readable representation of the object.

Returns:

  • (String)

    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.

Parameters:

  • method_name (Symbol)

    the method name or attribute to check

  • include_private (Boolean) (defaults to: false)

    whether to also include private methods

Returns:

  • (Boolean)

    whether the method is handled 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_aryArray<self>

Returns an array representation of self.

Returns:

  • (Array<self>)

    an array representation of self



34
35
36
# File 'lib/redd/models/basic_model.rb', line 34

def to_ary
  [self]
end

#to_hHash

Returns a Hash representation of the object.

Returns:

  • (Hash)

    a Hash representation of the object



29
30
31
# File 'lib/redd/models/basic_model.rb', line 29

def to_h
  @attributes
end