class Teamsupport::Base

Attributes

attrs[R]

Provide an attrs method for reading object attributes

@example

teamsupport_object = Teamsupport::Base.new(ID: 1)
teamsupport_object.attrs[:ID]

@return [Hash]

@api public

to_h[R]

Provide an attrs method for reading object attributes

@example

teamsupport_object = Teamsupport::Base.new(ID: 1)
teamsupport_object.attrs[:ID]

@return [Hash]

@api public

to_hash[R]

Provide an attrs method for reading object attributes

@example

teamsupport_object = Teamsupport::Base.new(ID: 1)
teamsupport_object.attrs[:ID]

@return [Hash]

@api public

Public Class Methods

attr_reader(*attrs) click to toggle source

Define methods for reading and checking existence of attributes

@example

attr_reader :attrs

@param attrs [Array, Symbol]

@return [Object, Boolean, nil]

@api public

# File lib/teamsupport/base.rb, line 34
def attr_reader(*attrs)
  attrs.each do |attr|
    define_attribute_method(attr)
    define_predicate_method(attr)
  end
end
define_attribute_method(key1, klass = nil, key2 = nil) click to toggle source

Dynamically define a method for getting the value of an attribute

@example

define_attribute_method(key1, klass, key2)

@param key1 [Symbol] @param klass [Symbol] @param key2 [Symbol]

@return [Object, nil]

@api public

# File lib/teamsupport/base.rb, line 84
def define_attribute_method(key1, klass = nil, key2 = nil)
  define_method(key1) do
    if attr_falsey_or_empty?(key1)
      NullObject.new
    else
      klass.nil? ? @attrs[key1] : Teamsupport.const_get(klass).new(attrs_for_object(key1, key2))
    end
  end
end
define_predicate_method(key1, key2 = key1) click to toggle source

Dynamically define a method for checking existence of an attribute

@example

define_predicate_method(key1)

@param key1 [Symbol] @param key2 [Symbol]

@return [Boolean]

@api public

# File lib/teamsupport/base.rb, line 105
def define_predicate_method(key1, key2 = key1)
  define_method(:"#{key1}?") do
    !attr_falsey_or_empty?(key2)
  end
end
new(attrs = {}) click to toggle source

Initializes a new object

@param attrs [Hash]

@return [Teamsupport::Base]

@api private

# File lib/teamsupport/base.rb, line 119
def initialize(attrs = {})
  @attrs = attrs || {}
end
object_attr_reader(klass, key1, key2 = nil) click to toggle source

Define methods for reading and checking existence of attributes within an object

@example

object_attr_reader :User, :user, :status

@param klass [Symbol] @param key1 [Symbol] @param key2 [Symbol]

@return [Object, Boolean, nil]

@api public

# File lib/teamsupport/base.rb, line 67
def object_attr_reader(klass, key1, key2 = nil)
  define_attribute_method(key1, klass, key2)
  define_predicate_method(key1)
end
predicate_attr_reader(*attrs) click to toggle source

Define methods for checking existence of attributes

@example

predicate_attr_reader :favorited

@return [Boolean]

@api public

# File lib/teamsupport/base.rb, line 49
def predicate_attr_reader(*attrs)
  attrs.each do |attr|
    define_predicate_method(attr)
  end
end

Public Instance Methods

[](method) click to toggle source

Fetches an attribute of an object using hash notation

@example

teamsupport_object = Teamsupport::Base.new(ID: 1)
teamsupport_object[:ID]

@param method [String, Symbol] Message to send to the object

@return [String, nil]

@api public

@deprecated Please use ##{method} to fetch the value instead

# File lib/teamsupport/base.rb, line 136
def [](method)
  warn "#{Kernel.caller.first}: [DEPRECATION] #[#{method.inspect}] is deprecated. Use ##{method} to fetch the value."
  send(method.to_sym)
rescue NoMethodError
  nil
end

Private Instance Methods

attr_falsey_or_empty?(key) click to toggle source

Check to see if the value for an object attribute is falsey or empty

@param key [Symbol]

@return [Boolean]

@api private

# File lib/teamsupport/base.rb, line 152
def attr_falsey_or_empty?(key)
  !@attrs[key] || @attrs[key].respond_to?(:empty?) && @attrs[key].empty?
end
attrs_for_object(key1, key2 = nil) click to toggle source

Fetch and/or update the attributes for an object

@param key1 [Symbol] @param key2 [Symbol]

@return [Hash]

@api private

# File lib/teamsupport/base.rb, line 164
def attrs_for_object(key1, key2 = nil)
  if key2.nil?
    @attrs[key1]
  else
    attrs = @attrs.dup
    attrs.delete(key1).merge(key2 => attrs)
  end
end