Class: Serel::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/serel/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, qty) ⇒ Relation

Returns a new instance of Relation.



5
6
7
8
9
10
11
12
13
# File 'lib/serel/relation.rb', line 5

def initialize(type, qty)
  @type = type
  @klass = Serel.const_get(type.to_s.classify)
  @scope = {
    api_key: Serel::Base.api_key,
    site: Serel::Base.site
  }
  @qty = qty
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *attrs, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/serel/relation.rb', line 38

def method_missing(sym, *attrs, &block)
  # If the base relation class responds to the method, call
  # it and merge in the resulting relation scope
  if @klass.respond_to?(sym)
    relation = @klass.send(sym, *attrs, &block)
    merge(relation)
    self
  end
  super(sym, *attrs, &block)
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/serel/relation.rb', line 3

def klass
  @klass
end

#qtyObject (readonly)

Returns the value of attribute qty.



3
4
5
# File 'lib/serel/relation.rb', line 3

def qty
  @qty
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/serel/relation.rb', line 3

def type
  @type
end

Instance Method Details

#allObject

Finder methods



86
87
88
89
90
91
92
# File 'lib/serel/relation.rb', line 86

def all
  if klass.respond_to?(:all)
    all_helper(1)
  else
    raise NoMethodError
  end
end

#find(*ids) ⇒ Serel::Response

Finds an object by an ID or list of IDs. For example:

Serel::Answer.find(1) # Find the answer with an ID of 1
Serel::Answer.find(1, 2, 3) # Find the answers with IDs of 1, 2 & 3

Parameters:

  • ids (Array)

    The ID or IDs of the objects you want returning.

Returns:

  • (Serel::Response)

    The data returned by the Stack Exchange API, parsed and pushed into our handy response wrapper.



101
102
103
104
105
106
107
108
# File 'lib/serel/relation.rb', line 101

def find(*ids)
  if klass.respond_to?(:find)
    arg = ids.length > 1 ? ids.join(';') : ids.pop
    url("#{@type}s/#{arg}").request
  else
    raise NoMethodError
  end
end

#getObject

Makes a request. If the URL is already set we just call #request, else we set the URL to the plural of the type and make the request.



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/serel/relation.rb', line 112

def get
  if scoping[:url]
    request
  else
    # Best check that the generic page getter is enabled here.
    if klass.respond_to?(:get)
      url("#{@type}s").request
    else
      raise NoMethodError
    end
  end
end

#merge(relation) ⇒ Object

Public: Merges two relation objects together. This is used in our awesome

new scoping engine!

relation - A Serel::Relation object with the same base class as the

current relation

Returns self



22
23
24
25
26
27
# File 'lib/serel/relation.rb', line 22

def merge(relation)
  if relation.instance_variable_get(:@type) != @type
    raise ArgumentError, 'You cannot merge two relation objects based on different classes'
  end
  @scope.merge!(relation.scoping)
end

#networkObject



77
78
79
80
# File 'lib/serel/relation.rb', line 77

def network
  @network = true
  self
end

#new_relationObject



34
35
36
# File 'lib/serel/relation.rb', line 34

def new_relation
  self
end

#requestObject

Request stuff



126
127
128
129
130
131
# File 'lib/serel/relation.rb', line 126

def request
  if (klass.network || @network)
    @scope[:network] = true
  end
  Serel::Request.new(@type, scoping, @qty).execute
end

#scopingObject

Scoping returns our internal scope defition. Things like url etc.



30
31
32
# File 'lib/serel/relation.rb', line 30

def scoping
  @scope
end