Class: Teamsupport::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/teamsupport/base.rb

Direct Known Subclasses

Identity

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

flat_pmap, pmap

Constructor Details

#initialize(attrs = {}) ⇒ Teamsupport::Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a new object

Parameters:

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


119
120
121
# File 'lib/teamsupport/base.rb', line 119

def initialize(attrs = {})
  @attrs = attrs || {}
end

Instance Attribute Details

#attrsHash (readonly) Also known as: to_h

Provide an attrs method for reading object attributes

Examples:

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

Returns:

  • (Hash)


19
20
21
# File 'lib/teamsupport/base.rb', line 19

def attrs
  @attrs
end

Class Method Details

.attr_reader(*attrs) ⇒ Object, ...

Define methods for reading and checking existence of attributes

Examples:

attr_reader :attrs

Parameters:

  • attrs (Array, Symbol)

Returns:

  • (Object, Boolean, nil)


34
35
36
37
38
39
# 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) ⇒ Object?

Dynamically define a method for getting the value of an attribute

Examples:

define_attribute_method(key1, klass, key2)

Parameters:

  • key1 (Symbol)
  • klass (Symbol) (defaults to: nil)
  • key2 (Symbol) (defaults to: nil)

Returns:

  • (Object, nil)


84
85
86
87
88
89
90
91
92
# 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) ⇒ Boolean

Dynamically define a method for checking existence of an attribute

Examples:

define_predicate_method(key1)

Parameters:

  • key1 (Symbol)
  • key2 (Symbol) (defaults to: key1)

Returns:

  • (Boolean)


105
106
107
108
109
# 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

.object_attr_reader(klass, key1, key2 = nil) ⇒ Object, ...

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

Examples:

object_attr_reader :User, :user, :status

Parameters:

  • klass (Symbol)
  • key1 (Symbol)
  • key2 (Symbol) (defaults to: nil)

Returns:

  • (Object, Boolean, nil)


67
68
69
70
# 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) ⇒ Boolean

Define methods for checking existence of attributes

Examples:

predicate_attr_reader :favorited

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/teamsupport/base.rb', line 49

def predicate_attr_reader(*attrs)
  attrs.each do |attr|
    define_predicate_method(attr)
  end
end

Instance Method Details

#[](method) ⇒ String?

Deprecated.

Please use ##method to fetch the value instead

Fetches an attribute of an object using hash notation

Examples:

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

Parameters:

  • method (String, Symbol)

    Message to send to the object

Returns:

  • (String, nil)


136
137
138
139
140
141
# 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