Class: TheCity::Base
- Inherits:
-
Object
- Object
- TheCity::Base
- Extended by:
- Forwardable
- Defined in:
- lib/the_city/base.rb
Direct Known Subclasses
Account, Content, Group, Permissions, RateLimit, Terminology, User
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_h, #to_hash, #to_hsh)
readonly
Returns the value of attribute attrs.
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.
-
.define_uri_method(key1, key2) ⇒ Object
Dynamically define a method for a URI.
-
.from_response(response, options) ⇒ TheCity::Base
Construct an object from a response hash.
-
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object
Define object methods from attributes.
-
.uri_attr_reader(*attrs) ⇒ Object
Define URI methods from attributes.
Instance Method Summary collapse
-
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation.
-
#initialize(attrs = {}, options = {}) ⇒ TheCity::Base
constructor
Initializes a new object.
- #memoize(key, &block) ⇒ Object
Constructor Details
#initialize(attrs = {}, options = {}) ⇒ TheCity::Base
Initializes a new object
111 112 113 114 |
# File 'lib/the_city/base.rb', line 111 def initialize(attrs={}, ={}) @attrs = attrs || {} @client = .delete(:client) rescue nil end |
Instance Attribute Details
#attrs ⇒ Object (readonly) Also known as: to_h, to_hash, to_hsh
Returns the value of attribute attrs.
7 8 9 |
# File 'lib/the_city/base.rb', line 7 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/the_city/base.rb', line 16 def self.attr_reader(*attrs) for attr in attrs define_attribute_method(attr) define_predicate_method(attr) end end |
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ Object
Dynamically define a method for an attribute
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/the_city/base.rb', line 66 def self.define_attribute_method(key1, klass=nil, key2=nil) define_method(key1) do memoize(key1) do if klass.nil? @attrs[key1] else if @attrs[key1] if key2.nil? TheCity.const_get(klass).new(@attrs[key1]) else attrs = @attrs.dup value = attrs.delete(key1) TheCity.const_get(klass).new(value.update(key2 => attrs)) end else TheCity::NullObject.instance end end end end end |
.define_predicate_method(key1, key2 = key1) ⇒ Object
Dynamically define a predicate method for an attribute
93 94 95 96 97 |
# File 'lib/the_city/base.rb', line 93 def self.define_predicate_method(key1, key2=key1) define_method(:"#{key1}?") do !!@attrs[key2] end end |
.define_uri_method(key1, key2) ⇒ Object
Dynamically define a method for a URI
53 54 55 56 57 58 59 |
# File 'lib/the_city/base.rb', line 53 def self.define_uri_method(key1, key2) define_method(key1) do memoize(key1) do ::URI.parse(@attrs[key2]) if @attrs[key2] end end end |
.from_response(response, options) ⇒ TheCity::Base
Construct an object from a response hash
103 104 105 |
# File 'lib/the_city/base.rb', line 103 def self.from_response(response, ) new(response[:body], ) end |
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object
Define object methods from attributes
28 29 30 31 |
# File 'lib/the_city/base.rb', line 28 def self.object_attr_reader(klass, key1, key2=nil) define_attribute_method(key1, klass, key2) define_predicate_method(key1) end |
.uri_attr_reader(*attrs) ⇒ Object
Define URI methods from attributes
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/the_city/base.rb', line 36 def self.uri_attr_reader(*attrs) for uri_key in attrs array = uri_key.to_s.split("_") index = array.index("uri") array[index] = "url" url_key = array.join("_").to_sym define_uri_method(uri_key, url_key) define_predicate_method(uri_key, url_key) alias_method(url_key, uri_key) alias_method("#{url_key}?", "#{uri_key}?") end end |
Instance Method Details
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation
119 120 121 122 123 |
# File 'lib/the_city/base.rb', line 119 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#memoize(key, &block) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/the_city/base.rb', line 125 def memoize(key, &block) ivar = :"@#{key}" return instance_variable_get(ivar) if instance_variable_defined?(ivar) result = block.call instance_variable_set(ivar, result) end |