Class: Teamsupport::Base
- Inherits:
-
Object
- Object
- Teamsupport::Base
- Extended by:
- Forwardable
- Includes:
- Utils
- Defined in:
- lib/teamsupport/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attrs ⇒ Hash
(also: #to_h)
readonly
Provide an attrs method for reading object attributes.
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ Object, ...
Define methods for reading and checking existence of attributes.
-
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ Object?
Dynamically define a method for getting the value of an attribute.
-
.define_predicate_method(key1, key2 = key1) ⇒ Boolean
Dynamically define a method for checking existence of an attribute.
-
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object, ...
Define methods for reading and checking existence of attributes within an object.
-
.predicate_attr_reader(*attrs) ⇒ Boolean
Define methods for checking existence of attributes.
Instance Method Summary collapse
-
#[](method) ⇒ String?
deprecated
Deprecated.
Please use ##method to fetch the value instead
-
#initialize(attrs = {}) ⇒ Teamsupport::Base
constructor
private
Initializes a new object.
Methods included from Utils
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
119 120 121 |
# File 'lib/teamsupport/base.rb', line 119 def initialize(attrs = {}) @attrs = attrs || {} end |
Instance Attribute Details
#attrs ⇒ Hash (readonly) Also known as: to_h
Provide an attrs method for reading object attributes
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
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
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
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
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
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?
Please use ##method to fetch the value instead
Fetches an attribute of an object using hash notation
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 |