Class: Turrialba::Base
- Inherits:
-
Object
- Object
- Turrialba::Base
- Includes:
- Memoizable
- Defined in:
- lib/turrialba/base.rb
Instance Attribute Summary collapse
- #attrs ⇒ Hash (also: #to_h)
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from attributes.
-
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ Object
Dynamically define a method for an attribute.
-
.define_predicate_method(key1, key2 = key1) ⇒ Object
Dynamically define a predicate method for an attribute.
-
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object
Define object methods from attributes.
- .predicate_attr_reader(*attrs) ⇒ Object
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ Twitter::Base
constructor
Initializes a new object.
Constructor Details
#initialize(attrs = {}) ⇒ Twitter::Base
Initializes a new object
71 72 73 |
# File 'lib/turrialba/base.rb', line 71 def initialize(attrs = {}) @attrs = attrs || {} end |
Instance Attribute Details
#attrs ⇒ Hash Also known as: to_h
8 9 10 |
# File 'lib/turrialba/base.rb', line 8 def attrs @attrs end |
Class Method Details
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from attributes
16 17 18 19 20 21 |
# File 'lib/turrialba/base.rb', line 16 def attr_reader(*attrs) attrs.each do |attr| define_attribute_method(attr.to_s) define_predicate_method(attr.to_s) end end |
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ Object
Dynamically define a method for an attribute
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/turrialba/base.rb', line 44 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] : Twitter.const_get(klass).new(attrs_for_object(key1, key2)) end end memoize(key1) end |
.define_predicate_method(key1, key2 = key1) ⇒ Object
Dynamically define a predicate method for an attribute
59 60 61 62 63 64 |
# File 'lib/turrialba/base.rb', line 59 def define_predicate_method(key1, key2 = key1) define_method(:"#{key1}?") do !attr_falsey_or_empty?(key2) end memoize(:"#{key1}?") end |
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object
Define object methods from attributes
34 35 36 37 |
# File 'lib/turrialba/base.rb', line 34 def object_attr_reader(klass, key1, key2 = nil) define_attribute_method(key1, klass, key2) define_predicate_method(key1) end |
.predicate_attr_reader(*attrs) ⇒ Object
23 24 25 26 27 |
# File 'lib/turrialba/base.rb', line 23 def predicate_attr_reader(*attrs) attrs.each do |attr| define_predicate_method(attr) end end |