Class: TheCity::Base

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

Direct Known Subclasses

Account, Content, Group, Permissions, RateLimit, Terminology, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}, options = {}) ⇒ TheCity::Base

Initializes a new object

Parameters:

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


111
112
113
114
# File 'lib/the_city/base.rb', line 111

def initialize(attrs={}, options={})
  @attrs = attrs || {}
  @client = options.delete(:client) rescue nil
end

Instance Attribute Details

#attrsObject (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

Parameters:

  • attrs (Array, Symbol)


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

Parameters:

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


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

Parameters:

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

    a customizable set of options

Options Hash (key2):

  • (Symbol)


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

Parameters:

  • key1 (Symbol)
  • key2 (Symbol)


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

Parameters:

  • response (Hash)

Returns:



103
104
105
# File 'lib/the_city/base.rb', line 103

def self.from_response(response, options)
  new(response[:body], options)
end

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

Define object methods from attributes

Parameters:

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


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

Parameters:

  • attrs (Array, Symbol)


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

Parameters:

  • method (String, Symbol)

    Message to send to the object



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